Creating war from its component folders - java

I have deployed my war file on a remote linux server. I run this war using jetty-runner. Its not feasible for me to push this war multiple time. Its size is huge and it takes aprrox 45 min to push a fresh war onto the server. To handle this issue I thought of using the following steps(with commands) :
unzip:Unzip war to its corresponding files/folders : WEB-INF, META-INF, index.jsp.
Updating new class file in WEB-INF.
zip:Repacking these folder into a war again.
But the newly created war does not work. Is there a standard/correct way to pack these files into a war. Also, jar command is not available on the server.
Please suggest.
P.S. Already looked into various SO questions but didn't find any useful solution.

The zip command does not work as expected. The war packed by that command did not work. Instead, we have to use the JAR command.
I was able to generate the war after modifying the contents by using :
jar -cvf webproject.war index.jsp META-INF/ WEB-INF/
Note: If the jar command is not available on the server, specify JAR path using installed java on the server:
PATH_TO_JAVA/bin/jar -cvf webproject.war index.jsp META-INF/ WEB-INF/

Related

Can't execute war - file: "no main manifest attribute"

i have created a jsp project with webservice in netbeans and i have created a war file to implement it in the AWS but while executing the command "java -jar filename.war it is showing some error
no main manifest attribute, in Hurling_Server.war
root#ip-172-31-21-53:/home/ubuntu# java -jar Hurling_Server.war
no main manifest attribute, in Hurling_Server.war
How you can execute/use the WAR file depends on what it contains and how you have packaged it. WAR files are web archives meant to be deployed in server containers like Tomcat, Jetty, etc.
If this is a regular WAR, deploy it in a server like Tomcat, Jetty and access the web application.
Have you bundled an embedded server like Tomcat or Jetty in the WAR? If yes then most likely it is already executable depending on how you have created the WAR in the first place. If not embedded nay server then, you have to add the MANIFEST.MF under META-INF/. And in the manifest file you have to specify the main class name like:
Main-Class:mypackage1.mypackage2.MyExecutableClass
The WAR structure should look like:
mypackage1/mypackag2/MyExecutableClass
WEB-INF/lib
WEB-INF/classes
META-INF/MANIFEST.MF
With embedded Jetty the MANIFEST and executable main class are placed the following way in WAR:
jetty/bootstrap/JettyBootstrap.class
jetty/bootstrap/LiveWarClassLoader.class
META-INF/MANIFEST.MF
So, why your WAR file is not executing depends on several factors mentioned above. Please check.

How to properly export a jar for running an applet?

I have been adding modifications to a Java application that runs on a server using an applet. On the server, there is just the jar file, and an index.html page that opens the applet.
Now, I am trying to test the new version of the app on my computer before putting it on the production server, and it doesn't work : the application doesn't connect with the MySQL DB (when I just run it from Eclipse it works, it is when I try to run the jar file from my browser that it doesn't work). I tried running the old app on my desktop the same way and it runs just fine, so I guess the problem is in my jar file.
I have opened the 2 jar archives to see what's inside, and here's what I get :
Old jar :
Different folders for the different packages of the app
bin folder
com folder
META-INF folder
org folder
.classpath file
.project file
java.policy.applet file
mysql-connector-java-5.0.8-bin.jar
And now, here is what's inside the new jar :
Different folders for the different packages of the app
META-INF folder
.classpath file
.fatjar file
.project file
java.policy.applet file
mysql-connector-java-5.0.8-bin.jar
settings.fatjar file
So my archive doesn't have the bin, com and org folders. It also has 2 additionnal files.
I don't know which settings should I use when exporting my jar in Eclipse to obtain the same content ?
Also, my new jar isn't signed, could this have something to do with my problem of database access ?
I'll answer my own question, as I found the solution :
The jar needs to be exported as a runnable jar
The option "extract required libraries into jar" must be selected
The jar must be signed (self signed works as it is an internal app, so no need to pay a certificate)
Now it works :)

Create a war file including external jars using command prompt

