How to know the compiled version of jar file or class file?
How to know the compiled version of jar file or class file?
To know the complied version of the jar file or class file below is the code.
You have to run the MainClass and pass the file name in the argument. You have to put jar or class file in same path of MainClass file.
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Logger;
public class CheckVersionUtil
{
private static final Logger LOGGER = Logger.getLogger(CheckVersionUtil.class.getSimpleName());
public static void copyToTemp(String destinationDir, String srcFile)
throws IOException
{
LOGGER.info("Inside copyToTemp()");
Path dest = Paths.get(destinationDir, new String[0]);
Path src = Paths.get(srcFile, new String[0]);
LOGGER.info("Destination file path = " + destinationDir);
LOGGER.info("Source file path = " + srcFile);
Path p = Files.copy(src, dest, new CopyOption[] { StandardCopyOption.REPLACE_EXISTING });
LOGGER.info("Copied file name to temp folder = " + p.getFileName());
LOGGER.info("Exiting copyToTemp()");
}
public static String excractjarFile(String jarFile, String destdir)
throws IOException
{
LOGGER.info("Inside excractjarFile()");
String oneClassFilePath = null;
JarFile jarfile = new JarFile(jarFile);
Enumeration enu = jarfile.entries();
boolean isClassFile = false;
while (enu.hasMoreElements())
{
JarEntry je = (JarEntry)enu.nextElement();
// LOGGER.info("Next Element is " + je.getName());
File fl = new File(destdir, je.getName());
if ((fl.getName().contains(".class")) && (oneClassFilePath == null))
{
oneClassFilePath = fl.getAbsolutePath();
isClassFile = true;
}
if (!fl.exists())
{
fl.getParentFile().mkdirs();
fl = new File(destdir, je.getName());
}
if (je.isDirectory())
{
// LOGGER.info(je.getName() + " is directory. so, will continue.");
}
else
{
InputStream is = jarfile.getInputStream(je);
FileOutputStream fo = new FileOutputStream(fl);
while (is.available() > 0) {
fo.write(is.read());
}
fo.close();
is.close();
if (isClassFile)
{
// LOGGER.info("isClassFile is true so, first class file is found.");
break;
}
}
}
//LOGGER.info("Exiting excractjarFile() with =" + oneClassFilePath);
return oneClassFilePath;
}
public static String getVersionDetails(String filename)
throws IOException
{
LOGGER.info("Inside getVersionDetails()");
LOGGER.info("filename = " + filename);
String version = "";
DataInputStream stream = new DataInputStream(new FileInputStream(filename));
int magicBytes = stream.readInt();
if (magicBytes != -889275714)
{
LOGGER.info(filename + " is not a valid java file!");
}
else
{
int minorVersion = stream.readUnsignedShort();
int majorVersion = stream.readUnsignedShort();
LOGGER.info("minorVersion = " + minorVersion + " ,majorVersion" + majorVersion);
version = majorVersion + "." + minorVersion;
}
stream.close();
LOGGER.info("Exiting getVersionDetails() with version = " + version);
return version;
}
public static String getVersion(String version)
{
LOGGER.info("Inside getVersion()");
String javaVersion = null;
if (version.equals(JavaMinorMajorVersion.JDK_VERSION_8.getMajorMinorVersion())) {
javaVersion = JavaMinorMajorVersion.JDK_VERSION_8.getJavaVersion();
} else if (version.equals(JavaMinorMajorVersion.JDK_VERSION_7.getMajorMinorVersion())) {
javaVersion = JavaMinorMajorVersion.JDK_VERSION_7.getJavaVersion();
} else if (version.equals(JavaMinorMajorVersion.JDK_VERSION_6.getMajorMinorVersion())) {
javaVersion = JavaMinorMajorVersion.JDK_VERSION_6.getJavaVersion();
} else if (version.equals(JavaMinorMajorVersion.JDK_VERSION_5.getMajorMinorVersion())) {
javaVersion = JavaMinorMajorVersion.JDK_VERSION_5.getJavaVersion();
} else if (version.equals(JavaMinorMajorVersion.JDK_VERSION_4.getMajorMinorVersion())) {
javaVersion = JavaMinorMajorVersion.JDK_VERSION_4.getJavaVersion();
} else if (version.equals(JavaMinorMajorVersion.JDK_VERSION_3.getMajorMinorVersion())) {
javaVersion = JavaMinorMajorVersion.JDK_VERSION_3.getJavaVersion();
} else if (version.equals(JavaMinorMajorVersion.JDK_VERSION_2.getMajorMinorVersion())) {
javaVersion = JavaMinorMajorVersion.JDK_VERSION_2.getJavaVersion();
} else if (version.equals(JavaMinorMajorVersion.JDK_VERSION_1.getMajorMinorVersion())) {
javaVersion = JavaMinorMajorVersion.JDK_VERSION_1.getJavaVersion();
} else {
javaVersion = JavaMinorMajorVersion.JDK_VERSION_0.getJavaVersion();
}
LOGGER.info("Exiting with javaVersion = " + javaVersion);
return javaVersion;
}
}
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
public class MainClass {
private static final Logger LOGGER = Logger.getLogger(CheckVersionUtil.class.getSimpleName());
public static void main(String[] args) {
File file = new File(args[0]);
try
{
if (file.getName().endsWith(".jar"))
{
String temp = System.getProperty("java.io.tmpdir");
StringBuilder destFile = new StringBuilder();
destFile.append(temp);
destFile.append(file.getName());
CheckVersionUtil.copyToTemp(destFile.toString(), file.getAbsolutePath());
File jarFile = new File(destFile.toString().substring(0, destFile.toString().lastIndexOf(".")));
boolean isCreated = jarFile.mkdir();
LOGGER.info("isCreated = " + isCreated);
String oneOfClassFile = CheckVersionUtil.excractjarFile(destFile.toString(),
jarFile.getAbsolutePath());
String compiledVersion = CheckVersionUtil.getVersionDetails(oneOfClassFile);
String version = CheckVersionUtil.getVersion(compiledVersion);
LOGGER.info("compiledVersion = " + version);
LOGGER.info("File Name : "+file.getName());
LOGGER.info("Version = "+version);
}
else if (file.getName().endsWith(".class"))
{
String compiledVersion = CheckVersionUtil.getVersionDetails(file.getAbsolutePath());
String version = CheckVersionUtil.getVersion(compiledVersion);
LOGGER.info("compiledVersion = " + version);
LOGGER.info("File Name : "+file.getName());
LOGGER.info("Version = "+version);
}
}
catch (IOException e)
{
e.printStackTrace();
LOGGER.info("IOException : ");
}
}
}
import java.util.logging.Logger;
public enum JavaMinorMajorVersion
{
JDK_VERSION_8("JDK 1.8", "52.0"), JDK_VERSION_7("JDK 1.7", "51.0"), JDK_VERSION_6("JDK 1.6", "50.0"), JDK_VERSION_5("JDK 1.5", "49.0"), JDK_VERSION_4("JDK 1.4", "48.0"), JDK_VERSION_3("JDK 1.3", "47.0"), JDK_VERSION_2("JDK 1.2", "46.0"), JDK_VERSION_1("JDK 1.1", "45.3"), JDK_VERSION_0("JDK 1.0", "45.3");
private static final Logger LOGGER = Logger.getLogger(JavaMinorMajorVersion.class.getSimpleName());
private String javaVersion;
private String majorMinorVersion;
private JavaMinorMajorVersion(String javaVersion, String majorMinorVersion)
{
this.javaVersion = javaVersion;
this.majorMinorVersion = majorMinorVersion;
}
public String getJavaVersion()
{
LOGGER.info("returning javaVersion = " + this.javaVersion);
return this.javaVersion;
}
public String getMajorMinorVersion()
{
LOGGER.info("returning majorMinorVersion = " + this.majorMinorVersion);
return this.majorMinorVersion;
}
}
Comments
Post a Comment