Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get java class version from javac or JRE without a compiled class

Java class version can be obtained from a class binary using javap

javap -verbose Test | grep 'minor\|major'
  minor version: 0
  major version: 55

Is it possible to know in advance which class version a JDK will produce? java -version does not show it

java -version
openjdk version "11.0.17" 2022-10-18

Usually related to java.lang.UnsupportedClassVersionError.

like image 494
LMC Avatar asked Feb 22 '26 12:02

LMC


1 Answers

Java class major/minor version produced by a JDK can be obtained from java, javac or javap commands as follows

java -XshowSettings:properties -version 2>&1 | grep -E -e 'java\.(class\.)?version '
javac -J-XshowSettings:properties -version 2>&1 | grep -E -e 'java\.(class\.)?version '

# testing all 3 commands
for cmd in "java " "javac -J" "javap -J"; do echo "${cmd% *}"; ${cmd}-XshowSettings:properties -version 2>&1  | grep -E -e 'java\.(class\.)?version ' ; done

Result:

java 
    java.class.version = 61.0
    java.version = 17.0.8.1
javac
    java.class.version = 55.0
    java.version = 11.0.20.1
javap
    java.class.version = 55.0
    java.version = 11.0.20.1

Note: As in my case :p , a Linux misconfiguration could give different results from java and javac showing that both commands belong to different versions. Than can be usually fixed using alternatives command.

like image 66
LMC Avatar answered Feb 24 '26 00:02

LMC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!