java - Get a JAR File version number -
java - Get a JAR File version number -
i have application used in clustering maintain available if 1 or more failed , want implement way check version of jar file in java.
i have piece of code (e.g.: in class myclass) :
urlclassloader cl = (urlclassloader) (new myclass ()) .getclass().getclassloader(); url url = cl.findresource("meta-inf/manifest.mf"); manifest manifest = new manifest(url.openstream()); attributes = manifest.getmainattributes(); string version = attributes.getvalue("implementation-version");
when run jar application works fine when utilize jar file librairie in other application, version number of other application.
so question is, how can manifest of jar myclass contained ?
nb : i'm not interested in solution using static constraint 'classloader.getressource("myjar.jar")' or file("myjar.jar")
you can code :
java.io.file file = new java.io.file("/packages/file.jar"); //give path , file name java.util.jar.jarfile jar = new java.util.jar.jarfile(file); java.util.jar.manifest manifest = jar.getmanifest(); string versionnumber = ""; java.util.jar.attributes attributes = manifest.getmainattributes(); if (attributes!=null){ java.util.iterator = attributes.keyset().iterator(); while (it.hasnext()){ java.util.jar.attributes.name key = (java.util.jar.attributes.name) it.next(); string keyword = key.tostring(); if (keyword.equals("implementation-version") || keyword.equals("bundle-version")){ versionnumber = (string) attributes.get(key); break; } } } jar.close(); system.out.println("version: " + versionnumber); //"here print version"
see this tutorial, give thanks you. larn new thing here.
java jar version manifest
Comments
Post a Comment