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:
- Make sure the Java runtime environment (or JRE, which includes the interpreter software that actually executes Java programs) is installed.
- 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:
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-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.
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.