Hi all,

I'm stumped and can't seem to find an answer on Google. I am running a java application through Eclipse and it connects to mysql. It connects fine if I run it through Eclipse but if I make a standalone jar file and then run it through Window's Vista command line then i get this error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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)
at database.Connect.ConnectDB(Connect.java:16)

I have downloaded mysql-connector-java-5.1.16-bin.jar file and placed it in the classpath and path environment variables.

I have also included it as an external jar file in the libraries section of Eclipse->Project->Properties->Java Build Path and also in Window->Preferences->Classpath Variables

But I still get the above error. This is my connect code:

package database;

import java.sql.Connection;
import java.sql.DriverManager;

public class Connect {
	public Connection ConnectDB()
    {
        Connection conn = null;

        try
        {
        	String userName = "root";
            String password = "planet";
            String url = "jdbc:mysql://localhost:3306/test";
            Class.forName ("com.mysql.jdbc.Driver").newInstance(); 
            conn = DriverManager.getConnection (url, userName, password);
            System.out.println ("Database connection established");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return (conn);
    }
}

Please help. Thanks so much :)

It's probably because you have the mysql.jar configured in your classpath for Eclipse (that's part of the process when you set up a datasource in the Eclipse database explorer, for example), but it's not in the classpath in your windows environment. You can set the CLASSPATH env variable to include the location of the mysql.jar file, or use the -cp (or -classpath) option with your java command to indicate the location of the mysql.jar file.

Hi dononelson,

As I mentioned in my post above I have set my classpath environment variable to include the mysql connector jar. But I still get the following error.

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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)
at database.Connect.ConnectDB(Connect.java:16)


I have also included the classpath when running my program: java -jar -classpath "C:\Program Files\mysql-connector-java-5.1.
16\mysql-connector-java-5.1.16-bin.jar" Server.jar

and I still get the same above error.

In any case I'd like a workaround where I don't have to set the classpath each time I run the jar file.

Any ideas? Or any other takers?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.