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

  2. Extract Glassfish to your drive

  3. Open your project on your IDE and add following jar’s to your classpath:
    (Not all are required for all Web projects but I use CDI and Servlet 3.0 spec)

  4. Add below 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()
		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 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.