Explain Codes LogoExplain Codes Logo

What version of javac built my jar?

java
bytecode
javap
java-versions
Alex KataevbyAlex Kataev·Oct 25, 2024
TLDR

Determine the javac version that constructed a .jar file by extracting its .class files and inspecting the class file's major version number. Run these commands:

# Who doesn't love unzipping stuff? unzip my-app.jar -d extracted/ # Time to Sherlock Holmes this jar! javap -verbose extracted/com/myapp/Main.class | grep -m 1 "major"

What does this major version number tell us? It's a mapping to the Java version:

  • 52 connotes Java 8
  • 55 represents Java 11
  • 59 signifies Java 15

In essence, all we need is the first occurrence to perform our compiler version magic trick.

Quick guide on bytecode skimming

When you invoke the javap -verbose command, it's akin to opening Pandora's box - a treasure trove of information.

  • Class versions for JVM compatibility: The class file version mainly suggests the earliest Java Runtime that can process it - not specifically the javac version.
  • Hex editors - the magnifying glass on bytecode: Dive into details by inspecting byte offsets 4-7 in class files with a hex editor, if you dare 😉.
  • Manifest insights: Peek into the MANIFEST.MF hiding in the META-INF directory. It might contain the JDK version. It's like a letter from the compiler - if you're lucky.
  • Command line tools: Unix systems flaunt their file command. Windows with Cygwin installed has its arsenal too. They're like pocket magnifying glasses for file information.

Deciphering the major version numbers

The major version numbers seem cryptic like a cipher but follow a consistent pattern. Understanding these patterns ensures seamless integration of Java applications on diverse environments. Here's your secret decoder ring:

Major VersionJava Version
506
517
528
......

You can find this Rosetta stone in the official Java class file documentation or a detailed table like the one on Wikipedia.

Unearthing secrets without unboxing

What if the .class file's version needs to be checked without extracting the .jar file? We can equip our command line armor again:

# Is it a bird? Is it a plane? No, it's javap -cp! javap -cp my-app.jar -verbose com.myapp.Main | grep "major"

Replace com.myapp.Main with your specific package and class name. Jane Austen would be proud - so much drama without much fuss.

Toolkit for the modern archaeologist

Feeling intimidated by the jargon or flashy command-line prowess? Fear not. We have a flourishing ecosystem of libraries and tools:

  • Apache Commons BCEL: An analysis weapon to detect javac version without getting your hands dirty.
  • Bytecode Viewer: An advanced Swiss army knife for digging deep into bytecode.
  • Java Decompiler: A handy photon cannon to light up the darkest corners of the .jar file.