Java Unsupported Major Minor version 51 ERROR

Because it is annoying, let’s take a look at this. What does this error mean? It turns out to be an issue of version incompatibility. First of all, if you don’t know, here is some background on how Java is run.

1. The Java source code (the .java file) is compiled. This step generates a .class file with the same prefix as the .java. In other words, if your source code is Hello.java, your generated class file is Hello.class. This step is done by a Java compiler. If you run Java from command line, it is javac Hello.java

2. Next, the .class file (called java bytecode) is run using a Java Virtual Machine (JVM). If you run in from command line, it is java Hello

Ok, back to error message. from Java class file, the number 51.0 is the version of the Java Class file (the ByteCode). 51 is Java 7, 50 is Java 6… The error message means that the Java Class file is compatible with Java 7 but the JVM is not. This happens when your source code was compiled by Java 7 and your run the bytecode with older JVM:

1

2

*.java--(Java compiler7)-->*.class-->(Older Java virtual machine)

How to fix it (for Linux Ubuntu)? You can fix this by either compiling your source code with older Java compiler OR running your bytecode with newer Java virtual machine. I will show you the second way: Installing Oracle JDK 7, which includes the JVM into your machine.

6. That’s basically it. If you run your Java inside Eclipse, you also need to update your Eclipse setting to make it work. In Eclipse Menu bar, open: Windows/Preference/Java/Installed JREs. Click ‘Add’ and add the path to the JRE 7 in the /usr/lib/jvm/jdk_version/jre. Apply, save and Eclipse should work fine.