Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Thursday, October 8, 2009

How To: Jars in a jar using Eclipse

I wrote a utility using GeoTools which uses a couple classes to transform geometry from one projection to another.  Projecting geometry uses EPSG definitions stored in the gt-epsg-hsql-2.5.7.jar which does not get bundled into an executable jar; for my utility to work as executable jar I needed to include the gt-epsg-hsql-2.5.7.jar.  


There are several approaches to building a jar that contains jars.  They typically involve using ant to bundle the files into the jar and a special class loader.  Writing the build.xml file and the class loader can be tedious.  Fortunately, the Fat Jar eclipse plugin simplifies the process and includes the One-JAR class loader.  


Installing the plugin can be done using Software Updates:



Click on Add Site

Enter the web site URL

Select the Fat Jar plugin and click on Install


Click Finish


Restart eclipse to finish the installation.


To build your project with external jars included, click on Export


Click on Other and select the Fat Jar Exporter

Select the project

Enter the name of the output jar, select the main class, and check off the One-JAR option to use the One-JAR class loader.

Clicking on next opens a dialog to select the jars to include, the default selects all external jars.  Unlike a typical executable jar, all the imported classes are not included so take the default and click on finish.  The Fat Jar plugin will build the jar with the external jars included, note that the final jar can become quite large.

Saturday, May 10, 2008

Embedded tomcat and launch4j FTW

I've been meaning to get around to this for years. Using this example, I compiled it with my webapps and then used lauch4j to create a windows executable wrapper. Throw in a jre and some data and I'm good to go. Now I just give the live demos to the sales people and clients on a USB stick without having to go through the install or setting it up on a web server. Here's the code example:
import org.apache.catalina.*;
import org.apache.catalina.connector.*;
import org.apache.catalina.realm.*;
import org.apache.catalina.startup.*;


public class EmbeddedTomcat
{
  // Instance variables:
  private String    name;
  private int       portNumber;
  private Embedded  embedded;
  private Engine    baseEngine;
  private Host      baseHost;
  private Connector httpConnector;
 
  /** Creates a new instance of EmbeddedTomcat */
  public EmbeddedTomcat(
    String name,
    int    portNumber)
  {
    this.name = name;
    this.portNumber = portNumber;
 
    init();
  }
 
  private void init()
  {
    MemoryRealm realm;
    Context     context;
    String      baseEngineName;
    String      hostName;
 
    embedded = new Embedded();
 
    // set default logger and realm
    /*
       FileLogger fileLog = new FileLogger();
       fileLog.setDirectory(".");
       fileLog.setPrefix(name);
       fileLog.setSuffix(".log");
       fileLog.setTimestamp(true);
       embedded.setLogger(fileLog);
     */
    realm = new MemoryRealm();
    embedded.setRealm(realm);
 
    // create an Engine
    baseEngine = embedded.createEngine();
 
    // set Engine properties
    baseEngineName = name + "Engine";
    hostName = name + "Host";
 
    baseEngine.setName(baseEngineName);
    baseEngine.setDefaultHost(hostName);
 
    baseHost = embedded.createHost(hostName, "webapps");
    baseEngine.addChild(baseHost);
 
    // RootContext
    context = addContext("", "ROOT");
 
    // ManagerContext
    context = addContext("/manager", "manager");
    context.setPrivileged(true);
    

   /* This is where you add your webapps*/

    //  GeoBrowserContext
    context = addContext("/geobrowser", "geobrowser");
    context.setPrivileged(true);
    
    // add new Engine to set of Engine for embedded server
    embedded.addEngine(baseEngine);
 
    // create Connector 
    httpConnector = embedded.createConnector((java.net.InetAddress) null,
        portNumber, false);
 
    // add new Connector to set of Connectors for embedded server, associated
    // with Engine
    embedded.addConnector(httpConnector);
  }
  
  
 
  public void start()
  {
    // start server
    try
    {
      embedded.start();
    }
    catch (org.apache.catalina.LifecycleException ex)
    {
      ex.printStackTrace();
 
      //fileLog.log("Startup failed");
      //fileLog.log(ex.getMessage());
    }
  }
  
  public void stop()
  {
    // start server
    try
    {
      embedded.stop();
    }
    catch (org.apache.catalina.LifecycleException ex)
    {
      ex.printStackTrace();
 
      //fileLog.log("Startup failed");
      //fileLog.log(ex.getMessage());
    }
  }
 
  public Context addContext(
    String path,
    String docBase)
  {
    Context c;
 
    c = embedded.createContext(path, docBase);
    baseHost.addChild(c);
 
    return c;
  }
 
  public static void main(String[] args)
  {
    new EmbeddedTomcat("RedSpider", 8080).start();
  }
}