views:

459

answers:

3

I get a "java.lang.NoClassDefFoundError: com/hp/hpl/jena/shared/BadURIException" when running a very simple servlet. The error points back to the initialisation of the "Tagger" class. The code is as follows

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import my.package.Tagger;

public class NormaliserServlet extends HttpServlet{
    public void doGet(HttpServletRequest req, HttpServletResponse response) throws IOException{
     Tagger pot = new Tagger("");

     response.setContentType("text");
     PrintWriter out = response.getWriter();
     out.println("hello");
        out.println(pot.someMethod());
     out.close();

     this.log("Request for normaliser");
    }
}

The war file contains the jar file defining "Tagger" in WEB-INF/lib and a similar invocation works outside of a servlet. I can't seem to find what the problem would be. The web.xml is pretty standard too:

<servlet>
    <servlet-name>normalise</servlet-name>
    <servlet-class>NormaliserServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>normalise</servlet-name>
    <url-pattern>/normalise</url-pattern>
</servlet-mapping>

I'm using winstone as the servlet container, but i get the exact same error in tomcat. The stacktrace is:

java.lang.NoClassDefFoundError: com/hp/hpl/jena/shared/BadURIException
    at NormaliserServlet.doGet(NormaliserServlet.java:13)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:104)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:45)
    at winstone.ServletConfiguration.execute(ServletConfiguration.java:249)
    at winstone.RequestDispatcher.forward(RequestDispatcher.java:335)
    at winstone.RequestHandlerThread.processRequest(RequestHandlerThread.java:244)
    at winstone.RequestHandlerThread.run(RequestHandlerThread.java:150)
    at java.lang.Thread.run(Thread.java:619)
+1  A: 

It looks like you're missing the jar for Jena(?) which defines the BadURIException class. Is that jar included in your WEB-INF/lib directory as well? Have you tried looking at the unpacked war file in Winstone and checking if the relevant jars are all there?

Mat Mannion
Well it seems to have done the trick (I get another error now), but I still don't see why I should have to include a jar file that my application doesnt actually use. Tomcat does the same thing. shouldn't it be the container's job to include the libs it needs? Is it a setup problem for winstone/tomcat?
brice
@brice: Are you sure that your application does not use the library? Tomcat certainly does not, and I cannot imagine that Winstone does.
Thilo
@brice It could be your application, or one of the libraries it uses, but somewhere along the line there would seem to be a dependency on Jena. If it happens in Tomcat as well it seems unlikely to be Winstone
Mat Mannion
@Thilo: You're absolutely right, I'm being a complete douche. Got everything fixed now. Reminder to self: blame self first... I shouldn't have even asked this.
brice
@brice, re: douche. "blame self first" is a good policy, but you should most definitely have asked this here. This kind of problem happens to people all the time.
Thilo
+2  A: 

You are missing the jar files from the Jena project. They should be in your WAR file.

It could be an indirect dependency of some other library that you use. Take a look at the stacktrace to see what else sits between Jena and your code.

Thilo
A: 

This class com/hp/hpl/jena/shared/BadURIException, presumably instantiated by Tagger is not in your servlet container's classpath.

If it works outside your servlet container, it's likely you have entries in your environment defining the classpath. Check the classpath and copy the missing lib within the servlet container's lib directory.

diciu
Not sure if you really want to copy the library into the servlet container's lib directory. That could lead to conflicts. The usual way is to put it into the web application's lib directory (WEB-INF/lib).
Thilo
You're right, WEB-INF/lib under the webapp context is a better option.
diciu