- How to Get the Version Number of a JAR File
- Parsing the File Name
- javaxt.io.Jar
- Related Articles
- What version of javac built my jar
- How to know with which Java version Jar is created? [duplicate]
- Using a jar library compiled with a higher version of javac
- Using multiple .jar with javac
- Get jar version in runtime
- I want to find out which version of java the jar file I have is available
- how to know the version of java used to create a jar file
- Determine Which JDK Version a JAR/Class File Was Compiled With
How to Get the Version Number of a JAR File
The preferred mechanism for storing version information is to use the Manifest file. There are two keywords commonly used to store version information in the manifest: «Implementation-Version» and «Bundle-Version». Here’s a simple script you can use to find the version number in the Manifest:
java.io.File file = new java.io.File("/drivers/h2/h2-1.3.162.jar"); 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 it = 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); //"Version: 1.3.162"
Parsing the File Name
If, for whatever reason, the version information is not found in the manifest, you can try to parse the jar file name. This is extremely error prone but sometimes its your only option. Here’s a simple script to extract the version number from the file name.
java.io.File file = new java.io.File("/drivers/h2/h2-1.3.162.jar"); String versionNumber = ""; String fileName = file.getName().substring(0, file.getName().lastIndexOf(".")); if (fileName.contains("."))< String majorVersion = fileName.substring(0, fileName.indexOf(".")); String minorVersion = fileName.substring(fileName.indexOf(".")); int delimiter = majorVersion.lastIndexOf("-"); if (majorVersion.indexOf("_")>delimiter) delimiter = majorVersion.indexOf("_"); majorVersion = majorVersion.substring(delimiter+1, fileName.indexOf(".")); versionNumber = majorVersion + minorVersion; > System.out.println("Version: " + versionNumber); //"Version: 1.3.162"
javaxt.io.Jar
Note that the javaxt.io.Jar class can be used to return the version of a jar file with one simple call. The getVersion() method combines both techniques described here into one method. Example:
System.out.println("Version: " + new javaxt.io.Jar(file).getVersion());
Related Articles
What version of javac built my jar
Solution: An Older Java runtime environment will not understand Java 8 specific features, so running your Java 8 code on Older Java JRE is not going to work. as So in case somebody use your library as dependency for fat jar, it returns the version of Manifest for fat jar.
How to know with which Java version Jar is created? [duplicate]
On Linux, Mac OS X or Windows with Cygwin installed, the file(1) command knows the class version.
$ file ./org/apache/log4j/Appender.class ./org/apache/log4j/Appender.class: compiled Java class data, version 45.3
And if you are relegated to a Windows environment without either file or grep
> javap -v ./org/apache/log4j/Appender.class | findstr major major version: 45
Reference : What version of javac built my jar?
Java — Which version was a .jar file built too?, Most jars are built using the provided «jar» tool packaged with the jdk. In SUN’s JDK the provided «jar» tool will add a «Created-By» line in the embedded META-INF/MANIFEST.MF file. That line will specify the version of the JDK that created the JAR (in my case «Created-By: 1.6.0_17 (Sun Microsystems Inc.)») …
Using a jar library compiled with a higher version of javac
An Older Java runtime environment will not understand Java 8 specific features, so running your Java 8 code on Older Java JRE is not going to work.
If you tried to use the JAR compiled in JAVA 8 in your older JAVA version, then you will come across the «Major-Minor Version» issue.
If the jar was compiled using -target 1.7 (or lower) then the Java 7 JRE will be able to use it, but then the author of the jar cannot use language features that only work on Java 8 or newer.
How to check the version of jar file?, To explain you more, I have import x.y.z, I know that x.y belongs to a-4.1.jar, but the application which i am working has been developed long back, and I dono what version of a.jar file have they used, I am worried about the version because, some of the classes have been depreciated from the jar of the older …
Using multiple .jar with javac
In addition to Romain Muller ‘s answer:
If you want to quickly use all of the *.jar files in the current directory, and you’re using JDK 6 or later, you can use a single-asterisk. In a unix shell (like in Linux), you’ll need to escape the asterisk:
This works when running the Java application as well:
Note the .: to tell Java to look in the current directory as well as the *.jar files to find Canvas.class .
On Windows, use a semicolon ( ; ) instead of a colon as a separator.
As far as I know, the -cp option requires classpath to be specified as a colon or semi-colon-separated list of places in most situations, and not a comma-separated list as your OS seems to derivate when expanding *.jar .
I am not sure why, but for me (on windows 10, java 17), the only thing that worked was:
With semi-colon, and without the \ that exists in @ZoogieZork answer
Why latest jre won’t run jar compiled with older version of, 1 Answer. Sorted by: 0. Blockquote My version of Jre in the other pc is: «1.8.0_321» which is supossed to be the latest version. The 1.8 version (JDK 8) is not the latest. It’s an older release from 2014. JDK 11 is a newer release from 2018. The latest release is JDK 18 from 03.2022. Here you can download the …
Get jar version in runtime
Try this, it may be helpful:
String s = new String(); System.out.println(s.getClass().getPackage().getSpecificationVersion()); System.out.println(s.getClass().getPackage().getImplementationVersion());
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); >>
Be careful using getPackage().getImplementationVersion/getSpecificationVersion()
getSpecificationVersion returns specVersion from Manifest. Manifest is a property of a jar and is used in sun.misc.URLClassPath as
public Manifest getManifest() throws IOException
So in case somebody use your library as dependency for fat jar, it returns the version of Manifest for fat jar.
Java — Using multiple .jar with javac, In addition to Romain Muller ‘s answer: If you want to quickly use all of the *.jar files in the current directory, and you’re using JDK 6 or later, you can use a single-asterisk. In a unix shell (like in Linux), you’ll need to escape the asterisk: javac -cp \* Canvas.java. This works when running the Java …
I want to find out which version of java the jar file I have is available
How can I find out which version of java the jar file I have is available?
To tell the truth, the jar file itself does not have a build version. A jar file is just an archive of class files, properties files and other files. For example, can a jar be a relative of tar or zip? Therefore, to find out if the jar file is available for a particular version of Java, you should not look at the version of the jar file itself, but what version of javac the class file contained in that jar file was built with. I need to take a look.
Here, commons-collections4-4.4.jar (Apache Commons Collections v.4.4) is available from which version as an example of the investigation method. I will investigate whether it is.
First, use the jar command to extract the contents of the jar file.
$ jar xf commons-collections4-4.4.jar
You should find multiple class files stored in the jar file. Here, we will use ./org/apache/commons/collections4/ArrayStack.class as a sample.
$ find . -name "*.class" | head -n1 ./org/apache/commons/collections4/ArrayStack.class
I’d like to find out which version of javac ./org/apache/commons/collections4/ArrayStack.class was built with, but the quickest way is to use the file command. In this case, it is obvious that it is built with Java 8.
$ file ./org/apache/commons/collections4/ArrayStack.class ./org/apache/commons/collections4/ArrayStack.class: compiled Java class data, version 52.0 (Java 1.8)
Use javap in an environment without the file command.
$ javap -v ./org/apache/commons/collections4/ArrayStack.class | grep major major version: 52
how to know the version of java used to create a jar file
posted 14 years ago
How to know the version of the java used to create a jar file? I know that MANIFEST.MF may contain Created-By field which may tell you. But this is not true all the time. Sometimes Created-By field is not there are sometimes it has some arbitrary value. So anybody has any other suggestion?
posted 14 years ago
I don’t think you can determine which Java version that was used to create the jar file, just like you can’t determine which Java version was used to compile a class. Why is this information important?
Are you maybe interested in which Java version is needed to run the jar file? That’s a different question, though.
posted 14 years ago
Open the jar file and check the Manifest file:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.4.2_12-b03 (Sun Microsystems Inc.)
The last line say java version.
posted 14 years ago
Yes that is exactly what I want to know. Which version of java is required to run a jar? Is there any way to know this?
Thanks and Regards,
Litty Preeth
posted 14 years ago
- 1
A good indication is the class file version. That is contained in the 7th and 8th byte of a class file. (See http://en.wikipedia.org/wiki/Class_(file_format)#General_layout for details.) So you can unjar the jar file, take one of its class file and examine its 7th and 8th byte. This could also be done programmatically using the java.util.jar classes.
posted 11 years ago
Ulf Dittmer wrote: A good indication is the class file version. That is contained in the 7th and 8th byte of a class file. (See http://en.wikipedia.org/wiki/Class_(file_format)#General_layout for details.) So you can unjar the jar file, take one of its class file and examine its 7th and 8th byte. This could also be done programmatically using the java.util.jar classes.
Your response helped me a lot. Thanks Ulf.
Determine Which JDK Version a JAR/Class File Was Compiled With
Today I came across a nasty error which occurred in a deployed Java application only but not during development or integration tests. The error went something like the following:
java.lang.NoSuchMethodError: java.nio.ByteBuffer.rewind()Ljava/nio/ByteBuffer; at nx.serializerkryo.internal.InternalKryoSerialzer.performToStream(InternalKryoSerialzer.java:33) at nx.serializerkryo.internal.InternalKryoSerialzer.serialize(InternalKryoSerialzer.java:63) at nx.serializerkryo.internal.InternalKryoSerialzer.serialize(InternalKryoSerialzer.java:21) at nx.persistence.jre.internal.OptimizedPersistedNodeSerializer.serialize(OptimizedPersistedNodeSerializer.java:47) at nx.persistence.jre.internal.OptimizedPe rsistedNodeSerializer.serialize(OptimizedPersistedNodeSerializer.java:21)
Now I had a feeling that this had something to do with me trying to be ahead of the curve and use a Java 9 JDK to compile the application. In order to debug this, I had to confirm which with JDK the classes I was using were compiled with. Thankfully I found a handy thread of StackOverflow.
Unfortunately, it wasn’t immediately obvious to me which solution listed there would work best, so I decided to provide the solution here in a more condensed form. Simply use the following command:
javap -v [path to your class file]
The output will then contain the following line (towards the top of the file):
public class . minor version: 0 major version: 50 flags: ACC_PUBLIC, ACC_SUPER
The major version and minor version indicates which version of Java the class was compiled with. The following contains a list of which Java versions which major versions relate to.
Java SE 9 = 53 (0x35 hex), Java SE 8 = 52 (0x34 hex), Java SE 7 = 51 (0x33 hex), Java SE 6.0 = 50 (0x32 hex), Java SE 5.0 = 49 (0x31 hex), JDK 1.4 = 48 (0x30 hex), JDK 1.3 = 47 (0x2F hex), JDK 1.2 = 46 (0x2E hex), JDK 1.1 = 45 (0x2D hex).
Interestingly my files were apparently compiled for Java 6 (Maven compiler plugin was responsible). The problem was that the files were compiled with JDK 9 (though they were compiled for 1.6). Downgrading the JDK used to do the compilation to JDK8 fixed the problem.
Insights for developing lean applications with ease 😎 and my musings on life and leadership ✍.