1

I am trying to run a web application that gets a specific length of numbers from a user and checks that against a database (so if the user made a typo, it will makes suggestions as to what key the user actually meant). The problem I am running into is the application won't connect to the database, I get this error:


java.lang.NullPointerException
at Validator.Validate.checkIDs(Validate.java:410)
at Validator.Validate.getAllValidIds(Validate.java:203)
at Python.WebServlet.processRequest(WebServlet.java:70)
at Python.WebServlet.doPost(WebServlet.java:247)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1805)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)


Line 410 in Validator.Validate is:

Statement stmt = Conn.createStatement();

Where Conn is a java.sql.Connection variable

Line 203 is a call to the method that does the ^ statement


java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at Validator.Validate.dbConnect(Validate.java:394)
at Validator.Validate.getAllValidIds(Validate.java:193)
at Python.WebServlet.processRequest(WebServlet.java:70)
at Python.WebServlet.doPost(WebServlet.java:247)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1805)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)


This is the error I get on my server, if I run it on my local machine it works just fine (it validates the ID). What could be the problem here? Cannot it not locate the com.mysql.jdbc.Driver class? Is that class the issue?

Thanks for the advice/help in advance!

NOTE:

The Validator class is an external jar that is in my class path. It calls on another jar in its class path the contains the com.mysql.jdbc classes, as you can see in the following pictures

Libraries currently in my netbeans project


The BugValidator jar contains the mysql-connector jar which has the Drivers class

Contents of the BugValidator jar


Contents of mysql-connector-java-5.1.7-bin.jar

Shahab
  • 1,977
  • 6
  • 24
  • 28
  • 2
    Have you included the mySQL JDBC driver in your classpath? – Alex Mar 20 '12 at 17:42
  • The validator class is an external jar and it contains another jar that has the mySQL JDBC driver class in the com.mysql.jdbc folder – Shahab Mar 20 '12 at 17:47
  • Nested jar files are not included in your classpath. So if you are including `BugValidator.jar`, the jars inside of it will *not* be in the classpath. Make sure that on the server the JDBC driver jar is explicitly in the classpath – Alex Mar 20 '12 at 18:10
  • The BugValidator contain a file called ".classpath" and has this in it: , would that be an explicit declaration – Shahab Mar 20 '12 at 18:14
  • You need to extract `mysql-connector-java-5.1.7-bin.jar` and place it the `lib` directory alongside `BugValidator.jar` – Alex Mar 20 '12 at 18:32

4 Answers4

5

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

You'll have to include the MySQL JDBC-Driver in your server's classpath.
Either by publishing it with your application or putting it in your server's lib directory.

Hope this helps. Have Fun!

Steven Bluen
  • 141
  • 2
  • 11
SimonSez
  • 7,399
  • 1
  • 30
  • 35
  • That won't work without custom code. See: http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html - relevant quote is: "The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path." – Jon Mar 20 '12 at 18:52
  • @Jon You're absolutely right! But as shown in the attached images it seems that the driver is already in the applications `lib` directoy. @Shahab can you verify that the driver is published to the server? Have a look in the application folder on the server machine and make sure you've exported the driver-jar from your root classpath (not the one located inside *BugValidator.jar*. Cheers! – SimonSez Mar 20 '12 at 19:51
  • Ya it exporting it to the application folder seems to have worked.... @Jon, changing it from a JAR within a JAR seems to have worked – Shahab Mar 20 '12 at 20:21
2

From this:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

It's pretty clear that you don't have the MySQL driver on your ClassPath. Somewhere in your project, you should have the jar that contains the MySQL driver. If you don't, then you either need to download it to your lib directory, or set up a Maven dependency to it, depending on how your projects dependencies are being handled.

EDIT: In response to your update. All your screenshot shows is that everything is set up fine for compilation within your IDE. But we still don't know where you are running your application from that's giving you this stack trace.

Are you running your app from inside your IDE and getting the stack trace from the console? Are you running it after it's been packaged and seeing the stack trace in some logs?

Jon
  • 3,510
  • 6
  • 27
  • 32
  • The error doesn't arise when I run it in the IDE, only when I package it into a war and send it of the my apache server.... I don't know if it has been but I check in the Bug Validator class and it contain: , is that sufficent? – Shahab Mar 20 '12 at 18:21
  • Then that suggests that something is wrong with your .war packaging process. Are you sure the .jar containing the driver is included within the .war you deploy? Have you unzipped the .war in a temporary directory to see what it contains? – Jon Mar 20 '12 at 18:23
  • Ah, I think I understand what you've done wrong now. Read this: http://stackoverflow.com/questions/183292/classpath-including-jar-within-a-jar - you're attempting to include a jar within a jar, within a war, is that right? – Jon Mar 20 '12 at 18:29
  • Assuming that I've guessed right - that isn't going to work unless you use a custom ClassLoader. Your BugValidator jar file can contain a classpath reference to the MySQL driver jar, but it can't wrap it as well - it has to refer to an external MySQL driver jar file. So, it looks like we've found your problem. When you package your war, you need to include the MySQL driver jar file directly within the war, not within the BugValidator jar file. – Jon Mar 20 '12 at 18:33
1

Did you load the driver class??

Class.forName("com.mysql.jdbc.Driver"); 

i.e.:

Class.forName("com.mysql.jdbc.Driver"); 
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/karthicraj","mysql","mysql"); 
stm=con.createStatement(); 
String strQry="update sriram set age=?,salary=? where name=? and age=?"; 

Extracted from: http://forums.mysql.com/read.php?60,42473,275468#msg-275468

Imaky
  • 1,227
  • 1
  • 16
  • 36
  • yes I did: Class.forName(mysqldriver); which is com.mysql.jdbc.Driver, but thank you for the suggestion – Shahab Mar 20 '12 at 18:38
0

The JDBC driver needs to go in the webapp's runtime classpath, not in some arbitrary /lib folder, let alone embedded in a different JAR file (that would also not work in normal Java applications by the way).

Put the libraries which the webapp requires during runtime individually in webapp's /WEB-INF/lib folder.


Unrelated to the concrete problem, please use package names according Java naming conventions.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555