Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change part of Java class code after deploying to server as webapp

I have a Java servlet web app (.war) which I deployed to Tomcat Ubuntu server.

Can I change the content the Java classes at: myProject/WEB-INF/classes/
without re-export-and-deploy the entire app ?

Thanks in advance.

like image 403
hcohen Avatar asked Jan 07 '23 17:01

hcohen


2 Answers

I could thought of below to re deploy your app without restarting tomcat:

  1. Modify your java file
  2. Generate .class file
  3. Replace it with your existing file in Tomcat
  4. Trigger your app restart by touching WEB-INF/web.xml
like image 167
Nitesh Virani Avatar answered Jan 23 '23 05:01

Nitesh Virani


Disclaimer: I don't recommend doing this because it can give unexpected results. I am aware that sometimes you need to do "quick-and-dirty" fix, but consider yourself warned :)

I assume you have a Web application, let's call it 'MyApp'. Go to <TOMCAT-DIR>/webapps and locate your MyApp.war file. Put new .class file into the MyApp.war (open it with some archiver) and then delete the folder <TOMCAT-DIR>/webapps/MyApp. Deleting the folder means undeployment for Tomcat. After some time, Tomcat will figure out that it has a .war file in his <TOMCAT-DIR>/webapps folder that needs to deploy and it will automatically do it. You will see in Tomcat's log something like:

Aug 25, 2015 9:24:55 AM org.apache.catalina.startup.HostConfig undeploy
INFO: Undeploying context [/MyApp]
Aug 25, 2015 9:24:55 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /home/tomcat/apache-tomcat-7.0.55/webapps/MyApp.war
Aug 25, 2015 9:25:17 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive /home/tomcat/apache-tomcat-7.0.55/webapps/MyApp.war has finished in 21,937 ms

And there you go, you just refreshed your application without redeployment and without restarting server.

like image 34
Miljen Mikic Avatar answered Jan 23 '23 04:01

Miljen Mikic