- Java: Determining the Java version used to compile a class, ‘class file has the wrong version’
- Quick Resolution
- Background
- Determining major version
- Compiling to lower versions
- How to Determine Java Version
- Verifying Online
- Windows
- Mac OSX
- Linux
- Expert Q&A
- You Might Also Like
- How to Determine Java Version
- Verifying Online
- Windows
- Mac OSX
- Linux
- Expert Q&A
- You Might Also Like
Java: Determining the Java version used to compile a class, ‘class file has the wrong version’
If a Java class file is compiled with a higher supported version than is currently being run, you will get the ‘bad class file’, ‘class file has the wrong version XX.0, should be XX.0’ error message.
For example, if the .class file was compiled as a Java 1.8 class file on a Jenkins continuous integration node, but the JRE on the desktop where you want to run it only has Java 1.7, then you will get this message when you attempt to run it.
The good news is that this is a relatively simple issue to address.
Quick Resolution
The easiest way to resolve this issue is to simply move up the higher Java version expected. The error message indicates ‘should be XX.0’, and the table below shows which JVM to use.
But there are also times when for security reasons or enterprise standards, that you cannot upgrade your JVM just to accommodate a single class or library. In these cases, you can look for older versions of the library or if the project is open-source, you can investigate recompiling at a lower level as long as newer features and packages are not utilized.
Background
Here is a table of the JVM versions mapped to the corresponding major version:
JVM | Major Version (decimal) |
Java 1.4 | 48 |
Java 5 | 49 |
Java 6 | 50 |
Java 7 | 51 |
Java 8 | 52 |
Java 9 | 53 |
Java 10 | 54 |
Java 11 | 55 |
Java 12 | 56 |
Java 13 | 57 |
Java 14 | 58 |
This major version number is stored in the header of the .class file, at byte 7.
Determining major version
If you have a JDK (Java Development Kit) installed on your machine, then you can use ‘javap’ to look into the class file. Note that a JRE (runtime) will not have this utility available.
$ javap -verbose MyClass | grep "major"
> javap -verbose MyClass | findstr "major"
If you don’t have access to javap, you can also using a console based utility or GUI hex editor to look at the value of the byte at position 7 in the file. Using Linux, this is easily done using the ‘od’ dump standard utility.
$ od --format=d1 MyClass.class -j 7 -N 1 0000007 51
0000011
Compiling to lower versions
Even if you are using a newer JDK, you can still compile code compatible with older JVM specifications by using the ‘-target’ parameter. If you target a lower JVM, it is important that no features/packages from the newer specification are used.
For example, even if the ‘javac’ being used below is from a 1.8 JVM, it can still create 1.7 compatible class files if you use the parameters below.
$ javac -classpath . MyClass.java -source 1.7 -target 1.7
Compiling to lower versions using Maven
If a project uses Maven, then the way to specify the JVM compatibility is in the pom.xml ‘maven-compiler-plugin’ section.
. .org.apache.maven.plugins maven-compiler-plugin 3.7.0 1.7
How to Determine Java Version
This article was co-authored by Yaffet Meshesha. Yaffet Meshesha is a Computer Specialist and the Founder of Techy, a full-service computer pickup, repair, and delivery service. With over eight years of experience, Yaffet specializes in computer repairs and technical support. Techy has been featured on TechCrunch and Time.
This article has been viewed 392,784 times.
Multiple copies of Java can be installed on a single computer, and, if you have more than one browser, every one of them could be using a different version or none at all. You can determine your Java version by verifying it on the Java website, or you could check using the Windows command prompt, Mac terminal, or Linux terminal.
Verifying Online
Open a new window in your web navigate to Java’s website. Oracle, the makers of Java, have provided an easy page that will check your Java installation and report the exact version you’re running. This can be done from any operating system.
After a few seconds, check the results! This will include the Version number as well as the Update number. The version number is most important if you’re checking for compatibility with other programs.
Windows
Wait for a black window with white text to appear on your screen. This is called a «console window.»
- You can tell what version of Java you have by looking at the number after the «1.» in the first line of the console output. In this example, the user has Java 8 installed.
Mac OSX
With Utilities open, open the Terminal and type in «java -version», then retrieve the current java version.
Linux
- This should return something like Java(TM) 2 Runtime Environment, Standard Edition (build 1.6) If it returns
-bash: java: command not found
This means that either it is not installed or you haven’t set up your paths properly
- With Firefox 3 go to Tools Add-ons button and go to the Plug-ins tab.
- With Firefox 2 or 3: In Firefox versions 2 and 3, enter in the address bar: about:plugins. If Java is installed there will be multiple Java entries.
- With Internet Explorer 7 or 8 go to Tools select Internet Options in the General tab click on the Browsing History Settings button and select the View Objects button right click on an ActiveX control and get its properties. Each ActiveX control has a «code base» and for each of the Java entries it will display the version number.
Expert Q&A
You Might Also Like
Use Easy Windows CMD Commands to Check Your Java Version
How to Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
How to Do Division in Java (Integer and Floating Point)
How to Compile and Run Java Programs Using Notepad++
Simple Steps to Type a Bunny with Your Keyboard
How to Determine Java Version
This article was co-authored by Yaffet Meshesha. Yaffet Meshesha is a Computer Specialist and the Founder of Techy, a full-service computer pickup, repair, and delivery service. With over eight years of experience, Yaffet specializes in computer repairs and technical support. Techy has been featured on TechCrunch and Time.
This article has been viewed 392,784 times.
Multiple copies of Java can be installed on a single computer, and, if you have more than one browser, every one of them could be using a different version or none at all. You can determine your Java version by verifying it on the Java website, or you could check using the Windows command prompt, Mac terminal, or Linux terminal.
Verifying Online
Open a new window in your web navigate to Java’s website. Oracle, the makers of Java, have provided an easy page that will check your Java installation and report the exact version you’re running. This can be done from any operating system.
After a few seconds, check the results! This will include the Version number as well as the Update number. The version number is most important if you’re checking for compatibility with other programs.
Windows
Wait for a black window with white text to appear on your screen. This is called a «console window.»
- You can tell what version of Java you have by looking at the number after the «1.» in the first line of the console output. In this example, the user has Java 8 installed.
Mac OSX
With Utilities open, open the Terminal and type in «java -version», then retrieve the current java version.
Linux
- This should return something like Java(TM) 2 Runtime Environment, Standard Edition (build 1.6) If it returns
-bash: java: command not found
This means that either it is not installed or you haven’t set up your paths properly
- With Firefox 3 go to Tools Add-ons button and go to the Plug-ins tab.
- With Firefox 2 or 3: In Firefox versions 2 and 3, enter in the address bar: about:plugins. If Java is installed there will be multiple Java entries.
- With Internet Explorer 7 or 8 go to Tools select Internet Options in the General tab click on the Browsing History Settings button and select the View Objects button right click on an ActiveX control and get its properties. Each ActiveX control has a «code base» and for each of the Java entries it will display the version number.
Expert Q&A
You Might Also Like
Use Easy Windows CMD Commands to Check Your Java Version
How to Set JAVA_HOME for JDK & JRE: A Step-by-Step Guide
How to Do Division in Java (Integer and Floating Point)
How to Compile and Run Java Programs Using Notepad++
Simple Steps to Type a Bunny with Your Keyboard