I have two projects - Project1 and Project2. Project1 contains all the java classes. And project2 contains all the jar files required in project1. Means, Project1 depends on Project1 (for jars).
I am using Glassfish server. While creating the war file of Project1, I included all the jar files from project2.
Example:
Project2 conatins 2 packages - POI and web-services.
POI package contains: poi-examples-3.6-20091214.jar,poi-contrib-3.6-20091214.jar, poi-ooxml-3.6-20091214.jar, poi-ooxml-schemas-3.6-20091214.jar, poi-scratchpad-3.6-20091214.jar.
web-services package contains: javax.servlet-3.0.jar, javax.servlet-api.jar, jersey-bundle-1.10.jar, jersey-client-1.12.jar, jersey-core-1.12.jar, jersey-multipart-1.4.jar.
Now for creating the war file for Project1 from command prompt, I used the following command:
jar cvf Project1.war D:\JavaProject\Project2\lib\external\poi\poi-examples-3.6-20091214.jar D:\JavaProject\Project2\lib\external\poi\poi-contrib-3.6-20091214.jar D:\JavaProject\Project2\lib\external\poi\poi-ooxml-3.6-20091214.jar D:\JavaProject\Project2\lib\external\poi\poi-ooxml-schemas-3.6-20091214.jar D:\JavaProject\Project2\lib\external\poi\poi-scratchpad-3.6-20091214.jar D:\JavaProject\Project2\lib\external\web-services\javax.servlet-3.0.jar D:\JavaProject\Project2\lib\external\web-services\javax.servlet-api.jar D:\JavaProject\Project2\lib\external\web-services\jersey-bundle-1.10.jar D:\JavaProject\Project2\lib\external\web-services\jersey-client-1.12.jar D:\JavaProject\Project2\lib\external\web-services\jersey-core-1.12.jar D:\JavaProject\Project2\lib\external\web-services\jersey-multipart-1.4.jar
Project1.war file created in the current directory.
But, when I tried to deploy the Project1.war on Glassfish server, I encountered this error:
C:\glassfish3\glassfish\domains\domain1\applications\Project1\D:\JavaProject\Project2\lib\external\poi\poi-contrib-3.6-20091214.jar
(C:\glassfish3\glassfish\domains\domain1\applications\ is the directory where '.war' files are copied after deployment)
I could not understand the problem. Is something wrong with the command, I executed for creating war file? How are external jars included while creating war file using command line prompt?
Please help.. Thanks in advance!
A WAR file has to have a specific file structure.
/
META-INF/
WEB-INF/
classes/
classes and other classpath resources go here
lib/
jar files go here
web.xml
other webapp stuff; e.g. HTML, JSPs, CSS ...
If you just throw stuff into a JAR file at random, it won't be recognizable as a webapp when it is unpacked by the webapp container.
You should be using the WAR exploded layout, in other words, project2 should be included at $PROJECT1_DEPLOYMENT_DIR/WEB-INF/lib and so on. Then your jar command will result in a deployable file to Glassfish or any other servlet container.

where does tomcat store my files?

I've created a web application using Netbeans. Before, when I was running the web app via netbeans and tomcat server (which was a zip), all my external files (uploaded files and other helper files I use for my app) are stored in bin directory.
Now, I tried installing an apache tomcat service using windows installer because I wanted to know how to deploy the project on a dedicated server. I have successfully deployed the war file using tomcat's deploy utility. However, when I run the project via the apache tomcat windows service, it is not saving the files in bin dir and it cannot read my files that I pasted in bin dir, too.
Where do you think should I place my files?
EDIT: Upon observing the tomcat service directory, I found out that it is store in the root. If I have my tomcat installed at 'E:\Apache\services\tomcat\', it is stored at the 'tomcat' directory.
Ultimately, it is what your application does that determines where the files are stored.
By the sounds of it, your application is storing files in the current directory of the JVM, which happens to be the "bin" directory when you launch the web server via NetBeans. If so, you will find them, in whatever the current directory is when Tomcat is launched as a windows service.
Frankly, I think you've got this wrong. You should be making a conscious decision as to were uploaded files should be stored, and then making sure that the upload mechanism you are using puts them there.
Obviously, putting them in the current directory is a bad idea. You don't want them being stored in different places depending on how the web container is started. And obviously the "bin" directory is an inappropriate place. (What happens if the user tries to upload a file whose name matches one of the scripts that live in "bin"?)
So where should you be putting the files?
In my opinion, you've got three choices:
In a subdirectory of the work directory ... which is where Tomcat conventionally puts transitory files such as compiled JSPs.
In a custom subdirectory of the Tomcat installation directory.
In a separate directory somewhere else in the file system.
You shouldn't be dropping then in the webapp directory, because files there are typically blown away when the webapp is redeployed, and because there's a greater risk that uploaded files will interfere with your webapp.
You shouldn't be dropping them in the bin or logs or lib or config directories because of the risk of interference ... and because they are simply not the logical place.
If you want to write files relative to the root of the tomcat installation directory, you can find out what that is by calling System.getProperty("catalina.base").
But what ever you do, you need to make sure that a user can't accidentally or deliberately upload files to the wrong place; e.g by supplying an absolute pathname, or a pathname that uses "../../...." to escape from your upload area.
When you install Apache, the project should be inside the webapp folder :
C:\Apache\tomcat\webapps
Like my Project is gaganisonline so the directory structure is something like this :
Path : C:\Apache\tomcat\webapps\gaganisonline
gaganisonline
| |
WEB-INF index.html
|
---------------------------
| | | |
web.xml src lib classes

Exploding ROOT.war

I am copying my ROOT.war file to webapps of Tomcat 6.0 . Is there a way I can explode the file upon copying. i.e when my script copies it in the webapps .. it should explode and create ROOT directory ??
There are two ways that come to mind.
One is that the WAR file is a jar file, so you can use the jar command to expand it.
The other is to use the Tomcat autodeploy magic. You copy the WAR file in; Tomcat notices the change, expands the WAR, and reloads it.
The Ant task unzip/unjar will also do the trick as the WAR file is JAR/ZIP
Simple, but you could always do:
cp blah.jar /usr/local/tomcat/webapps && unzip blah.jar
It assumes Unix, on windows you should be able to do something similar...
You should not be putting your stuff in ROOT context. Create a war named Foo.war and let "Foo" be the name of your context.
My WAR failed to auto-explode after I dropped it into the /webapps/ directory in tomcat7 because of read-only file permissions.
After I changed the .WAR file permissions, the WAR was exploded successfully.

Categories