List of Java class file format major version numbers?
JavaVersionJava Problem Overview
I saw this list of major version numbers for Java in another post:
> * Java 1.2 uses major version 46 > * Java 1.3 uses major version 47 > * Java 1.4 uses major version 48 > * Java 5 uses major version 49 > * Java 6 uses major version 50 > * Java 7 uses major version 51 > * Java 8 uses major version 52 > * Java 9 uses major version 53 > * Java 10 uses major version 54 > * Java 11 uses major version 55 > * Java 12 uses major version 56 > * Java 13 uses major version 57 > * Java 14 uses major version 58 > * Java 15 uses major version 59 > * Java 16 uses major version 60 > * Java 17 uses major version 61 > * Java 18 uses major version 62 > * Java 19 uses major version 63
Where does this list come from? Is there a specific reference for this? Preferably something that shows minor versions too?
Java Solutions
Solution 1 - Java
These come from the class version. If you try to load something compiled for java 6 in a java 5 runtime you'll get the error, incompatible class version, got 50, expected 49. Or something like that.
See here in byte offset 7 for more info.
Additional info can also be found here.
Solution 2 - Java
I found a list of Java class file versions on the Wikipedia page that describes the class file format:
http://en.wikipedia.org/wiki/Java_class_file#General_layout
Under byte offset 6 & 7, the versions are listed with which Java VM they correspond to.
Solution 3 - Java
Official source for major version number:
Java Virtual Machine Specification, Chapter 4. The class File Format
The latest published version of the JVM spec can be found here.
Solution 4 - Java
If you're having some problem about "error compiler of class file", it's possible to resolve this by changing the project's JRE to its correspondent through Eclipse.
- Build path
- Configure build path
- Change library to correspondent of table that friend shows last.
- Create "jar file" and compile and execute.
I did that and it worked.
Solution 5 - Java
If you have a class file at build/com/foo/Hello.class
, you can check what java version it is compiled at using the command:
javap -v <path to class file> | grep "major"
Example usage:
$ javap -v build/classes/com/example/Book.class | grep major
major version: 57
According to the table in the OP, major version 57 means the class file was compiled to JDK 13 bytecode level
Solution 6 - Java
(Really an extended comment than an answer ..)
The question was regarding "where does the list of versions come from".
That was answered in this response, which references the JAVA SE Specification on: "The class
File Format". Seems that's pretty authoritative (it's also referenced in the Wiki for the byte 6 (major version) values) and should be the accepted answer.
Several answers seem to focus on how to determine the value using javap
or not using it. Those should be separate questions. Nevertheless, a non- javap
means of finding the version is unix command file
. file
reads the magic
, which is specified in the ClassFile structure.
ie: file myClass.class
, or more elegantly, file -b myClass.class | awk -F',' '{print $NF}'
eg:
$ find * -name "*.class" -exec file -b {} \; | awk -F',' '{print $NF}' | sort -u
version 45.3
version 50.0 (Java 1.6)
version 52.0 (Java 1.8)
Solution 7 - Java
I use javap in my .lessfilter for classes, so I can decompile and know what version they were compiled with directly
*.class)
echo "/** "
javap -verbose "$1" | grep version | sed -e 's/50/Java6/' -e 's/51/Java7/' -e 's/52/Java8/' -e 's/53/Java9/' -e 's/54/Java10/' -e 's/55/Java11/' -e 's/56/Java12/' -e 's/57/Java13/'
echo " **/"
java -jar ~/bin/cfr-0.150.jar "$1" | enscript --color --language=ansi --highlight=java -o - -q
;;
(tried to add as a comment to previous answer, but couldn't get the code to be formatted, and thought this might be useful for others)