Java version mismatch

If you’ve tried developing with Java on Ubuntu, Fedora or any other Brand X GNU/Linux distribution, you might have run into a simple, yet frustrating problem when trying to run your programs.

If your programs are spitting out Exception in thread "main" java.lang.UnsupportedClassVersionError: [myprog] : Unsupported major.minor version 51.0 when you try to run them, you’ve come to the right place. Read on…

The typical installation procedure for the Java toolchain on such systems is as follows:

  1. Make sure the Java runtime environment (or JRE, which includes the interpreter software that actually executes Java programs) is installed.
  2. Install the Java development kit (JDK) which includes the Java compiler.

You’ll usually install both of these through your distribution’s package manager. All may seem to be going well: you write your Java program:

…you compile it:

…and then you run it—ah.

Exception in thread "main" java.lang.UnsupportedClassVersionError: myprog : 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: myprog. Program will exit.

The problem here is a lot simpler than that cryptic error message makes it look. As I’ve mentioned, there are two things you need on your computer to develop and run Java programs:

  • The Java Runtime Environment (JRE), which runs Java programs;
  • The Java Development Kit (JDK), which compiles Java programs so that they are ready for execution by the runtime environment.

The error message listed above when the JDK you have used to compile the program is a newer version than the JRE that is the default one on your computer. For instance, you might have the Java 6 runtime set as your default, but OpenJDK 7′s compiler set as your default Java compiler.

The Java 6 JRE cannot run programs compiled with Java 7′s compiler. This isn’t true the other way round—JREs are backward-compatible, so can run programs compiled with older JDKs. For this reason, you should always make sure you have the newest version of Java’s runtime environment installed, and that this matches the version of the JDK you installed.

The package manager usually refers to these packages with names like openjdk-7-jre or openjdk-7-jdk. Do a search for packages with openjdk in the name and install the relevant ones for your distribution.

If, after doing this, your program still won’t run, and you’re certain you’ve installed the correct JRE, you may still have the old JRE set as your default. You can change this using a utility most Linux distributions provide called update-alternatives. Your session might look like this:

jonathan@Durandal-UOB:~/Desktop$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-openjdk/jre/bin/java         1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk/jre/bin/java         1061      manual mode
  2            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1051      manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java to provide /usr/bin/java (java) in manual mode.
jonathan@Durandal-UOB:~/Desktop$ java myprog
Java is HORRIBLE
jonathan@Durandal-UOB:~/Desktop$

This should fix the problem. If you have further issues, it might be time to have a look on your distribution’s forums or support pages to find a solution. Alternatively, you can just stop using Java, since it’s a horrible language encumbered by an exceptionally heavy runtime, patents and dodgy conventions, and learn a decent language like Go, Vala, Python or Ruby. :)

Good luck!

NOTE TO TEACHERS: Feel free to copy this to your own intranets or provide copies for your students if it’s helpful. Please ensure you include a link back to this page.



5 responses to “Java version mismatch

  1. Pingback: Unsupported version errors in Java? | Jonathan Rothwell

  2. I just wanted to thank you so much for making this so simple to correct. I am new to Java (although I know a lot of other languages) and some of the simplest things are still hanging me up. Mostly relating to missing classes or in this case, the wrong JRE running even thought I had the correct one installed.

  3. thanks a lot for providing me the correct information

    I am in troble with java jre for exection of my java programs
    now it is clred

    thanks a lot

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>