reading MANIFEST.MF file from jar file using JAVA

前端 未结 8 1351
情深已故
情深已故 2020-12-02 20:34

Is there any way i can read the contents of a jar file. like i want to read the manifest file in order to find the creator of the jar file and version. Is there any way to a

8条回答
  •  忘掉有多难
    2020-12-02 20:47

    Achieve the attributes in this simple way

        public static String  getMainClasFromJarFile(String jarFilePath) throws Exception{
        // Path example: "C:\\Users\\GIGABYTE\\.m2\\repository\\domolin\\DeviceTest\\1.0-SNAPSHOT\\DeviceTest-1.0-SNAPSHOT.jar";
        JarInputStream jarStream = new JarInputStream(new FileInputStream(jarFilePath));
        Manifest mf = jarStream.getManifest();
        Attributes attributes = mf.getMainAttributes();
        // Manifest-Version: 1.0
        // Built-By: GIGABYTE
        // Created-By: Apache Maven 3.0.5
        // Build-Jdk: 1.8.0_144
        // Main-Class: domolin.devicetest.DeviceTest
        String mainClass = attributes.getValue("Main-Class");
        //String mainClass = attributes.getValue("Created-By");
        //  Output: domolin.devicetest.DeviceTest
        return mainClass;
    }
    

提交回复
热议问题