Exploding ROOT.war - java

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.

Related

Creating war from its component folders

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/

Java, Tomcat - Can you load a resource during run-time from the WEB-INF/lib directory?

I have a Java project deployed on Tomcat 8. I'm not using any framework.
If, during run-time, I run something like:
this.getClass().getClassLoader().getResourceAsStream("xyz");
This works if "xyz" is a file located in the WEB-INF/classes directory; however, the above line returns 'null' if the file "xyz" is located in the WEB-INF/lib directory.
Is there any way for me to read files in the WEB-INF/lib directory during run-time using the 'getResourceAsStream()' method of the 'ClassLoader' class?
The answer, as I've learned from the comments, is that Classloader.getResourceAsStream() cannot read from the WEB-INF/lib folder in Tomcat.

how to change a file in web-inf/classes directory in war without unpacking?

here is the structure of my war file.
MyApp.war
WEB-INF
classes/com/info/App.java
classes/application.properties
lib/
META-INF
test
just before deployment of this war to tomcat, Ops team will need to update the application.properties file.
I prefer not to explode. but just to update the war and leave for tomcat to explode during start up
I tried this
jar uvf MyApp.war /var/data/prod/application.properties
After I executed this command, I see a new file being added at root level of MyApp.war. but I want WEB-INF/classes/application.properties to be replaced this one. How can I do it.
I don't believe that it is the good approach anyway, you should externalize your configuration file as it will change from one env to another, so it should not be part of your archive. You should adapt your code to be able to read the configuration of your application from an external folder. Your Ops team will then have the same war to deploy on all env instead of one per env, which is much less error prone.
How about this....
mkdir -p /tmp/MyApp.war/WEB-INF/classes
cp /var/data/prod/application.properties /tmp/MyApp.war/WEB-INF/classes
jar -uvf MyApp.war -C /tmp/MyApp.war WEB-INF
rm -rf /tmp/MyApp.war

Replace a file from .war with another file using Cron Job

I have a mayapp.war which is deployed in tomcat server. I want to replace a file(test.properties) which resides inside myapp.war with another file(test.properties) and this file resides outside myapp.war. I want to do this stuff using a cron job. will appreciate any kind of help.
A war file is just a zip file. You can use any zip command tool to replace a file or you can use the jar command from the JDK.
Use following command:
jar uf myapp.war test.properties

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

Categories