List of Java Class File Format Major Version Numbers

List of Java class file format major version numbers?

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.

how to check the jdk version used to compile a .class file

You're looking for this on the command line (for a class called MyClass):

On Unix/Linux:

javap -verbose MyClass | grep "major"

On Windows:

javap -verbose MyClass | findstr "major"

You want the major version from the results. Here are some example values:

  • 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

error class file has wrong version 55.0, should be 52.0 when building Alfresco

from your stacktrace spring-webscripts-7.9.jar is compiled with jdk
11. so you got this error (class file has wrong version 55.0, should be 52.0).

build version

Solution:
Change spring-webscripts-7.9 to spring-webscripts.7.2 as your system has java 8 version. No need upgrade Java 8 to Java 11.

add the below dependency in your pom.xml file

<!-- https://mvnrepository.com/artifact/org.alfresco.surf/spring-webscripts -->
<dependency>
<groupId>org.alfresco.surf</groupId>
<artifactId>spring-webscripts</artifactId>
<version>7.2</version>
</dependency>

I hope this helps.

find the compiled class version number

You can use javap (with -v for verbose mode), and specify any class from the jar file. For example, looking at a Joda Time jar file:

javap -cp joda-time-2.7.jar -v org.joda.time.LocalDate

Here the -cp argument specifies the jar file to be in the classpath, the -v specifies that we want more verbose information, and then there's the name of one class in the jar file.

The output starts with:

Classfile jar:file:/c:/Users/Jon/Test/joda-time-2.7.jar!/org/joda/time/LocalDate.class
Last modified 12-Jan-2015; size 16535 bytes
MD5 checksum d19ebb51bc5eabecbf225945eccd23ef
Compiled from "LocalDate.java"
public final class org.joda.time.LocalDate extends org.joda.time.base.BaseLocal implements org.joda.time.ReadablePartial,java.io.Serializable
minor version: 0
major version: 49

The "minor version" and "major version" bits are the ones you're interested in.

It's possible that a single jar file contains classes compiled with different versions, of course.

Unsupported class file major version 61

(I incorrectly was zeroing in on the "Unsupported class file major version 61" message without looking at the stacktrace.)

The problem (as pointed out by @Mark Rotteveel) is that glowroot is failing while trying to do some code transformation using ASM. Apparently the ClassReader in the version of ASM that is bundled in glowroot 0.13.6 doesn't understand version 61 (Java 17) class files.

Q: How to solve this?

A: Use glowroot 0.14.0-beta.2 or later; see https://github.com/glowroot/glowroot/issues/906. Alternative, build your application and its dependencies (as required) for an earlier (target) version of Java, and (maybe1) run on an earlier version of Java.


1 - It depends on whether the code transformations involve the ASM ClassReader reading Java SE classes.

What version of javac built my jar?

You can't tell from the JAR file itself, necessarily.

Download a hex editor and open one of the class files inside the JAR and look at byte offsets 4 through 7. The version information is built in.

http://en.wikipedia.org/wiki/Java_class_file

Note: As mentioned in the comment below,

those bytes tell you what version the class has been compiled FOR, not
what version compiled it.

How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version

The version number shown describes the version of the JRE the class file is compatible with.

The reported major numbers are:

Java SE 18 = 62,
Java SE 17 = 61,
Java SE 16 = 60,
Java SE 15 = 59,
Java SE 14 = 58,
Java SE 13 = 57,
Java SE 12 = 56,
Java SE 11 = 55,
Java SE 10 = 54,
Java SE 9 = 53,
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

(Source: Wikipedia)

To fix the actual problem you should try to either run the Java code with a newer version of Java JRE or specify the target parameter to the Java compiler to instruct the compiler to create code compatible with earlier Java versions.

For example, in order to generate class files compatible with Java 1.4, use the following command line:

javac -target 1.4 HelloWorld.java

With newer versions of the Java compiler you are likely to get a warning about the bootstrap class path not being set. More information about this error is available in a blog post New javac warning for setting an older source without bootclasspath.

Class compiled by a more recent version of the Java Runtime (0.0), this version of the Java Runtime only recognizes class file versions up to 52.0

Your statement

return new ClassWriter(classReader,
ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES).toByteArray();

create a new ClassWriter but doesn’t write anything to it before requesting the generated bytecode.

You have to populate it with the class information. In your specific case:

ClassWriter cw = new ClassWriter(classReader,
ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
classNode.accept(cw);
return cw.toByteArray();

Like classReader.accept(classNode, ClassReader.EXPAND_FRAMES); copied all information from the class reader to the classNode, the classNode.accept(cw); will copy the modified information from the classNode to the class writer.

To answer the question literally, a call to
visit​(int version, int access, java.lang.String name, java.lang.String signature, java.lang.String superName, java.lang.String[] interfaces) will specify the main information of the class, including the version number, however, the method will be invoked automatically when using the Visitor Pattern.



Related Topics



Leave a reply



Submit