This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

finding javac version used to compile a class

 
peter cooke
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have several provided 3rd party jar file. One class is throwing a java.lang.UnsupportedClassVersionError "Unsupported major.minor v" (sorry not permitted to post actual exception). I have only seen this when a class is compiled in a newer jdk than what is running the class.

I need to know what version of javac compiled the classes them.

javap tells me methods and variables but does not tell me what the compiler version was.

Thoughts suggestions?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a Java class file, the first 4 bytes are the magic number (0xCAFEBABE). The next two bytes are the respective major and minor versions.

See this page for an example of how to read these...

http://www.rgagnon.com/javadetails/java-0544.html
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or just javap -verbose; the class version info is at the top.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be picky: There's no way to find out the version of the Java compiler used to compile a class (which is what you're asking). The best you can do is find out the Java version for which the code has been compiled, which is not necessarily the same, but which is usually what you're interested in. Just a little something to keep in mind; check out the documentation of the "-source" and "-target" switches of the "javac" executable for more detail.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic