Search code examples
javajarmanifest.mfcompilation-time

Get JAR compilation time


I am trying to get the compilation time of the runnable JAR file I am exporting from Eclipse. One way to do this would probably be to get the modification time of the META-INF/MANIFEST.MF file. Unfortunately I can't seem to find a way to get this information (I know how to read the manifest itself using getResourceAsStream("/META-INF/MANIFEST.MF"), but I can't seem to be able to read its modification time).

Has anybody some insight on how to do it?


Solution

  • In the end, based on Aleks G's answer and others found elsewhere, I came up with a more robust solution (that e.g. works also on network shares):

    public static Long getTime(Class<?> cl) {
        try {
            String rn = cl.getName().replace('.', '/') + ".class";
            JarURLConnection j = (JarURLConnection) cl.getClassLoader().getResource(rn).openConnection();
            return j.getJarFile().getEntry("META-INF/MANIFEST.MF").getTime();
        } catch (Exception e) {
            return null;
        }
    }
    

    I was hoping for a better way to go from the Class object to the resource name but I guess this will have to do.