Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get jar version in runtime

Tags:

I'm wondering if it is possible to retrieve, in runtime, a version number of a jar from which the class comes from?

I know its possible to find jar from which the class comes from:

MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath(); 

but what about a version?

(assuming its not in the file name:) )

like image 320
zibi Avatar asked Feb 12 '14 09:02

zibi


People also ask

How do I find the version of a JAR file?

Go to the /opt/novell/sentinel/bin directory. At the command line, specify the ./versionreader.sh <path/jar file name>. Running the script without any arguments gives the version of the installed Sentinel server.

What is Java JAR command?

The jar command is a general-purpose archiving and compression tool, based on the ZIP and ZLIB compression formats. Initially, the jar command was designed to package Java applets (not supported since JDK 11) or applications; however, beginning with JDK 9, users can use the jar command to create modular JARs.

How do I extract a JAR file on Mac?

Press ⌘ Command + V to paste in your JAR file's path, and press ⏎ Return . This executes the command to extract the JAR file. Go back to the JAR file's location.

How do I unpack a JAR file?

To unpackage a JAR, you need a program that can extract compressed files. Windows includes functionality for this, but you can also use file extraction software like 7-Zip or WinRAR to get the job done. Open the JAR file within the software, and you can browse all the folders and files within it.


2 Answers

Try this, it may be helpful:

String s = new String(); System.out.println(s.getClass().getPackage().getSpecificationVersion()); System.out.println(s.getClass().getPackage().getImplementationVersion()); 

Output:

1.7 1.7.0_25 
like image 143
Shekhar Khairnar Avatar answered Oct 03 '22 04:10

Shekhar Khairnar


import javax.mail.internet.InternetAddress;  /** Display package name and version information for javax.mail.internet. */ public final class ReadVersion {   public static void main(String... aArgs){     ReadVersion readVersion = new ReadVersion();     readVersion.readVersionInfoInManifest();   }    public void readVersionInfoInManifest(){     InternetAddress object = new InternetAddress();     Package objPackage = object.getClass().getPackage();     //examine the package object      String name = objPackage.getSpecificationTitle();     String version = objPackage.getSpecificationVersion();     //some jars may use 'Implementation Version' entries in the manifest instead     System.out.println("Package name: " + name);     System.out.println("Package version: " + version);   } } 
like image 27
1ac0 Avatar answered Oct 03 '22 04:10

1ac0