CodeMonkey
CodeMonkey

Reputation: 2295

Servlet throws Exception

This is my Servlet code which i am using to query a solr index

import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

//Solr Imports
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.client.solrj.request.AbstractUpdateRequest;

/*
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
*/

public class HelloWorldExample extends HttpServlet {

    public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
    {
        PrintWriter out = response.getWriter();
        String inputStr=request.getParameter("input");
        out.println("<html><h1>" + inputStr + "</h1></body></html>");
        //try {
        SolrServer server = new CommonsHttpSolrServer("http://localhost:8080/solr/");
        SolrQuery solrQuery = new SolrQuery(); 
        //solrQuery.setQuery("fileName:"+input);
        solrQuery.setQuery("Latitude:"+32.55668);
        QueryResponse rsp = server.query(solrQuery);
        //SolrDocumentList x = rsp.getResults();
        System.out.println(rsp);
        }
        catch (SolrServerException e) {
            e.printStackTrace();
        }       
    }
}

I am compiling this code using

D:\xampp\tomcat\webapps\examples\WEB-INF\classes>javac -classpath .;D:\JAR\servl et-api.jar;D:\JAR\1solr-core-1.3.0.jar;D:\JAR\1solr-solrj-1.3.0.jar;D:\JAR\1solr -common-1.3.0.jar;D:\JAR\apache-solr-solrj-1.4.0.jar HelloWorldExample.java

This code compiles without problem. However when i run this servlwt it gives me an error:

type Exception report message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception root cause

java.lang.NoClassDefFoundError: org/apache/solr/client/solrj/impl/CommonsHttpSolrServer HelloWorldExample.doGet(HelloWorldExample.java:35) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

Upvotes: 0

Views: 2559

Answers (2)

BalusC
BalusC

Reputation: 1109874

Put the library JAR files where the webapp depends on in its /WEB-INF/lib folder. That folder is by default covered by the webapp's runtime classpath.

Upvotes: 0

esej
esej

Reputation: 3059

When you are compiling you are properly including all dependant libraries into the classpath.

The classpath used for execution is a totally different beast. You need to make sure the Servlet has access to the jars when it is being executed. How to do that depends on the container you are using. Since it is tomcat it should be easy to find out (search the web) how to add thirdparty libraries (pretty sure you can just dump the jar into common/lib directory).

Best would be to have a build process different from manual compiling of the servlet class. For example and IDE or maven - that would produce a war / web-application and deploy it to the servlet container (tomcat in your case). I do really really recommend doing this instead of manual compiling and deploying (once you get IDE-way to work and become more masterful at deployment you could go back to manual deployment)

Maybe search the internet for a tutorial on servlets and tomcat?

Upvotes: 1

Related Questions