0

My java program was compiled succesfully. But when I tried to run I got

Exception in thread "main" java.lang.UnsupportedClassVersionError: hello :
        Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: hello. Program will exit.
1
  • have you got any hello.class files in the directory where you compiled java code
    – Chinmaya B
    Jul 20, 2014 at 6:59

1 Answer 1

0

You seem to use two different java versions for compiling and running the program, like 1.8 and 1.6, with the compiler being much more recent.

Could be that the JDK used is in path after a JRE of a different version. Take a look at

echo $PATH

and

type javac
type java

javac -version
java -version


For example, if you have the following versions:
javac 1.7.0_55 and
java 1.6.0_31, you could either put JDK/JRE 1.7 in $PATH before 1.6 - to compile and run with 1.7;
or get a 1.6 JDK and put that in $PATH before the JDK you currently use - to compile and run with 1.6.

3
  • javac -version javac 1.7.0_55 java -version java version "1.6.0_31" OpenJDK Runtime Environment (IcedTea6 1.13.3) (6b31-1.13.3-1ubuntu1~0.12.04.2) OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode) yes they are different.Now what should I do Jul 22, 2014 at 1:33
  • @SowmyaRavichandran Either put 1.7 in $PATH before 1.6 - to compile and run with 1.7; or get a 1.6 JDK and put that in $PATH before the JDK you currently use - to compile and run with 1.6. Jul 22, 2014 at 2:59
  • @SowmyaRavichandran Glad to help - you should "accept" the answer with the checkmark if it solved the problem, and maybe upvote it. (Otherwise it will technically stay unanswered forever) Jul 23, 2014 at 1:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .