1

I am getting well-known NoClassDefFoundError: com/mysql/jdbc/StringUtils, but my case is little different: I am not using MySQL at all.

I'm working with PostgreSQL 9.1 database using JDK 1.6. On my computer everything works perfectly (PostgreSQL driver is loaded, no classpath issues etc.), but I have to upload my project to my University's servers, where I am getting the following error:

java.lang.NoClassDefFoundError: Could not initialize class com.mysql.jdbc.StringUtils

Here is a full stack trace:

com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:286)
java.sql.DriverManager.getConnection(DriverManager.java:582)
java.sql.DriverManager.getConnection(DriverManager.java:185)
elections.ConnectionManager.getConnection(ConnectionManager.java:29)
elections.daos.LoginDAO.login(LoginDAO.java:23) 
elections.controllers.LoginController.doPost(LoginController.java:46) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:641) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
sun.reflect.GeneratedMethodAccessor35.invoke(Unknown Source) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
java.lang.reflect.Method.invoke(Method.java:597) 
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:274) 
org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:271) 
java.security.AccessController.doPrivileged(Native Method) 
javax.security.auth.Subject.doAsPrivileged(Subject.java:517) 
org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:306) 
org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:166)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:299) 
org.apache.catalina.core.ApplicationFilterChain.access$000(ApplicationFilterChain.java:57) 
org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:193) 
org.apache.catalina.core.ApplicationFilterChain$1.run(ApplicationFilterChain.java:189) 
java.security.AccessController.doPrivileged(Native Method) 
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188) 
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:208) 
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) 
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
org.apache.coyote.ajp.AjpAprProcessor.process(AjpAprProcessor.java:197) 
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579) 
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1770) 
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) 
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) 
java.lang.Thread.run(Thread.java:662)

Here's my class which provides connection (It's my first JDBC project; I know that code below is not threadsafe; I'm going to rewrite it using Connection Pooling, but first I need to get it to work...)

static Connection conn;

public static Connection getConnection() throws Exception {
    try {

        String url = "jdbc:postgresql://host/database";
        String username = "username";
        String pwd = "password";

        Class.forName("org.postgresql.Driver");

        try {
            conn = DriverManager.getConnection(url, username, pwd);
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        }
    } catch (ClassNotFoundException e) {
        throw e;
    }
    return conn;
}

I did huge search on the internet but I seem to be the only man in the world with such issue... Everybody else has this problem with MySQL only.

I have even tried to put MySQL driver (containing the lacking com.mysql.jdbc.StringUtils class) into WEB-INF/lib folder, but this didn't help.

Only useful information I have found is that probably error is hidden somewhere else (that means, some class that com.mysql.jdbc.StringUtils depends on couldn;t be loaded), but I don't know where to search.

I really don't understand why MySQL classes are searched for, while I use PostgreSQL only.

Any kind of help would be appreciated! :)

4
  • Did you checked that on your university server wasn't some application installed with the same name, earlier? Try to clean work directory (if it is tomcat server), and try to start it again. Commented Jan 20, 2013 at 19:24
  • I am not able to clean work directory because I don't have access to Tomcat folders. Only thing I am able to do is to upload .WAR files which Tomcat deploys using AutoDeploy. I cannot check but I doubt that anyone else installed anything there, because it requires administrator privilleges. To install my app I had to ask administator for special setup, it wasn't possible by default. Commented Jan 20, 2013 at 19:33
  • 1
    In your case it would be more constructive to ask the administrator of the server at your University. If you say that on your local machine there are no problems running your app, then it is obvious that the problem is with your University server. Commented Jan 20, 2013 at 22:13
  • This is what I'm going to do tomorrow :) I just thought that maybe it's something that I can solve by myself. Commented Jan 20, 2013 at 23:11

3 Answers 3

1

JDBC 4.0 auto-discovery can scan and load JDBC drivers automatically, I guess that DriverManager scan and load MySQL JDBC driver too even if it is never used by your program.

then we can guess that your server kept many jdbc drivers for popular database vendors. you may test with mysql locally to find out why MySQL driver can't work, then you can tell server admin to update drivers or settings for mysql.

http://www.techfaq360.com/tutorial/jdbc/jdbc3_jdbc4.jsp

Sign up to request clarification or add additional context in comments.

3 Comments

Wow, nice! :) that pretty much explains why i get MySQL-related errors. I'm gonna dig into that.
Link says that it should work without calling Class.forName at all, but it doesn't. My driver is JBDC4, double checked.
Do you use JDK 6+? only DriverManager of JDBC 4.0 is aware of existence of /META-INF/services/java.sql.Driver and load class mentioned in the file.
1

Ok, It turned out to be permissions issue with Tomcat Security Manager. As soon as admin will examine what the exact reason was, I'll post it here and accept the best answer.

Comments

0

Usually, JDBC drivers are placed in Tomcat SHARED libs. So it might be that the tomcat you deploy on has some libs here that mess up with your project.

Adding The missing lib in your project's WEB-INF/lib cannot solve the problem because if the code that cause you trouble is trying to use the missing class from another class loader, it might be unable to see what you try to provide from your app "scope".

So, as I assume you don't have access to the shared libs of your server, you should see this with you sys. admin.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.