Reduce server restart on java web projects

Java web application development have been criticized a lot in the past as painful compared to building building php web application.  When developing an application in php, developers would refresh the browser to see changes they have made.

In java, the developer would have to  restart the server and then refresh the browser. This process limits developer productivity. JRebel was created to solve this problem and has done this very well. The only problem is that jrebel is not free.  

A free alternate to jrebel is  "Dynamic Code Evaluation Virtaul Machine"  or  "DCEVM" .  I wrote about DCEVM  a while back. This article would describe how I use it in my development environment. My environment consist of the following tools

  1. Eclipse
  2. TomEE
  3. JDK 7 update 51 and above with DCEVM installed

  Let's get started. If you haven't downloaded these tools , do so now.

Install DCEVM

 

To install DCEVM, download the jar file to your machine and execute the jar

dcevm

Select the JDK you want to install DCEVM into and click on "Install DCEVM as altjvm"

dcevm screen

Setting  up the project in eclipse

Next step would be to create a "Dynamic Web project"  or a "Java project" in eclipse. There are many articles/tutorials on the internet describing the process of creating a java project in eclipse.

I am not going to cover that in this article.   I normally create my projects manually and then configure them in eclipse.

Here is an example of my folder structure and configuration.

eclipse folder structure

 

My project's folder structure from package explorer under Java perspective.

 

folder structure eclipse

 

Let's prepare TomEE for development purposes.

Go to TomEE's web.xml file in tomee/conf/web.xml "org.apache.jasper.servlet.JspServlet".

Change the development init param value to true.

 

web xml

Next add a user library to eclipse that contains all the libraries that comes with TomEE.

tomee classpath

Right click on your project > select properties> Java build path > The library tab Click on Add Library > User Library > Next > Select EXTENDIT_TOMEE > Finish > Ok

 Note : You can also just add the jar files(tome's libraries) to the project's build path.

Under TomEE's conf folder, create an xml file (context file) called ROOT.xml( or the name of your context) . 

The content of your file should look like this below:

context element

Next create a servlet and a service bean extendit.TestServlet.java

 

test servlet

extendit.TestService

 

test service

Next , create a "Run Configuration" for the application in eclipse.

run configuration

In the main class section and the Boostrap class from TomEE : org.apache.catalina.startup.Bootstrap On the "Arguments" tab :

  1.    add "start" to "Program arguments"
  2.    add the following to the "VM arguments" ( configure to you own environment)

 

 -Dfile.encoding=UTF -Djava.endorsed.dirs="tomee/endorsed" -Dcatalina.base="tomee" -Dcatalina.home="tomee" -Djava.io.tmpdir="tomee/temp" -XXaltjvm=dcevm

 

run configuration

 

On the "Classpath" tab:

  1. add eclipse bootstrap jars from TomEE's bin folder

 

run configuration classpath

On the "Common" tab

  1. Select "Debug" and "Run"

 

run configuration common

 

Click Apply and Close  "Run Configuration".  

 Configure project's deployment descriptor

Add bean.xml and web.xml to WEB-INF folder under webapp directory.

bean web xml

beans.xml

 

bean xml

  web.xml

 

web xml

Next Run, your project with the debug configuration. Make changes to the service class and refresh your browser.

Project Source