Hi everyone,
I am using Ubuntu 7.10, and I have Java and MySQL installed with the command line as follow:

1. sudo apt-get install sun-java6-jdk
2. sudo apt-get install mysql-server

and my JAVA and MySQL are working well, but now I want create connection between JAVA application and MySQL using the JDBC API not using tomcat, but when I run my file, I get the following error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at DB.dbConnect(getmySQLConnection.java:23)
at getmySQLConnection.main(getmySQLConnection.java:8)


it is about two days I am searchind on the web, plz some one help me

Dani AI

Generated

Short answer: that stack trace means the MySQL JDBC driver (Connector/J) is not available to the JVM at runtime — the class com.mysql.jdbc.Driver (or its modern name) must be in the application classpath. was right to point to the connector JAR and ’s follow-up about using the actual JAR filename is the usual practical fix. (dev.mysql.com)

Two quick, reliable fixes: either install the distro package that provides Connector/J (Ubuntu commonly exposes it via libmysql-java, which puts a JAR under /usr/share/java) or download the official Connector/J binary from MySQL and add that JAR to your app’s runtime classpath. Run the JVM with the connector JAR on -cp rather than relying on a global environment CLASSPATH for one-off runs; for example:

java -cp .:/usr/share/java/mysql-connector-java.jar com.example.Main

(Adjust the path to the exact JAR the package installed or the one you downloaded.) (help.ubuntu.com)

If your project uses Maven/Gradle, add Connector/J as a dependency instead of fiddling with environment variables. Maven coordinates are provided by Oracle/MySQL (groupId com.mysql, artifactId mysql-connector-j); that puts the driver on the classpath for builds and runs managed by the tool. Example Maven snippet:

<dependency>
  <groupId>com.mysql</groupId>
  <artifactId>mysql-connector-j</artifactId>
  <version>8.0.x</version>
</dependency>

Check MySQL’s download and Maven docs for the current versions. (dev.mysql.com)

Notes and troubleshooting: modern Connector/J uses com.mysql.cj.jdbc.Driver (the old com.mysql.jdbc.Driver is deprecated), and JDBC 4+ usually auto-registers drivers via META-INF/services/java.sql.Driver so Class.forName() is often unnecessary — but using it with the correct class name still works. If problems continue, verify the JAR path actually exists, check file permissions, ensure the process launching your app inherits the environment, and print System.getProperty("java.class.path") from your program to see what the JVM actually sees. (dev.mysql.com)

Recommended Answers

All 10 Replies

if you're really spent 2 days searching for an answer to this and haven't found it you're extremely incompetent.
This question must have been asked by kids who don't know what they're doing, how to set a classpath, and how to READ MANUALS a million times.

IOW, Read The F***ing Manual and actually learn Java before you attempt to do things more complicated than printing "Hello World" on the console.

commented: Why waste your time actually criticizing someone for asking a question? +0

You man!!!!!!!!!!!!!!!! after spending about 1 hour and more, instead of posting such stupid words, post the solution, if you can. obviously I don't know, because of this I posted here...

first thanks for replying:

for further info:

1. I download the following file:
mysql-connector-java-5.0.8.

2. Then I used the following command:
tar -ztvf mysql-connector-java-5.0.8.

3. then I went to opt directory like below:
cd /opt

4. Then I used the following command:
tar -zxvf /home/sherzad/Desktop/mysql-connector-java-5.0.8.

5. Then I opened the file like beneath:
gedit ~/.bashrc

6. and finally I added the following two lines:

CLASSPATH=$CLASSPATH:/usr/share/java/mysql.jar
export CLASSPATH

I dont know much of ubuntu, plz be a little in detail, Thanks

finally, I found the solution, just the CLASSPATH must the name of the jar file, not mysql.jar.

Hi Tanha,

Thank you for this posting - I was having the same problem until I realized that mysql is not released with a JDBC driver: as your procedure illustrates, this must be downloaded separately.

Don't let the haters get you down - your post was very useful to me, and I'm sure to others as well.

Don't let the haters get you down - your post was very useful to me, and I'm sure to others as well.

I'm sure that in the six months it has been since they created this thread, they have gotten over any negative comments.

I'm sure that in the six months it has been since they created this thread, they have gotten over any negative comments.

you never know ... maybe he 's the kind of person that keeps a grudge :/

you never know ... maybe he 's the kind of person that keeps a grudge :/

I think the major issue is for those of us finding these threads through Google, we don't want to read someone's rude remark to an honest question. If someone has a solution but would rather just insult the OP, maybe they should go take a dump in their own coffee and drink it. I, on the other hand, would just like to see a solution.

I think the major issue is for those of us finding these threads through Google, we don't want to read someone's rude remark to an honest question. If someone has a solution but would rather just insult the OP, maybe they should go take a dump in their own coffee and drink it. I, on the other hand, would just like to see a solution.

You can
A) have look on setting up environment
B) create new thread and explain in detail what exactly is problem (not knowing how to install, getting some errors, etc)

Thread closed

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.