简体   繁体   中英

java.lang.ClassNotFoundException while creating an Instance of Class from Servlet's doGet()

I am relatively new to Java Servlets. I am trying create an instance of a class named HospitalFinder from the doGet() method in my MedicareLocatorServlet. The class HospitalFinder uses external Libs. When I initiate a get request form my webpage, the servlet throws an ClassNotFoundException. Here are the Exception Details:

SEVERE: Servlet.service() for servlet [MedicareLocatorServlet] in context with path [/Medicare] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.openrdf.sail.Sail
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
    at testclass.getHFInstance(testclass.java:6)
    at MedicareLocatorServlet.doGet(MedicareLocatorServlet.java:53)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

When I try to create the instance of HospitalFinder from another class, in the same package, there is no exception thrown. I tried to search for ways to handle classNotFoundException but nothing seems to work. PS - I am using Eclipse dynamic web project. Can someone help me with this?

First , you should have a clear understanding of how a servlet project is deployed and run by eclipse

1.eclipse will "compile" all the source code into several .class files.(Maybe it is not very definite to use compile because java is an interpreted language)

2.eclipse will put the .class file into the corresponding directory which can be used by tomcat.

3.You send a http get request from your browser ,you tomcat will resolve you request and redirect your request to your MedicareLocatorServlet.class to deal with and then send back the http response.

So,you should check that if your eclipse have already compiled the class and also put it into your server webapps directory. In default,linux eclipse will put the class file in the directory: workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/webapps .workspace is your eclipse workspace.So ,you should cd to this directory and check if ".class" files is ready.If not,you should build your project and deploy it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM