javajavacunsupported-class-version

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.


Solution

  • 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.