Monday, September 09, 2013

AWS Linux EC2 instance: java.lang.UnsupportedClassVersionError Unsupported major.minor version 51.0 on

If you are getting this error trying to run a Java application you compiled on Windows and are trying to run on a Linux AWS EC2 instance, it is probably because the AWS EC2 instance comes with an OpenJDK version of Java that is not compatible:

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

To fix this problem you could attempt to remove that version of Java and change JAVA_HOME to point to the version of Java you want, however I am not sure if that will cause anything AWS related to malfunction on the Linux instance.

An alternative is just to specify the path to the version of java you want to run when you run your application

So instead of:

java -jar com.radicalsoftware.jar

Use this:

/usr/java/jdk1.7.0_25/java -jar com.radicalsoftware.jar

You'll need to install the Oracle JDK of course as noted in this post:

http://websitenotebook.blogspot.com/2013/09/install-oracle-java-jdk-on-amazon-linux.html