This question already has answers here:
Reading my own Jar's Manifest
(13 answers)
Closed 8 years ago.
I'm trying to access the MANIFEST.MF file in my java application.
I'm doing the following
//prints /usr/local/apache-tomcat-8.0.9/webapps/MyProject/WEB-INF/lib/activation-1.1.jar!/META-INF/MANIFEST.MF
System.out.println( "We are looking at "+this.getClass().getClassLoader()
.getResource("META-INF/MANIFEST.MF"));`
//Im looking to print /usr/local/apache-tomcat-8.0.9/webapps/MyProject/META-INF/MANIFEST.MF
//prints-/usr/local/apache-tomcat-8.0.9/lib/
System.out.println("The base path is"+this.getClass().getClassLoader()
.getResource("."));
Here's the directory structure
MyProject
-src/main/java/my/packages/MyClass.java
-target
-MyProject.war
Any thoughts on how I can correct my code would be great.
Directory structure of my war
Myproject
META-INF apidocs other folders
WEB-INF
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
This question already has answers here:
Including dependencies in a jar with Maven
(16 answers)
Closed 5 years ago.
I'm trying to develop a plugin for an FOSS application written by someone else. All I need to do is take a single class I've written, package it in a JAR file, and copy the JAR file to a directory in a pre-existing installation of the application.
When the application sees my JAR file, it should load it on startup.
Problem is, it doesn't seem to be able to load my JAR file.
According to their docs, my manifest may need a CLASSPATH specified.
My JAR file structure is simply: MyJarFile.jar/MyClass.java
It's literally just a JAR file with a single JAVA class file inside it.
I'm new at this, and all the manifest file examples I'm seeing on Google seem to reference other JAR files.
Do I even need to have a CLASSPATH in the manifest?
If so, how do I reference MyClass.java?
I'm using IntelliJ and Maven (for the first time).
Thanks.
Check out the article "Setting an Application's Entry Point" in the java docs:
https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
You need to add a line to the manifest like "Main-Class: MyClass" if you haven't already in order to run the jar.
Do you need to reference classes from another JAR from within your JAR?
If so, you will need to add to the JAR's classpath. See https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html for how to do that.
In general, I would suggest reading the java docs info on JARs
https://docs.oracle.com/javase/tutorial/deployment/jar/index.html
If you still need help after reading that, please explain your specific use case in more detail, e.g. share actual code or error messages
Try this: Click "File" -> "Project structure" -> "Artifacts" -> right click your jar file and choose "Put into output root"
This question already has answers here:
getResourceAsStream() vs FileInputStream
(6 answers)
Closed 7 years ago.
I am currently writing a servlet based application (the client side). I tried to get a text file inside the same package where the code is located. All of the methods that I have come across used either MyClass.class.getResourceAsStream("Words.txt") or classLoader.getResourceAsStream("Words.txt") to get the text file (eg: SO1, SO2). But I have tried FileInputStream("./src/package/Words.txt") and the text file can still be successfully loaded.
What are the differences? And why is the method getResourceAsStream encouraged?
At the moment, you're on your developer workstation, and are probably running your app from your IDE. Tomcat happens to be started from the IDE project root directory, and thus using
new FileInputStream("./src/package/Words.txt")
allows reading the file stored in your project src directory.
But that's not how the project will be run in production. In production, you'll have a Tomcat server started from a totally different directory, using a shell script. And the production server won't have the source project at all. All it will have is Tomcat, and the war file constituting the artifact built from the project.
So there will be no src directory at all, and the file Words.txt won't even be anywhere on the file system. It will only be en entry of the war file (which is in fact a zip file), located under WEB-INF/classes/package along with the .class files produced by the compiler from your Java source files.
So, in order to be able to read that "file", you can't use file IO: the "file" doesn't exist in the file system. You need to use the ClassLoader that will locate the "file" inside the war file and load it from there.
That will also go fine during development, when the app is run from an exploded war structure: the class loader will find the class under the target directory used by your IDE to store the class files and resource files.
Note that what you need to load that resource, if it's in the package com.foo and MyClass is in that same package, is
MyClass.class.getResourceAsStream("Words.txt")
or
AnyOtherOfYourClassesWhateverThePackageIs.class.getResourceAsStream("/com/foo/Words.txt")
or
classLoader.getResourceAsStream("com/foo/Words.txt")
This question already has answers here:
What's in an Eclipse .classpath/.project file?
(4 answers)
Closed 8 years ago.
I have some doubts regarding the files .classpath and .project. I'm using eclipse IDE. I created a project. In that project is see that two files are created. They are
.classpath
.project.
Following questions I have:
Can I know what are the main differences between these two files(.classpath & .project)?
What is the main purpose of .project file?
Can i use .project file in place of .classpath file?
Whether .project file is created in all java IDE's?
Any help is highly appreciated
Most Java IDEs have their own method of storing metadata they need to assist you with your project. Eclipse uses the files you mention. IDEA uses a different file (or a directory if you choose that option).
You generally shouldn't edit these files directly unless you are trying to do something tricky - they are really there for the IDE, not for you, and are not required if you compile your project using an external tool such as javac, ant or maven.
As #geoand mentioned, these files are usually not added to source control as they generally relate to configuration specific to your machine (such as directory structure, etc) and they can be regenerated if required.
This question already has answers here:
How can I use external JARs in an Android project?
(12 answers)
Closed 9 years ago.
I am new in using jar files in Android application. While checking code for Android apps code.google.com suggested the jar files. I don't know how to use the classes from jar files. Please help me!
Thanks
Just put the jar in the libs folder of the project, then it will automatically be added to your project by Android SDK.
Now the code from the jar is accessible in your code.
If you added a jar containing the package org.jartest with the class IAmAnObject. You should be able to write IAmAnObject test = new IAmAnObject(), right click IAmAnObject in eclipse and select import org.jartest.IAmAnObject.
Create a folder that is called /libs at the root directory of your project, and put your jar files in there.
I don't know about eclipse.
The way you use a external library in an android project is by creating a 'libs' folder in the projects source folder and putting the jar files in their.
Don't forget to add the path of libs to the build path so they are available when building the final project (in eclipse it's right click the jar and select 'add to build path'
It has already been answered and a definite duplicate but I cannot seem to paste the answer URL.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Problem locating file in a classpath
Where should the configuration files like xml file should be placed in the java dynamic web project in the case, where the external jar file in the lib folder should read the configuration file at runtime.
The bottom line is that these files must arrive to you application classpath. In case of web probject WEB-INF/classes is the best location. (as already mentioned by #Easwaramoorthy Kanaga).
The question is where to put the files in project.
If you are using maven it almost dictates the location src/main/resources. If you are not with maven put them to resources directory anyway. The resources directory should be defined on the same level as src directory. For example if you have src directory under your project root create resources directory at the same place. Configure your IDE to copy these files to target directory. Update your build script to do the same.
You can place it in the WEB-INF/classes folder. Even you can place a folder inside the classes folder which will work perfectly.
One more thing, The jar file should read the config file with a hardcoded path, instead it should do like this.
String xmlFilePath = "configfile.xml";
InputStream file = this.getClass().getClassLoader().getResourceAsStream(xmlFilePath);