Using a manifest file as input in a webapplication
You want to load a file that is bundled with your web application application. This file will be part of the war file of your deployed app. It thus won't be on the file system. So loading it with a FileInputStream is not the right solution.
BTW, file paths are not relative to the class creating the FileInputStream. They're relative to the directory from which the application server is launched.
The way to load webapp resources is to use the ServletContext.getResourceAsStream()
method. Read its javadoc (and the javadoc of ServletContext.getResource()
) carefully.
You should also realize that WebContent
is the name of the directory where the sources of your webapp are. Once packaged and deployed on a server, there won't be any WebContent directory anymore.
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.
Reading own MANIFEST.MF in Java servlet
I also tried
this.getServletContext().getResourceAsStream("META-INF/MANIFEST.MF");
but it returns a null.
That path must start with /
in order to represent an absolute WAR resource path.
this.getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
Using ClassLoader#getResourceXxx()
doesn't make sense as WAR's own manifest file isn't located in classpath. It's located in webroot, next to /WEB-INF
and all. Therefore, ServletContext#getResourceXxx()
is the only way.
How to read MANIFEST.MF inside WAR application?
How can I find its file name?
You already have it. Maybe you meant to find the absolute file location? You can use ServletContext#getRealPath()
for this.
String relativeWARPath = "/META-INF/MANIFEST.MF";
String absoluteDiskPath = getServletContext().getRealPath(relativeWARPath);
File file = new File(absoluteDiskPath);
// ...
Or if you want to get it as InputStream
directly, use ServletContext#getResourceAsStream()
.
InputStream input = getServletContext().getResourceAsStream("/META-INF/MANIFEST.MF");
// ...
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
}
Why am I getting the wrong MANIFEST?
You need to use the ServletContext.getResourceAsStream
I have found the classloader method not as reliable for MANIFEST files on web apps.
Also it's not going to reliably work well if your running your app from maven or eclipse wtp if your manifest is generated by your build process.
The real duplicate of the question: How do I read the manifest file for a webapp running in apache tomcat?
Related Topics
Gradle: Could Not Determine Java Version from '11.0.2'
Spring-Batch Without Persisting Metadata to Database
Select an Option from the Right-Click Menu in Selenium Webdriver - Java
CSV Parsing in Java - Working Example
Dependency Injection in Struts2 Accessing Session Scoped Beans
Choose Between Executorservice's Submit and Executorservice's Execute
How to Call Some Blocking Method with a Timeout in Java
Using Powermockito.Whennew() Is Not Getting Mocked and Original Method Is Called
Method to Dynamically Load Java Class Files
Merge (Concat) Multiple JSONobjects in Java
Final Keyword in Method Parameters
Java Jtextfield with Input Hint
In What Order Are Static Blocks and Static Variables in a Class Executed
How to Get My Maven Integration Tests to Run