[jetty-user] ClassLoader issues

Pinney Colton <[email protected]> Wed, 22 Jun 2011 09:23:48 -0500
Newsgroups gmane.comp.java.jetty.support
Message-ID <[email protected]>
I am very new to Jetty and have been struggling with what I thought was a
pretty simple proof-of-concept.  I am unable to instantiate an object of
class org.apache.lucene.util.OpenBitSet in main (below), despite including
it in my import statements.  I am savvy enough to know the issue is a
ClassLoader problem and not an issue with my Classpath, but I have been
unable to solve the problem.  I attempted to solve it using the first line
of code in main():

Thread.currentThread().setContextClassLoader(WebAppClassLoader.class.getClassLoader());


Exception:

Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/lucene/util/OpenBitSet
        at DataWarehouse.ActionHandler.main(ActionHandler.java:52)
Caused by: java.lang.ClassNotFoundException:
org.apache.lucene.util.OpenBitSet
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        ... 1 more


Full code here:

package DataWarehouse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.apache.lucene.util.OpenBitSet;
import java.lang.ClassLoader;
import org.eclipse.jetty.webapp.WebAppClassLoader;

public class ActionHandler extends AbstractHandler
{
        public void handle( String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response ) throws
IOException, ServletException
        {
                response.setContentType( "text/html;charset=utf-8" );
                response.setStatus( HttpServletResponse.SC_OK );
                baseRequest.setHandled( true );
                String resp;
                if ( request.getQueryString().equals( "execute" ) )
                {
                        resp = doExecute( request );
                }
                else
                {
                        resp = "Invalid action.";
                }
                response.getWriter().println( resp );
        }

        public String doExecute( HttpServletRequest request )
        {
                String r = "";
                Script s = new Script();
                try
                {
                        r = s.execute( request.getParameter( "content" ) );
                }
                catch( Exception e )
                {
                        r = "<pre>" + e.getMessage() + "</pre>";
                }
                return r;
        }

        public static void main( String[] args ) throws Exception
        {

Thread.currentThread().setContextClassLoader(WebAppClassLoader.class.getClassLoader());
                OpenBitSet bs = new OpenBitSet();
                Server server = new Server( 8080 );
                ContextHandler context = new ContextHandler();
                context.setContextPath( "/" );
                context.setResourceBase( "." );
                server.setHandler( context );
                context.setHandler( new ActionHandler() );
                server.start();
                server.join();
        }
}