Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hot reload Tomcat server in VSCode

I'm migrating from Eclipse IDE (+ VSCode for coding Java servlets and HTML/CSS/JS webpages, respectively) to only Visual Studio Code for its lightweight.


I have several Java extensions installed for VSCode:

  • Language Support for Java(TM) by Red Hat
  • Tomcat for Java
  • Debugger for Java

Eclipse has a series of settings for hot reloading:

- Automatically publish when resources change
- Refresh using native hooks or polling

while VSCode doesn't seem to have any for me.


A few things I've try to reload my Java and web code:

  • Restart Tomcat server
  • Delete and re-adding Tomcat server
  • Delete and regenerate .war package (not sure if this does anything, it can run well without a .war package)
like image 636
Dlean Jeans Avatar asked Jul 20 '18 04:07

Dlean Jeans


People also ask

What is reload in Tomcat?

On an Apache Tomcat server, to "reload" an application means to call a method of the StandardContext class called StandardContext. reload() on a given Context. This method creates a new classloader and new servlets, drops all references to the old servlets, and then calls the Servlet.

How do I run a war file code in Visual Studio?

Select Copy & Open to open your default browser. Paste in the access code and continue the sign in process. Once you have signed in, you can open the command prompt or terminal window and build the project using Maven commands. This will generate a new war or jar artifact in the target directory.


1 Answers

Good news...

It works automatically now. With Tomcat for Java Extension (And the rest of the Java Extension Pack): https://marketplace.visualstudio.com/items?itemName=adashen.vscode-tomcat

Just make sure your settings are like this (search for "java.autobuild" to get those two in the first results): enter image description here

Very important is, that you wont see any logs in the console on the HCR (Hot Code Replacement) like it happens in Eclipse ... but you in fact will see the replaced code behavior. Just debug over the piece of code you changed, and you will see it in fact changed in the running server.

UPDATE: I've found it works better with the 'manual'(default) setting. Just clicking once in the lightning icon. (Testing in Ubuntu 18.04 LTS) [I say it works better because if I added lines to a Class' code with the 'auto' setting it will not add that code ... only if I change code in the existing lines. But with the manual setting I just save the file, and then click the lightning icon wait ~3 seconds myself and debug over the new code ... and everything goes well!! enter image description here

... This solves my coding needs(lightweight Editor/IDE with Hot-Code-Replacement in Tomcat)]

Enjoy !

like image 163
Dan Ortega Avatar answered Oct 07 '22 10:10

Dan Ortega