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.


Java Magazine Premiere Issue

While the rest of the world is talking about the release of Java 7 today, this post is about the premiere issue of Java Magazine. Java Magazine is a free bi-monthly digital magazine and is all about Java technology, the Java programming language, and Java-based applications. It’s interesting that the premiere issue of Java Magazine is published the same day as the long awaited Java 7 release.

The first issue celebrates the arrival of Java 7. O ther highlights include Globo TV, RESTful Web Services, Resource Injection with Java EE 6, and Scala on the Java Virtual Machine.

If you have a subscription, you can start reading the premiere Java Magazine edition now. If you don’t have one, you can get a subscription from the Oracle website.


Quick Quartz 2.0 Tutorial

This is a quick Quartz 2.0 tutorial that uses the DSL/builder-based API. You will need Eclipse and the Eclipse m2e Plugin to follow the steps below.

1. Go to Eclipse and create a new Maven project.

Create a Maven Project

2. Click “Create a simple project” and hit Next. On the next page, enter Group Id as “com.teamextension”, Artifact Id as “quickquartz” and Name as “quickquartz”.

Configure Maven Project and hit Finish.

3. Right click on the project and go to Maven -> Add Dependency. Enter “quartz-scheduler” in the search box and pick “org.quartz-scheduler” version 2.0.2.

Add Dependency

We will also add SLF4J as a dependency so you can see the Quartz logs. Enter “slf4j-simple” in the search box and pick “org.slf4j slf4j-simple” version 1.6.1.

SLF4J

4. Create 2 classes com.teamextension.QuickQuartz and com.teamextension.QuickQuartzJob.

package com.teamextension;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.ScheduleBuilder;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class QuickQuartz {
	public static void main(String[] args) throws SchedulerException, InterruptedException {
		// start the Scheduler
		Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		scheduler.start();

		// create the Job
		JobDetail job = JobBuilder.newJob(QuickQuartzJob.class).
				withIdentity("QuickQuartzJob").build();

		// create the Schedule, run every 5 seconds
		ScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.
				simpleSchedule().
				withIntervalInSeconds(5).
				repeatForever();

		// create the Trigger
		Trigger trigger = TriggerBuilder.
				newTrigger().
				withIdentity("QuickQuartzTrigger").
				withSchedule(scheduleBuilder).
				startNow().build();

		// schedule the Job
		scheduler.scheduleJob(job, trigger);

		// run for 30 seconds and exit
		Thread.sleep(1000 * 30);
		scheduler.shutdown();
	}
}

The main class starts the Scheduler, and creates a job and trigger. The schedule in the example is set to every 5 seconds. We also sleep for 30 seconds to give time for the job to run.

package com.teamextension;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class QuickQuartzJob implements Job {
	public void execute(JobExecutionContext context) throws JobExecutionException {
		System.out.println(new Date());
	}
}

The job simply prints the current time to System.out.

Run the application and you should see the current date printed out a few times before the program exits.

For more in-depth information, read what’s new with Quartz 2.0, Quartz Cookbook and the official Quartz Tutorial.