2

I am writing a simple program in Java using to demonstrate insertion of data in MySQL table using JDBC. But the program is generating ClassNotFoundException while registering driver.

import java.sql.*;

public class test {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch(ClassNotFoundException e) {
            System.out.println("ClassNotFoundException: "+e.getMessage());
        }
    }    
}

I have added the driver JAR file in the same directory.

To compile the program:

javac -cp ".:~/Programs/D/friends/assignment/driver.jar;" test.java

To execute the program:

java -cp ".:~/Programs/D/friends/assignment/driver.jar;" test

O/p:

ClassNotFoundException: com.mysql.jdbc.Driver

enter image description here

  • 1
    `driver.jar` is the library for the MySQL JDBC stuff? And what reason has the trailing semicolon after "…/driver.jar"? – tquadrat Feb 05 '21 at 09:24
  • And you know that an explicit registration of the driver is not necessary when you use a current version of the JDBC driver? – tquadrat Feb 05 '21 at 09:27
  • @tquadrat Yeah right, it is the JDBC driver of MySQL and the trailing semicolon is for the reason that while setting classpaths we separate two different classpaths using a semicolon. And I know the fact that it is not necessary to register driver but I was doing so because while creating connection the code was raising exceptions. And so I explicitly registered driver to confirm if the driver is being registered or not. – Shashank Shekhar Feb 05 '21 at 14:29

2 Answers2

3

Note: The issue is caused by ; at the end of the driver.jar and also not using fully qualified path.

Windows based OS uses ; separator whereas Unix-based OS uses : separator.

Solution :

  1. First compile the code : javac test.java (Run this command)

  2. Run the code without semi-colon : java -cp .:<fully-qualified-path>/driver.jar test

Sample output :

anish@Anishs-MacBook-Pro ~ % javac Test.java
anish@Anishs-MacBook-Pro ~ % java -cp .:/Users/anish/driver.jar Test  
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

Note : I'm using mysql-connector-8.0.15.jar. If you are using the same or greater, then change from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver as that class is deprecated.

Anish B.
  • 9,111
  • 3
  • 21
  • 41
2

1.at the end of the classpath there seems to be an extra semi-colon:
/assignment/driver.jar;"
try without it

2. Are you sure driver.jar is the correct file?
normally they are called like mysql-connector-java-8.0.23.jar

  • I renamed that for my convenience. – Shashank Shekhar Feb 05 '21 at 14:31
  • And I tried without the semicolon and now it's raising a new exception. **Error: Could not find or load main class test Caused by: java.lang.ClassNotFoundException: test** – Shashank Shekhar Feb 05 '21 at 14:32
  • so it is a different problem/question - maybe add the directory to the package containing `test.class` to the classpath –  Feb 05 '21 at 15:09
  • 1
    and colon `:` is the separator for non-Windows systems; semi-colon `;` for Windows (since `:` is used for drive specification) –  Feb 05 '21 at 15:13
  • Actually, I didn't know that before. But now I got the concept of semi-colon separator. Thanks for sharing that. – Shashank Shekhar Feb 05 '21 at 15:58