reading MANIFEST.MF file from jar file using JAVA

前端 未结 8 1357
情深已故
情深已故 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 21:14

    1. read version;
    2. we copied MANIFEST.MF from jar to user home.
        public void processManifestFile() {
            String version = this.getClass().getPackage().getImplementationVersion();
            LOG.info("Version: {}", version);
            Path targetFile = Paths.get(System.getProperty("user.home"), "my-project", "MANIFEST.MF");
    
            try {
                URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation();
                JarFile jarFile = new JarFile(url.getFile());
                Manifest manifest = jarFile.getManifest();
                try(FileWriter fw = new FileWriter(targetFile.toFile())) {
                    manifest.getMainAttributes().entrySet().stream().forEach( x -> {
                        try {
                            fw.write(x.getKey() + ": " + x.getValue() + "\n");
                            LOG.info("{}: {}", x.getKey(), x.getValue());
                        } catch (IOException e) {
                            LOG.error("error in write manifest, {}", e.getMessage());
                        }
                    });
                }
                LOG.info("Copy MANIFEST.MF to {}", targetFile);
            } catch (Exception e) {
                LOG.error("Error in processing MANIFEST.MF file", e);
            }
        }
    

提交回复
热议问题