Are there any Java APIs to find out the JDK version a class file is compiled for? Of course there is the javap tool to find out the major version as mentioned in here. However I want to do it programmatically so that that I could warn the user to compile it for the appropriate JDK
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
basszero's approach can be done via the UNIX command line, and the "od(1)" command:
"feca beba" is the magic number. The "0000 3100" is 0x31, which represents J2SE 5.0.
Just read the class file directly. It's VERY easy to figure out the version. Check out the the spec and wiki and then try the code. Wrapping this code and making is more useful/pretty is left as an exercise. Alternatively, you could use a library like BCEL, ASM, or JavaAssist
JavaP does have an API, but it's specific to the Sun JDK.
It's found in
tools.jar
, undersun/tools/javap/Main.class
.Apache BCEL provides this API:
On Linux, you can use the
file
command on a class file, which I found easiest for my use case. For example:As others have shown, it is easy enough to do by reading the first eight bytes of a class file. If you want a pre-built binary library, you can download one here.