Explain Codes LogoExplain Codes Logo

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

java
bytecode-analysis
jdk-versions
javap
Alex KataevbyAlex Kataev·Jan 2, 2025
TLDR

Find out which JDK version compiled a .class file with this command. Be sure to replace 'YourClass' with your actual class name:

javap -v YourClass.class | grep 'major'

Scout for the major version in the output and associate it with a JDK release, such as 52 (Java 8), 58 (Java 14), etc.

An in-depth approach

While javap stands as a magnificent start, there might be instances where a finer bytecode analysis might be needed.

An introduction to advanced tools

If you require a more detailed assembly or are faced with a Jar file, resort to tools such as JARCHECK or jclasslib. With jclasslib's bytecode viewer, which is available both as a standalone application and as an IntelliJ IDEA plugin, bytecode analysis becomes an effortless task.

jclasslib in action

// Trust me, this is easier than it looks. - Fire up jclasslib. - Load your `.class` or `.jar` file. - Go to the **"Version"** section. - Unveil the JDK version information. It's like magic, but real.

Eclipse setups

If you're an Eclipse user, don't forget to cross-verify your compiler compatibility settings. Eclipse has an innate ability to mirror different JDK versions, but it can only perform its magic when set up correctly:

// Like setting your clock to the correct time zone. Simple. - Navigate to **Project** -> **Properties**. - Access the **Java Compiler** section. - Confirm that the **Compiler compliance level** mirrors your target JDK version.

Detecting hidden pitfalls

Beware of silent killers, such as setting the wrong compatibility mode or inadequate change application. These can often lead to silent failures, where Eclipse might not adhere to the defined settings.

Reading between the lines

The bytecode can drop hints — methods, APIs, and language constructs exclusive to certain JDK versions. The invokedynamic usage is a clear indication of Java 7 or above.

Delving deeper

Ensure harmony between JDK & JRE

To safeguard against the unpleasant "Bad version number in .class file," see to it that your JDK and JRE versions echo each other. This ensures your compiled bytecode and the Java runtime gel seamlessly.

Flag -verbose to the rescue

At times, when the class name eludes you or you're just curious about runtime loadings, resort to the -verbose flag using the Java command line:

// If Sherlock Holmes had this, he'd solve cases in no time. java -verbose YourApplication

This flag spits out a verbose log of class loading, including the class file versions.

Tapping into Eclipse community

Seek solace in the Eclipse community forums if you're quivering with JDK compatibility nightmares. You might stumble upon shared resolutions from developers who've been in the same boat before.