Java API to find out the JDK version a class file is compiled for?

Viewed 33076

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

9 Answers

I use this script in a batch file on windows, redirect the output and grep for the major number I am verifying for:

for /R %%f in (*.class) do (
"C:\Program Files\Java\JDK8\bin\javap" -v "%%f" | findstr major )

I run it as such:

verify.bat > versionResults.txt
Related