710

I saw this list of major version numbers for Java in another post:

Java Major version
25 69
24 68
23 67
22 66
21 65
20 64
19 63
18 62
17 61
16 60
15 59
14 58
13 57
12 56
11 55
10 54
9 53
8 52
7 51
6 50
5 49
1.4 48
1.3 47
1.2 46
1.1 45
1.0.2 45

Where does this list come from? Is there a specific reference for this? Preferably something that shows minor versions too?

2
  • 37
    Kudos to the people updating this question every year because it's one of the top google entries... Commented May 15, 2024 at 21:02
  • 11
    @Zoomzoom It's one of the top entries because the "version" construct Java uses is literally INSANE. If anyone who's not a paid JAVA-ONLY programmer understands it, I'd be surprised! It has to be the worst version-naming "convention" ever invented in computer science history... Commented Jul 19, 2024 at 14:18

7 Answers 7

162

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to echo the major version number (class version) directly from javac, without using an existing class like, javap -verbose MyClass?
No there isn't.
@samus It's possible from javacommand as shown in this answer.
70

Major Version Number

Official source for major version number:

Java SE 25 Virtual Machine Specification, Section 1.2. The Java Virtual Machine

From Java 10 up to 24 this can be found in
Virtual Machine Specification, Chapter 4. The class File Format (link for Java 24)

The latest published version of the JVM spec can be found here (including some previous versions)

Minor Version Number

Chapter 4 of JVM (see link above):

For a class file whose major_version is 56 or above, the minor_version must be 0 or 65535.
For a class file whose major_version is between 45 and 55 inclusive, the minor_version may be any value.
...
A class file is said to depend on the preview features of Java SE N (N ≥ 12) if it has a major_version that corresponds to Java SE N (according to Table 4.1-A) and a minor_version of 65535.

Comments

28

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.

Comments

17

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

1 Comment

file /path/to/a.class also returns a major version of a class while java -XshowSettings:properties -version returns local java.class.version
6

(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)

Comments

3

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.

  1. Build path
  2. Configure build path
  3. Change library to correspondent of table that friend shows last.
  4. Create "jar file" and compile and execute.

I did that and it worked.

1 Comment

The specific error will be more like <class> has been compiled by a more recent version of the Java Runtime (class file version XY.Z), this version of the Java Runtime only recognizes class file versions up to AB.C. And note that bumping the JRE/JDK isn't a cure-all, since some dependencies may do things disallowed by current versions of Java (such as illegal reflective access).
3

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.