Unsupported major.minor version 52.0 comes when you are trying to run a class compiled using Java 1.8 compiler into a lower JRE version e.g. JRE 1.7 or JRE 1.6. Simplest way to fix this error is install the latest Java release i.e. Java 8 and run your program. If upgrading to Java 8 is not an option then make sure you use Java compiler's cross compilation feature and compile source code for lower Java version by using -target option of javac command. If you have multiple JRE installed make sure that the latest one comes first in PATH environment variable. You can get "java.lang.unsupportedclassversionerror unsupported major.minor version 52.0" while running a Java program explicitly using java command or running a Java Applet in browser or running a Java program from command line but compiled in Eclipse or any other build tool like Maven or ANT. Main reason of this error is that during compilation, you have used a higher version of JDK but during deployment, you have deployed into a lower version of JDK or JRE. Let's understand the cause and solution of Unsupported major.minor version 52.0 Error in little more detail.
TL:DR; upgrade to Java 8 or compile for lower JRE version using java -target 1.6 option.
You can see that Java 8 has major version 52, which means if you run javac command from Java 8 installation, it will by default generate a class with major version 52. If you run this class file in JRE 7, you will get "Unsupported major.minor version 52.0". Same is the case with an applet compiled in JDK 1.8, running in a browser with JRE 1.7.
BTW, Java allows you to generate class files supported by lower version. So, for example, you can generate a Java class file for JDK 1.4 by running javac command with -target option as shown below :
The Helloworld.class file generated by this command can be run in any version of Java 1.4 onward e.g. Java 1.4, Java 5 or Java 6.
If you cannot upgrade to higher Java version e.g. your production is running on JDK 1.6 and your latest release is failing due to "Unsupported major.minor version 52.0" error then you need to fix your build environment. Make sure to install the same JDK version for building your Java application as you are going to run in production. If that's not possible, use Java compiler's cross compilation option to generate class file for your production Java/JRE version using javac -target option. For example, if your build environment is using Java 7 and your production environment is using Java 1.6 then compile using java -target 1.6 option to generate class files for Java 6.
That's all about how to fix Unsupported major.minor version 52.0 Error in Java. In short, you are getting this error because it is compiled with Java 8, source version 52 corresponds to Java 1.8 release but you are trying to run this program in lower JRE e.g. Java 7 or Java 6. This means you have two solution, first upgrade to Java 8 or compiler your program for lower version even if you are using Java 8 compiler by using -target option of javac command. If you are using Maven, ANT or any other build tool to generate your class files, make sure they have got the right settings too.
TL:DR; upgrade to Java 8 or compile for lower JRE version using java -target 1.6 option.
Cause of Unsupported major.minor version 52.0 Error in Java
Many people think why do you get a version mismatch error if Java is backward compatible. Well, its true that Java is backward compatible, which means you can run a Java class file or Java binary (JAR file) compiled in lower version (java 6) into higher version e.g. Java 8, but it doesn't mean that you can run a class compiled using Java 7 into Java 5, Why? because higher version usually have features which are not supported by lower version. Think about, can you run lambda expression or Stream API code in Java 5, no right. Anyway, even if you are not using features which is not supported in lower version, every class file has a major or minor version, which is populated by Java compiler, which is closely related to Java version. Here are the major version of every JRE released so far :Java SE 8 = 52, Java SE 7 = 51, Java SE 6.0 = 50, Java SE 5.0 = 49, JDK 1.4 = 48, JDK 1.3 = 47, JDK 1.2 = 46, JDK 1.1 = 45
You can see that Java 8 has major version 52, which means if you run javac command from Java 8 installation, it will by default generate a class with major version 52. If you run this class file in JRE 7, you will get "Unsupported major.minor version 52.0". Same is the case with an applet compiled in JDK 1.8, running in a browser with JRE 1.7.
BTW, Java allows you to generate class files supported by lower version. So, for example, you can generate a Java class file for JDK 1.4 by running javac command with -target option as shown below :
javac -target 1.4 HelloWorld.java
The Helloworld.class file generated by this command can be run in any version of Java 1.4 onward e.g. Java 1.4, Java 5 or Java 6.
Solution of Unsupported major.minor version 52.0 Error in Java
As I said earlier, there are two ways to fix "Unsupported major.minor version 52.0" error, first upgrade to higher Java version or compiler Java source files for lower Java version of target environment. If you are getting "java.lang.unsupportedclassversionerror HelloWorld unsupported major.minor version 52.0", which means Java version in your PATH is lesser than Java 8. If you already installed the JDK 1.8 then its due to incorrect PATH setting. If you have multiple JRE version installed then lower version is coming ahead in PATH than higher version. To fix this, examine your PATH environment variable and make sure the latest version of Java comes first. You don't need to touch your CLASSPATH, because its not a classpath issue. Remember, difference between PATH and CLASSPATH.If you cannot upgrade to higher Java version e.g. your production is running on JDK 1.6 and your latest release is failing due to "Unsupported major.minor version 52.0" error then you need to fix your build environment. Make sure to install the same JDK version for building your Java application as you are going to run in production. If that's not possible, use Java compiler's cross compilation option to generate class file for your production Java/JRE version using javac -target option. For example, if your build environment is using Java 7 and your production environment is using Java 1.6 then compile using java -target 1.6 option to generate class files for Java 6.
In Applet
If you are trying to run an Applet from a website (internal or external) then you don't have any control on build or compile step. Your only option is to upgrade the JRE plugin of your browser to Java 8. Once you done that you should be able to run the Applet compiled in any Java version without any issue, remember Java is backward compatible. BTW, from Java 8 onward security has been tighten and you cannot run an unsigned or self signed Applet in Java, especially if you have HIGH security setting in Java Control Panel. Java 8 has also removed the MEDIUM security settings, which earlier shows warning when you run such Applets.In Eclipse
If you are compiling your Java program in Eclipse and running it from command line, then you have the option to compile Java files for target environment. This is also necessary if you are exporting JARs from Eclipse. You can change compiler setting in Eclipse by selecting your project, properties, java compiler and unchecked JDK compliance checkbox to change the Compiler compliance level. By default Eclipse compiles based upon JRE version of the project, which could be different in Java in your PATH, which comes into play when you run your Java program from command line. Here is the screenshot of changing compiler compliance level in Eclipse :- Select your project
- Right click, select properties
- Choose Java Compiler
- Uncheck JDK compilance check box
That's all about how to fix Unsupported major.minor version 52.0 Error in Java. In short, you are getting this error because it is compiled with Java 8, source version 52 corresponds to Java 1.8 release but you are trying to run this program in lower JRE e.g. Java 7 or Java 6. This means you have two solution, first upgrade to Java 8 or compiler your program for lower version even if you are using Java 8 compiler by using -target option of javac command. If you are using Maven, ANT or any other build tool to generate your class files, make sure they have got the right settings too.
No comments:
Post a Comment