[SOLVED] ClassNotFoundException: com.mysql.jdbc.Driver
Dear reader,
I hope I picked the correct board for this topic, I know how much weight some people can place on that, if not, I' m sorry.
I started learning java 2 weeks ago and have been playing around with pircbot.
My application works fine when I run it in eclipse, but when I try to run it from the terminal I get the following error message:
Code:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at javaRot.bot.db.<init>(db.java:15)
at javaRot.bot.javaRotMain.main(javaRotMain.java:7)
Some more info on me:
As you know a am beginner with java.
The same goes for mysql and JDBC, which I all have been playing around with for 2 weeks now.
I am running ubuntu linux, which I am a little more experienced with. I have been using it for a year now. And recently installed 11.04.
Versions:
Eclipse SDK - Version: 3.5.2
JDBC - mysql-connector-java-5.1.16
Java - JDK Java 6 runtime
As suggested I searched the web and this forum on solutions before posting (about 1 hours last night and 2 hours today) and I did find some solutions. Steps taken:
- In eclipse, I already had the added connector in the (Java build path > libraries) (I tried adding them as external and internal jars, ofc not both at the same time), but I found out I should have also added it to the Classpath, so I rightclicked my project in eclipse and picked: Run as> Run Configurations > Classpath. Under User entries I added the connector jar (again I tried it first as internal and later as external jar)
- Copied "mysql-connector-java-5.1.16-bin.jar" to "/usr/share/java"
- Installed libmysql-java on ubuntu
- set the classpath in the ubuntu terminal: "CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar", but I also tried the connector I downloaded and copied there: "CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.16-bin.jar" and the connector I downloaded in it's original location (before I copied the connector to the "/usr/share/java dir") "CLASSPATH=$CLASSPATH:/media/MultiSave/colocations/locahost/java/JavaRot/lib/mysql-connector-java-5.1.16-bin.jar"
Additional info:
I saw you can check if you set the classpath correctly by typing "java com.mysql.jdbc.Driver" in the terminal.
First the result was something like not found or unable to find and now the result is:
Code:
mah@mah-Domestique://media/MultiSave/Colocations/localhost$ java com.mysql.jdbc.Driver
Exception in thread "main" java.lang.NoSuchMethodError: main
So that tells me the classpath is correct, but then again it' s not as when I try to run my app ("java -jar JavaRot.jar" I get the same old:
Code:
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at javaRot.bot.db.<init>(db.java:15)
at javaRot.bot.javaRotMain.main(javaRotMain.java:7)
Now I know there are countless articles about this and I could spend another week reading them all trying to find my solution, so my request is, please point me toward the documentation I need so I can read up an how to properly get my application set up to run from the terminal.
I am pretty sure that my application code is fine, since it does work properly when it's run from eclipse, but here are the 2 classes you will be intrested in:
javaRotMain.java
Code:
package javaRot.bot]
import java.sql.ResultSet;
public class javaRotMain {
public static void main(String[] args) throws Exception {
db dbAction = new db(); // Line 7
ResultSet settingsResult = dbAction.readAll("botSettings");
while (settingsResult.next()) {
String network = settingsResult.getString ("network");
String url = settingsResult.getString ("url");
String nick = settingsResult.getString ("nick");
String ident = settingsResult.getString ("ident");
String homeChan = settingsResult.getString ("homeChan");
String netChan = settingsResult.getString ("netChan");
int connects = settingsResult.getInt ("connects");
String lastConnect = settingsResult.getString ("lastConnect");
javaRot RotBot = new javaRot(url, network, nick, ident, homeChan, netChan, connects, lastConnect); // TOADD homePass, netPass
}
System.out.println("''''''''''''' OK JavaRot: Bot started");
}
}
name=db.java
Code:
package javaRot.bot;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class db {
private Connection con;
public db() throws ClassNotFoundException, SQLException {
if (con == null) {
System.out.println("||||||||||||| JDBC: Not connected, connecting.");
String dbUrl = "jdbc:mysql://localhost/tbk_bot";
Class.forName("com.mysql.jdbc.Driver"); // Line 15
con = DriverManager.getConnection(dbUrl,"user", "pass");
}
}
public ResultSet readAll (String table) throws SQLException {
// Kan efficienter 2 regels op 1.
System.out.println("||||||||||||| SELECT * FROM `" + table + "`;");
ResultSet result = null;
PreparedStatement statement = con.prepareStatement("SELECT * FROM `" + table + "`;");
result = statement.executeQuery();
return result;
}
public ResultSet read (String table, String variable, String value) throws SQLException {
System.out.println("||||||||||||| SELECT * FROM `" + table + "` WHERE `" + variable + "` = '" + value + "';");
ResultSet result = null;
PreparedStatement statement = con.prepareStatement("SELECT * FROM `" + table + "` WHERE `" + variable + "` = '" + value + "';");
result = statement.executeQuery();
return result;
}
public void write (String action, String table, String values) throws SQLException {
System.out.println("||||||||||||| " + action + " `" + table + "` SET " + values + ";");
java.sql.Statement st = con.createStatement(); //(action + " " + table + " SET " + values + ";");
int val = st.executeUpdate(action + " `" + table + "` SET " + values + ";");
}
}
Thanks in advance,
Maarten Alberic 'tBKwtWS'.