Possible Duplicate:
Cannot execute Java program: UnsupportedClassVersionError

I'm a newbie to Ubuntu and Im looking to run Java code from the command line. Ive checked that path as well. The interesting thing is the code compiles but fails to run


user@ubuntu:~/py-scripts$ javac Main.java' works well.

but when I do .

`user@ubuntu:~/py-scripts$ java Main

I get the following error

Exception in thread "main" java.lang.UnsupportedClassVersionError: Main : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
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:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
 Could not find the main class: Main. Program will exit.'
  • What version of Java are you running? Run "java -version" and see what you get. – Kelley Apr 6 '12 at 14:53
  • can you post your Main.java – Tachyons Apr 6 '12 at 15:09
  • @tachyons ,@Kelley Main.java public class Main { public static void main (String[] args) { System.out.println ("Hello, world."); } } and java version:OpenJDK Runtime Environment (IcedTea6 1.11pre) (6b23~pre11-0ubuntu1.11.10.2) OpenJDK 64-Bit Server VM (build 20.0-b11, mixed mode) – seeker Apr 6 '12 at 15:12
  • whats the result of ls ? – Tachyons Apr 6 '12 at 15:18
  • verify that Main.class exit in your directory ,else change your directory to home folder – Tachyons Apr 6 '12 at 15:18

If you have multiple versions of the JRE/JDK installed (e.g. versions 6 and 7), you should make sure that the JRE you're running (using java) uses a version that is class-compatible with what you've used to compile the class (e.g. JRE 7 for classes compiled with JDK 6 or 7, but not JRE 6 for classes compiled with JDK 7).

You can keep multiple versions installed and choose which one is used by java and javac using update-alternatives --config java and update-alternatives --config javac (as documented here).

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.