Executing Server Jar Programatically (Caucho Resin)

577 views Asked by At

I have successfully used the Caucho Resin web server and I'm considering of packaging it into a java application. They provide a jar file and code in order to start the server programatically.

Example: example/TestResin.java

package example;

import com.caucho.resin.*;

public class TestResin {

  public static void main(String []args)
  {
    ResinEmbed resin = new ResinEmbed();

    HttpEmbed http = new HttpEmbed(8080);
    resin.addPort(http);

    WebAppEmbed webApp = new WebAppEmbed("/", "/var/www/htdocs");
    resin.addWebApp(webApp);

    resin.start();
    resin.join();
  }
}

However I get the following error

java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at com.caucho.server.resin.EnvInit.init(EnvInit.java:49)
    at com.caucho.server.resin.EnvInit.<init>(EnvInit.java:44)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at java.lang.Class.newInstance(Class.java:374)
    at com.caucho.loader.Environment.initializeEnvironment(Environment.java:971)
    at com.caucho.resin.ResinEmbed.start(ResinEmbed.java:320)
    at com.mycompany.server.Resin.main(Resin.java:23)
Caused by: java.lang.ClassNotFoundException: javax.transaction.TransactionManager
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 22 more
Exception in thread "main" java.lang.NoClassDefFoundError: javax/enterprise/inject/spi/BeanManager
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at com.caucho.env.service.ResinSystem.<init>(ResinSystem.java:138)
    at com.caucho.env.service.ResinSystem.<init>(ResinSystem.java:99)
    at com.caucho.server.resin.Resin.<init>(Resin.java:236)
    at com.caucho.server.resin.ResinEmbedded.<init>(ResinEmbedded.java:46)
    at com.caucho.resin.ResinEmbed.start(ResinEmbed.java:322)
    at com.mycompany.server.Resin.main(Resin.java:23)
Caused by: java.lang.ClassNotFoundException: javax.enterprise.inject.spi.BeanManager
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 18 more
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Solution:

Added the javaee-16.jar library found inside glassfish installation directory and all works. Apparently donwloading the glassfish-api.jar from Oracle doesn't work because only methods declarations are included and no implementations.

3

There are 3 answers

0
saimiris_devel On BEST ANSWER

Added the javaee-16.jar library found inside glassfish installation directory and all works.

0
Danny Daglas On

This is a process lifecycle question that should be carefully considered for your app. There are several ways to do this:

  • Executing from within your app via Runtime.getRuntime().exec() or equivalent. Essentially, spawn some process outside of the running JVM from your app.
  • Writing scripts and/or cron jobs externally. I.e., externally spawning some process outside your app.
  • Running the web server's main() method from a thread in your app. (You can determine this by inspecting the jar's manifest to get its Main Class.) This means that you are running it as a thread (or threads) from within your running JVM.

From these, we can determine that the two dimensions you should consider are:

  1. Spawn/control from within or outside your app.
  2. Run from within or outside your JVM.

The bottom line is that you will have to determine what is best for your app since you know who will run it, how it will be deployed, if it's something that will run a long time or a short time, will it be spawned frequently or infrequently, etc. I suggest that you choose carefully. Often, people leave this as an afterthought, which often leads to lots of headaches down the road.

1
Danny Daglas On

Use this to interpret the jar as if you ran it with java -jar ...:

File f = new File("resin-3.1.0/lib/resin.jar");
URL url = new URL("file", null, f.getAbsolutePath());
ClassLoader cl = new URLClassLoader(new URL[] {url});
Class<?> mcls = cl.loadClass("a.b.c.Main");
Method mmethod = mcls.getMethod("main", new Class[] {String[].class});
mmethod.invoke(mcls);

Here, Resin will run in its own classloader.