Reading my own Jar's Manifest
You can do one of two things:
Call
getResources()
and iterate through the returned collection of URLs, reading them as manifests until you find yours:Enumeration<URL> resources = getClass().getClassLoader()
.getResources("META-INF/MANIFEST.MF");
while (resources.hasMoreElements()) {
try {
Manifest manifest = new Manifest(resources.nextElement().openStream());
// check that this is your manifest and do what you need or get the next one
...
} catch (IOException E) {
// handle
}
}You can try checking whether
getClass().getClassLoader()
is an instance ofjava.net.URLClassLoader
. Majority of Sun classloaders are, includingAppletClassLoader
.
You can then cast it and callfindResource()
which has been known - for applets, at least - to return the needed manifest directly:URLClassLoader cl = (URLClassLoader) getClass().getClassLoader();
try {
URL url = cl.findResource("META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(url.openStream());
// do stuff with it
...
} catch (IOException E) {
// handle
}
reading MANIFEST.MF file from jar file using JAVA
Next code should help:
JarInputStream jarStream = new JarInputStream(stream);
Manifest mf = jarStream.getManifest();
Exception handling is left for you :)
Using Java to read a .jar manifest file
The problem with using the jar
tool is that it requires the full JDK to be installed. Many users of Java will only have the JRE installed, which does not include jar
.
Also, jar
would have to be on the user's PATH.
So instead I would recommend using the proper API, like this:
Manifest m = new JarFile("anyjar.jar").getManifest();
That should actually be easier!
Read META-INF/MANIFEST.MF from JAR
Depending on the class loader and the order of the JARs on the class path, this is just normal behavior. As there are multiple instances of /META-INF/MANIFEST.MF
, which one you actually get is dependent on the class loader (that loaded the current class, as returned by getClass()
in your code).
Instead, you can use ClassLoader.getResouces(String)
, which will give you an enumeration of all the manifests:
Enumeration<URL> manifests = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
From that enumeration, you can look for the right one.
Another option, is using the Package
class, which will give you easy access to the information from the manifest for your class/package:
Package myPackage = getClass().getPackage();
myPackage.getImplementationTitle();
myPackage.getImplementationVersion();
myPackage.getImplementationVendor();
Programmatically read Manifest.MF from JAR in an EAR
I manage to solve my problem by using this code:
JarFile myEAR = new JarFile(urlOfMyEAR);
File f = File.createTempFile("jar", null);
FileOutputStream resourceOS = new FileOutputStream(f);
byte[] byteArray = new byte[1024];
int i;
InputStream jarIS = myEAR.getInputStream(myEAR.getEntry(libname));
while ((i = jarIS.read(byteArray)) > 0) {
//Write the bytes to the output stream
resourceOS.write(byteArray, 0, i);
}
//Close streams to prevent errors
jarIS.close();
resourceOS.close();
JarFile myJAR = new JarFile(f);
Attributes m = myJAR.getManifest().getMainAttributes();
System.out.println(m.getValue("Implementation-Version"));
(thanks to this post https://stackoverflow.com/a/17902842/2454970)
the whole uncompressing takes only 7 seconds for a 7MB jar in a nearly 200MB ear...
thanks for your thoughts, guys.
Reading Manifest File Inside of Jar with Servlet
The Servlet context is /BotServlet/
not into the /BotServlet/WEB-INF/lib/Server_BotServlet.jar
file. You could put a file you want to read in /WEB-INF/lib/myfile.txt
and access that - or you could open the jar (/BotServlet/WEB-INF/lib/Server_BotServlet.jar
) and read your MANIFEST. Really it depends on what your ultimate objectives are.
Read the jar version for a class
It seems I found some suitable solution here:
https://stackoverflow.com/a/37325538/4222206
So in the end this code can display the correct version of the jar (at least) in JBoss:
this.getClass().getPackage().getImplementationTitle();
this.getClass().getPackage().getImplementationVersion();
Hopefully I will find this answer when I search next time...
Related Topics
Delete Directories Recursively in Java
Eventlistenerlist Firing Order
Seeking Useful Eclipse Java Code Templates
Get Query from Java.Sql.Preparedstatement
Xml Configuration Versus Annotation Based Configuration
Polymorphism: Why Use "List List = New Arraylist" Instead of "Arraylist List = New Arraylist"
How to Create an .Exe for a Java Program
How to Load a Java Class Dynamically on Android/Dalvik
Eclipse 2021-09 Code Completion Not Showing All Methods and Classes
How to Get Java 11 Run-Time Environment Working Since There Is No More Jre 11 for Download
Where Is Java_Home on MACos Mojave (10.14) to Lion (10.7)
How to Use Raw SQL Within a Spring Repository
Practical Uses for Atomicinteger
Determine File Creation Date in Java
How to Access a Value Defined in the Application.Properties File in Spring Boot