Reputation: 29989
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)
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
Upvotes: 3
Views: 9980
Reputation: 1
I was also facing the similar issue but on copying the MySql Connector jar file to my lib folder things worked for me. Thanks!
Upvotes: 0
Reputation: 41123
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
Upvotes: 5
Reputation: 4128
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
Upvotes: 0