I have a .war file. Inside this .war file I have WEB-INF folder. Inside WEB-INF I have a lib folder and inside this lib folder I have a .jar file. Inside this jar I have a class with main method. I need to call this method. Can I do this from command line? I don't have an option of deplying war file, so I need a command which will access my class with main method through the war file.
Easiest way to do this is to go to the location where your jar file is located from command prompt and type java youpackagename.yourclassname .for eg java net.grinder.Grinder
Accessing the jar in the war package might be difficult. Due to the fact that war packaging is intended for deployment on a Java Web server, the war packaging is obsolete and not handy for you usecase. Unzip the war archive and run the contained jar archive after unpacking like you'd run any other jar archive with java -jar /path/to/archive.jar (you'll find a ton of examples on this page).
Related
I have a requirement to add some custom classes and JSPs to an existing java web application. The custom classes must be in their own package, away from the existing java classes. The existing application is built on the struts framework.
I'm fairly new to all this and need guidance on the following:
Using Eclipse IDE, If I import the existing WAR file and libraries and unpack the WAR file, how do I compile new .java source files and add the .class files to the WAR?
The existing .class files are contained in a JAR. Do I need to create a JAR file containing the new .class files outside of eclipse and then add it to the same location as the existing .class JAR file i.e. WEB-INF/lib (there is no WEB-INF/lib/classes folder)
Adding the JSPs and amending the web.xml file should be fine but are there any pitfalls to look out for ?
Any and all help greatly appreciated
If I import the existing WAR file
I don't advise you to import third party war file into Eclipse. Instead do the following:
Create a dynamic web application in Eclipse (See the screenshot below):
Implement your custom classes and put them under the folder Java Resources / src
Put the JSPs under WebContent / jsps (you can choose any name you like)
Create a new directory in your file system (Windows / Unix ...) and copy the thirdparty war into it.
Unpack the war file using command line as:
jar xvf mythirdparty.war
Copy your class files (they should be under build / classes folder; see screenshot) to WEB-INF / classes folder of the unpacked thirdparty war.
Copy the JSPs (folder jsps) to the root of the unpacked war.
Remove the the old war (of the thirdparty war) so that
Navigate to the root and pack again as:
jar cvf thirdparty.war *
I'm a Java and Maven newbie and I have a question about the default manifest file.
As far as I understand, a manifest is created when a Java application is built using maven and it is added to the generated jar file.
I've also seen lots of references to a manifest.mf file, which can be found in a directory called meta-inf. But I can't find it anywhere.
I have successfully built my Java application using mvn package. I can see the jar file but I can't find the manifest file anywhere. Is it automatically deleted when the build completes?
The MANIFEST.MF file is inside the jar in the META-INF directory. You can see the content of your jar with jar tf your_jar_file.jar
You can also unzip the content of your jar with jar -xf your_jar_file.jar.
I have WAR file that contains symlinks for certain libraries.
When I extract the war file into webapps folder using unzip command it creates the symlink properly and web app loads without any issues.
However when I place the WAR file in webapps and let Tomcat deploy it , the symlink becomes a simple text file.
How do I overcome this situation, is there a way to customize the WAR explosion process.
EDIT
I suspect this is because Tomcat user Jar command to unpack a war file, is there a way to make tomcat use unzip command instead?
I have quite an interesting issue. I'm running maven to compile my Servlet site into a single WAR File. This works completely fine on my local machine; and even when I change my deployment settings to use just the war file, it works fine. However, when I deploy the WAR file to the server, I'm getting 404 errors. I'm no expert with WAR files, so is there some sort of internal file that specifies the location of resources that I need to look at?
First try to unzip your war (wars, jars, ears are zip files) and see if your files are actually there, verify if your unzipped war contains:
dir WEB-INF
file WEB-INF/web.xml
dir WEB-INF/lib with the jars your aplication depends on
dir WEB-INF/classes with *.class files, where your servlets and related classes are supposed to be (if you have decided to have them there and not in a jar in WEB-INF/lib)
static resources in the root directory
You could make your conclusions of what is missing in your war.
You could also try to build your own war manually (creating a zip file with the structure I mentioned above and renaming it as *.war) if you have problems with doing it by your IDE's options
I tried clean and build and found a .jar file in the dist directory. However, the libraries and resource files the program needs to run properly are absent in the .jar file. One workaround is put all libraries and resource files somewhere in the same directory or sub-directory of the .jar file. It is very inconvenient. How can I inject everything into the .jar file?
See the NetBeans documentation on Packaging and Distributing Java Desktop Applications, especially the section on Running and Distributing the JAR File.