Archive for June, 2010

Creating windows executable launcher of our Java web application

In this tutorial we will see how to create exe launcher for Java web application. For simplicity let us divide this tutorial in several parts. In the first part we will create simple jar launcher which can deploy our web application in embedded server. After successfully achieving this we will see how can we create exe file from it using Java Native interface (JNI).

1. Create launcher jar file using embedded Glassfish api which will remove dependency of server on machine
2. Create exe using JNI invocation API
Part 1:
Lets start with embedding webserver/servlet container in our Java program. Glassfish and Tomcat both provide an api for embedding server in java application. We will be using Glassfish for achieving our purpose.
Requirements:
Here is a test program for launching simple web application:
package test.glassfish;
import org.glassfish.api.deployment.DeployCommandParameters;
import org.glassfish.api.embedded.*;

import java.io.*;
public class WebApplication {
public static void main(String[] args) throws IOException, LifecycleException {
Server.Builder builder = new Server.Builder(“test”);
Server server = builder.build();
server.createPort(8080);
ContainerBuilder containerBuilder = server.createConfig(ContainerBuilder.Type.web);
server.addContainer(containerBuilder);
server.start();
File war = new File(“C:\\Users\\sunils\\maventests\\simple-webapp\\target\\simple-webapp.war“);
EmbeddedDeployer deployer = server.getDeployer();
DeployCommandParameters params = new DeployCommandParameters();
params.contextroot = “simple”;
deployer.deploy(war, params);
}
}
You just need to change path to war file. You should have your web application started under domain http://localhost:8080/simple

As this is embedded server and we want to create exe out of it we need a way to get logging out of it. In next part we will see how to exe launcher from above program