package de.tutorials;
import java.io.DataInputStream;
import java.io.File;
import java.util.Collections;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class JarFileInfo {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
JarFile jarFile = new JarFile(new File("/home/tom/Desktop/eclipse 3.5 Galileo/plugins/org.junit4_4.5.0.v20090423/junit.jar"));
for (JarEntry entry : Collections.list(jarFile.entries())){
if(!entry.isDirectory()){
boolean isCompressedEntry = entry.getSize() != entry.getCompressedSize();
JavaVersion version = null;
if(entry.getName().endsWith(".class")){
version = inspectClassVersion(entry,jarFile);
}
System.out.println(entry +" " + isCompressedEntry + " " + version);
}
}
}
private static JavaVersion inspectClassVersion(JarEntry entry, JarFile jarFile) throws Exception {
DataInputStream dataInputStream = new DataInputStream(jarFile.getInputStream(entry));
dataInputStream.skip(6);
short majorVersionNumber = dataInputStream.readShort();
dataInputStream.close();
return JavaVersion.valueOf(majorVersionNumber);
}
enum JavaVersion{
Java14(48),
Java15(49),
Java16(50);
int majorVersionNumber;
private JavaVersion(int majorVersionNumber){
this.majorVersionNumber = majorVersionNumber;
}
/*
http://en.wikipedia.org/wiki/Class_(file_format)
J2SE 6.0 = 50 (0x32 hex),
J2SE 5.0 = 49 (0x31 hex),
JDK 1.4 = 48 (0x30 hex),
JDK 1.3 = 47 (0x2F hex),
JDK 1.2 = 46 (0x2E hex),
JDK 1.1 = 45 (0x2D hex).
*/
public static JavaVersion valueOf(int majorVersionNumber){
for(JavaVersion version : values()){
if(majorVersionNumber == version.majorVersionNumber){
return version;
}
}
throw new IllegalArgumentException("Unknown Java Version with major number" + majorVersionNumber);
}
}
}