Running Vaadin in embedded Glassfish

Couple people have asked from me how to run Vaadin with embedded Glassfish, decided to write few steps how to do it.

  1. Download Glassfish Web bundle

    http://download.java.net/glassfish/3.1.1/release/glassfish-3.1.1-web.zip

  2. Extract Glassfish to your drive

  3. Open your project on your IDE and add following jar’s to your classpath:
    glassfish3/glassfish/lib/embedded/glassfish-embedded-static-shell.jar
    glassfish3/glassfish/lib/javahelp.jar
    glassfish3/glassfish/lib/javaee.jar
    glassfish3/glassfish/lib/appserv-rt.jar
    glassfish3/glassfish/lib/jndi-properties.jar
    (Not all are required for all Web projects but I use CDI and Servlet 3.0 spec)

  4. Add below GlassfishLauncher.java to your project


public class GlassfishLauncher {

	private static String context = "myproject";
	private static String webroot = "/eclispeworkspace/myproject/WebContent";
	private static int httpPort = 8080;

	public static void main(final String[] args) throws Exception {
		GlassFishProperties gfProps = new GlassFishProperties();
		gfProps.setPort("http-listener", httpPort);
		GlassFish glassfish = GlassFishRuntime.bootstrap()
				.newGlassFish(gfProps);
		glassfish.start();
		Deployer deployer = glassfish.getDeployer();
		deployer.deploy(new File(webroot), "--contextroot=" + context,
				"--name=myproject", "--force=true");
	}
}

Note, edit webroot to point into your web project’s WebContent, meaning the directory that contains WEB-INF

  1. E.g. on Eclipse’s package explorer, right click GlassfishLauncher.java and select either “Debug as…” or “Run as…”
    => Your Vaadin application starts in few seconds, browse to http://localhost:8080/myproject/

Nice thing using embedded Glassfish is that this works with any Eclipse bundle, even the most simplest (and quickest) bundle. When you are done, just end the Glassfish process on your IDE. Starting the server can be done with IDE’s hotkey (e.g. debug or run last).

I personally like to use Glassfish because it supports Servlet 3.0 and comes with CDI out of the box. CDI is very nice with Vaadin.