2

I am trying to build a login-registration web app which essentially lets users register on a database and then allows them to log in. I have written all the code, and I am trying to connect my program to my database. Here is the error I receive:

Exception

javax.servlet.ServletException: Servlet execution threw an exception
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Root Cause

java.lang.NoClassDefFoundError: java/sql/Driver

I have added all the Postgres, Tomcat, and SQL connector/driver jar to my classpath and my WEB-INF/lib folders. I am not really sure what else to do. I have tried testing with another class and I am able to connect to my database. I have triple check my connection URL as well. What's wrong?

Thanks!

Matteo Lunghi
  • 21
  • 1
  • 3
  • Strange. This is usually due to missing jar in classpath. Can you please provide bit more detailed stacktrace? – Shweta Gupta Jun 24 '19 at 10:56
  • Yeah! Thanks for your reply! SEVERE: Servlet.service() for servlet [login.submit.register.LoginRegister] in context with path [/RegistrationPage] threw exception [Servlet execution threw an exception] with root cause java.lang.NoClassDefFoundError: java/sql/Driver at java.base/java.lang.ClassLoader.findBootstrapClass(Native Method) at java.base/java.lang.ClassLoader.findBootstrapClassOrNull(ClassLoader.java:1257) – Matteo Lunghi Jun 24 '19 at 11:00
  • I have the postgres and mysql connectors jars - am I missing any? – Matteo Lunghi Jun 24 '19 at 11:02
  • Please see https://stackoverflow.com/questions/17484764/java-lang-classnotfoundexception-com-mysql-jdbc-driver-in-eclipse – TechFree Jun 24 '19 at 11:06
  • I am using postgres, not mysql, so my link is "org.postgres.Driver", and I have already added the driver jars to the project. What else can I do? – Matteo Lunghi Jun 24 '19 at 11:08
  • `java.sql.Driver` is part of the JDK, seems like your Java installation is broken –  Feb 12 '20 at 13:23
  • NoClassDefFound indicates that the class was found but could not be initialised. Sounds like a URL or password problem, or the Tomcat server does not have access to the DB server. – kiwiron Oct 28 '22 at 20:29

3 Answers3

3

If you are getting this problem while running Jetpack compose desktop app, consider adding this line into build.gradle.kts

compose.desktop {
    application {
        // ..
        nativeDistributions {
            // ...
            modules("java.sql")
        }
    }
}
philoopher97
  • 772
  • 1
  • 6
  • 18
0

I know this has nothing to do with Servlets, but I had the same problem with a module application that uses SQLite:

Exception in thread "main" java.lang.NoClassDefFoundError: java/sql/Driver
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1095)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:206)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:759)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassInModuleOrNull(BuiltinClassLoader.java:680)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:605)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:315)
...
Caused by: java.lang.ClassNotFoundException: java.sql.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 14 more

A little puzzling at at first, but I finally figured it out. I just had to add the requires statement for java.sql to the module-info.java:

module mymodule {
    ...
    requires sqlite.jdbc;
    requires java.sql;
}

(The requires sqlite.jdbc was already in there, added it above just for clarity)

pinkston00
  • 168
  • 9
0

For Kotlin Compose Multiplatform this seems to fall in the category "It's a feature and not a bug". philoopher97 posted already the solution.

See here for a complete explanation including a hint on how to configure Proguard.

Nantoka
  • 4,174
  • 1
  • 33
  • 36