0

I created a simple MySQL java program named, DBDemo.java. This class is in the package named dbdemo.

I put the mysql-connector-java.jar file under a directory named lib, so I run the program as:

java -classpath ./lib/mysql-connector-java.jar;. dbdemo.DBDemo

It works with no problem.

Now, I created my program into a JAR file, named DBDemo.jar. So, I tried:

java -classpath ./lib/mysql-connector-java.jar;. -jar DBDemo.jar

But, an errors comes as:

SQLException on database creation: No suitable driver found for jdbc:mysql://localhost:3306/

It seems that the mysql-connector-java.jar in -classpath works with my DBDemo.class file program, however, the -classpath is not accessible if I try to run my DBDemo.jar file.

How can I make my -classpath accessible with my DBDemo.jar program?

I am using Java 15 on Windows 10.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
KBee
  • 87
  • 1
  • 5
  • When you use `-jar`, the class path is fully defined by the `Class-Path` entry in the manifest of the JAR. Any value set through `-classpath` is ignored. Either use `java -classpath ./lib/mysql-connector-java.jar;DBDemo.jar dbdemo.DBDemo`, or make sure your JAR contains the right `Class-Path` field in its manifest. – Mark Rotteveel Oct 17 '21 at 13:24
  • Thank you, Mark Rotteveel. Your suggestion works. However, is there a link to how to define Class-Path entry in the manifest of a JAR? – KBee Oct 22 '21 at 12:53
  • That depends on how you create the JAR. Build tools like Gradle and Maven have features built-in for this. – Mark Rotteveel Oct 22 '21 at 14:27

0 Answers0