3

working on a JSF application and accessing a page which uses a bean to check some credentials against a database to authenticate a user.

I'm getting a Tomcat error when the page is loaded.

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:188)
DB.init(DB.java:27)
Login.<init>(Login.java:19)

Full log here

I've downloaded the mysql-connector-java-5.1.24-bin.jar and added it to the build path. I'm loading my details in form a properties file that looks like this:

jdbc.url=jdbc:mysql://127.0.0.1:3306
jdbc.user=root
jdbc.password=
jdbc.driver=com.mysql.jdbc.Driver

I can do an import com.mysql.jdbc.Driver in my Login or DB file with no problems, so I don't understand why the web app is having trouble loading the class.

Here's the DB.java file:

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DB {

private static String url;
private static String username;
private static String password;

public static void init() throws IOException, ClassNotFoundException{

    Properties props = new Properties();
    FileInputStream in = new FileInputStream("/home/dan/workspace/DeutschAkademie/WebContent/db.prop");
    props.load(in);

    String driver = props.getProperty("jdbc.driver");
    url = props.getProperty("jdbc.url");
    username = props.getProperty("jdbc.username");
    password = props.getProperty("jdbc.password");

    if(username == null) username = "";
    if(password == null) password = "";
    Class.forName(driver);

}

public static Connection getConnection () throws SQLException {
    return DriverManager.getConnection(url, username, password);
}

}

My project directory:

 .
├── build
│   └── classes
│       ├── DB.class
│       ├── Login.class
│       └── User.class
├── database.sql
├── mysql-connector-java-5.1.24-bin.jar
├── src
│   ├── DB.java
│   ├── Login.java
│   └── User.java
└── WebContent
    ├── add-user.xhtml
    ├── css
    │   ├── bootstrap.min.css
    │   ├── button.css
    │   ├── font-awesome.css
    │   ├── font-awesome-ie7.min.css
    │   ├── font-awesome.min.css
    │   ├── global.css
    │   └── typography.css
    ├── css.xhtml
    ├── db.prop
    ├── font
    ├── history.xhtml
    ├── home.xhtml
    ├── login.xhtml
    ├── META-INF
    │   └── MANIFEST.MF
    ├── navbar.xhtml
    ├── test.xhtml
    ├── WEB-INF
    │   ├── faces-config.xml
    │   ├── inc
    │   ├── lib
    │   │   ├── javax.faces-2.1.9.jar
    │   │   └── mysql-connector-java-5.1.24-bin.jar
    │   └── web.xml
    └── words.xhtml
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Dan Prince
  • 29,491
  • 13
  • 89
  • 120
  • Make sure your MySQL jar is in *WEB-INF/lib* folder. Before re-deploying the application, clear the tomcat *webapps* folder and delete the *temp* folder, then redeploy your app. – Luiggi Mendoza Apr 29 '13 at 00:15
  • I've got it in that directory as well. – Dan Prince Apr 29 '13 at 00:18
  • 2
    Make sure that the jar is there when generating the WAR file to be deployed. The problem looks like your Tomcat is not replacing the last WAR when deploying your app. That's why I also said you to clean your Tomcat deploy folder. – Luiggi Mendoza Apr 29 '13 at 00:19
  • One more thing: This problem is not related with JSF nor Java EE at all. – Luiggi Mendoza Apr 29 '13 at 00:22
  • I cleaned those directories and now the pages won't load at all. Here's the server startup logs. http://paste2.org/IAtFkDh8 – Dan Prince Apr 29 '13 at 00:48
  • First thing to do when working with Tomcat on Eclipse: [change the deploy folder to Tomcat's installation path](http://stackoverflow.com/q/1012378/1065197). After doing this, shutdown your Tomcat, restart it and deploy your app. – Luiggi Mendoza Apr 29 '13 at 04:32

3 Answers3

5

If you're confident the driver's jar file is already placed on $TOMCAT_HOME/lib or myapp/WEB-INF/lib, other cause of the problem is timing issue. Maybe when your init() method runs not all of the classes have been loaded.

To proof this point, can you test loading the driver class at some other time (eg: create a dummy test servlet that loads the driver class). If it works then it is a timing issue

gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • Even if there was a timing issue, how would I fix it? – Dan Prince Apr 29 '13 at 00:24
  • Initialize your driver at some other time, don't use static method of class. You can create a servlet context listener on context initialized event (http://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/), or even try install the datasource on JNDI such that it is created when tomcat starts -- not when your app deploys (http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html) – gerrytan Apr 29 '13 at 00:29
  • By "Initialize your drive at some other time", do you mean something like *Class.forName("com.mysql.jdbc.Driver").newInstance();* ? – user2813274 Apr 15 '15 at 18:47
0

See this answer: How to configure Tomcat to connect with MySQL

You are adding the driver to the build path, which is needed for building, but wont help when it comes to runtime.

You can put it in Tomcat/lib or YourApp/WEB-INF/lib

Community
  • 1
  • 1
Kevin
  • 4,070
  • 4
  • 45
  • 67
0

I was also facing the similar issue but on copying the MySql Connector jar file to my lib folder things worked for me. Thanks!