Get java class version from javac or JRE without a compiled class
Asked Answered
I

1

1

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.

Iceman answered 26/1, 2023 at 13:57 Comment(2)
You can also just refer to official documentation or WikipediaWilonah
Yes, thanks. I found it interesting to know it from command line for scripting/troubleshooting purposes.Iceman
I
9

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.

Iceman answered 26/1, 2023 at 13:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.