Hot Deployment of Java Web Applications in Eclipse

Being able to hot deploy changes you make to your Java web applications is very important. This makes development more productive since it eliminates the build and deploy steps. You use your IDE to make changes and you can see and test your changes right away. Here are ways to do it when using Eclipse.

1. Servers View

The easiest way to hot deploy your Java web application is to create a Server in the Eclipse Servers view and run your web application inside the Server. For this article, We will use Tomcat 7 as our Server.

Servers View

Server View - Figure 2

Your web application must be assigned the Dynamic Web Module Project Facet for this to work. If you didn’t create this web application using the Eclipse New Project Wizard, you will have to assign this Project Facet in Project Properties.

Servers View, Figure 3

Now make changes to your controller and you should see Eclipse automatically reload the web application a few seconds after you save your changes.

Servers View, Figure 4

Servers View, Figure 5

2. Maven Jetty Plugin

If you use Maven to run your server, you can use the Jetty Plugin. It can be configured to periodically scan for changes and automatically redeploy the webapp.

Add the Jetty plugin in pom.xml and run jetty:run. Version 6.1.26 is the latest as of this writing.

    <plugin>
    	<groupId>org.mortbay.jetty</groupId>
    	<artifactId>maven-jetty-plugin</artifactId>
    	<version>6.1.26</version>
    	<configuration>
    		<scanIntervalSeconds>5</scanIntervalSeconds>
    	</configuration>
    </plugin>
project-dir> mvn jetty:run

Now make changes to your controller in Eclipse and you should see the Maven Jetty plugin automatically reload the web application.

3. JRebel

If you have to run Tomcat from the command line or as a service, you can use JRebel. If you need to reload libraries or you have a more complicated application setup, you also will have to use JRebel. There’s a small annual license fee, and it’s free for OSS developers.

To set up, get a JRebel license and install the JRebel Eclipse Plugin from the Eclipse update site http://www.zeroturnaround.com/update-site/. When prompted for a license, copy the license file to the plugin directory and point the license prompt to that file. The plugin directory is something like <eclipse-home>pluginsorg.zeroturnaround.eclipse.embedder_4.*jrebel, and this is where jrebel.jar lives. If you use Pulse, this will be under the Genuitec/Common folder.

To setup the web application, right click on the project and go to JRebel -> Generate rebel.xml in src/main/resources.

JRebel

To setup Tomcat, create <tomcat-home>/bin/startup-jrebel. cmd and use this to run Tomcat.

@echo off
set JAVA_OPTS=-javaagent:"<jrebel-home>jrebel.jar" %JAVA_OPTS%
call "%~dp0startup.bat" %*

JRebel, Figure 2

Now make changes to your controller in Eclipse and you should see JRebel automatically reload the class.

4. Exploded WAR

Another approach is to deploy an exploded war file, and configure Eclipse to write classes to it. You can write a custom Project Builder to copy the .class files to the exploded war file.

Exploded WAR

If your operating system supports it, you can also create a symbolic link so you don’t have to worry about copying the files. If you don’t want to deploy an exploded war file, just overwrite the .war file every time you make changes in Eclipse using a custom Builder and Tomcat should reload the web application automatically.


Eclipse Indigo on Java 7

I have installed Java 7 on my development machine and Eclipse Indigo wouldn’t start up properly. Eclipse sometimes would crash, sometimes it would hang. Looking at the workspace .metadata/.log file shows errors that say

java.lang.OutOfMemoryError: PermGen space

Eclipse has an FAQ page about how to increase the permgen size, but I didn’t go this route. I opted to switch Eclipse back to Java 6. I went to eclipse.ini and added the following line

-vm
C:Program FilesJavajre6binclientjvm.dll

I had to add this before -vmargs, or else it didn’t work. Eclipse seems to be back to normal again.

Note: If you are instead looking to add Java 7 development support to Eclipse 3.7, go to the Eclipse update site http://build.eclipse.org/eclipse/java7patch/ and add Eclipse Java Development Tools Patch for Java 7 Support (BETA).

Update 8/5/2011: Please read Java 7 on Eclipse Indigo to see how to run Java 7 code on Eclipse. The Java Tools Patch for Java 7 is no longer available.


Debugging Java Web Applications in Eclipse

Debugging Inside Eclipse

The easiest way to debug your Java web application is to create a Server in the Eclipse Servers view and start the Server in Debug mode. For this article, We will use Tomcat 7 as our Server.

Your web application must be assigned the Dynamic Web Module Project Facet for this to work. If you didn’t create this web application using the Eclipse New Project Wizard, you will have to assign this Project Facet in Project Properties.

Place breakpoints and start stepping through code.

Remote Debugging

If you cannot run your server inside Eclipse, try remote debugging. First, start the Server in debug mode. To run Tomcat in debug mode, run the following command.

tomcat-bin>catalina jpda start

Then go to Run -> Debug Configurations… and create a new Remote Java Application.

The default debug port for both Tomcat and Eclipse is 8000, so we just accept the defaults. Click the Debug button to start the remote debugging process.

Maven Debugging

If you run your Server from within Maven, then we have to run Maven in debug mode.

webapp-home>mvnDebug tomcat:run

This starts Maven, and Tomcat, in debug mode. Now that Tomcat is running, follow the same steps as remote debugging Tomcat from Eclipse. Note that mvnDebug uses -Xrunjdwp:suspend=y, so Maven is suspended until Eclipse connects to it.