java.lang.ClassNotFoundException: com.mysql.jdbc.Driver When run jar file

Posted by user1024858 on Stack Overflow See other posts from Stack Overflow or by user1024858
Published on 2012-09-28T03:18:06Z Indexed on 2012/09/28 3:37 UTC
Read the original article Hit count: 188

Filed under:

Hi all,

Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "test";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "admin";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();}

I run in eclipse it's ok, but i built to jar file and run on command line java -jar Test.jar i get this error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)

Please help me how to fix it.

Thanks!!!

© Stack Overflow or respective owner

Related posts about java