简体   繁体   中英

java.lang.NoClassDefFoundError: org/skife/jdbi/v2/DBI

I created a project locally on my computer using JDBI and it interfaces with database without any problems.

I am now using the same project on a Tomcat server. I am compiling my code using javac -cp "lib/*" cs5530/*.java where lib contains the external jars I am relying on like, jdbi-2.49.jar.

This code is where I create a new DBI.

public static DAO getDbConnection() {
    MysqlDataSource ds = new MysqlDataSource();
    ds.setServerName("server");
    ds.setUser("user");
    ds.setPassword("password");
    ds.setDatabaseName("dbName");
    DBI dbi = new DBI(ds);
    return dbi.onDemand(DAO.class);
}

It called in my class constructors:

public Customer() {
    this.conn = getDbConnection();
}

This is the error I get:

java.lang.NoClassDefFoundError: org/skife/jdbi/v2/DBI
    cs5530.Customer.getDbConnection(Customer.java:85)
    cs5530.Customer.<init>(Customer.java:31)
    org.apache.jsp._1_jsp._jspService(_1_jsp.java:105)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

So why would it compile fine seeing the necessary classes and then bork at runtime and how would I fix this?

Thanks.

The NoClassDefFoundError happens when the JVM can't find a matching class in its classpath. It's not finding your jdbi library. At compile time you're explicitly telling javac where to find the library, but at runtime Tomcat needs to be able to find it, and that means putting it where Tomcat knows to look for it.

Typically Tomcat expects to find Java code organized as separate web applications under a webapps folder, so that the application jars would go under

 $CATALINA_HOME/webapps/whateveryourwebappnameis/WEB-INF/lib

and the class files for the application would go under

 $CATALINA_HOME/webapps/whateveryourwebappnameis/WEB-INF/classes

public_html seems to me like a strange place to put Java class files. But if what you have is otherwise working for you, move the lib folder to directly under WEB-INF.

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