Ephraim
Ephraim

Reputation: 319

ClassNotFoundException in MySQL Connector/J

This has been asked before but I cannot find the answer I need.

1) Using Class.forName("com.mysql.java.Driver") in the eclipse IDE all works well. I load the correct jar (mysql-connector-java-5.1.20-bin.jar), no exception.

When I create a jar for my app a1.jar and double click the jar, I get the ClassnotFoundException.

I created a .bat file in Windows XP with

java -classpath c:\temp\mysql-connector-java-5.1.20-bin.jar -jar c:\temp\a1.jar the app statrs with the same exception.

Furthermore using System.getProperty ("java.class.path") shows c:\temp\a1.jar whilst in the IDE I can see several directories

Upvotes: 0

Views: 955

Answers (4)

Mark Rotteveel
Mark Rotteveel

Reputation: 108961

If you are executing a file using -jar, then any external classpath (either provided on the commandline or the environment) is ignored. It only looks to the Class-path: entry in the META-INF/MANIFEST.MF file. If the MySQL jar isn't specified there, it won't load it.

Upvotes: 0

Paul Vargas
Paul Vargas

Reputation: 42020

Another thing you could do is put all the required libraries in a single file in an executable jar. The following response may be of great help:

Since Eclipse 3.5, you could also use the eclipse wizard to export an executable jar.

Use File > Export... and select Runnable JAR file. The Runnable Jar Export window has a radio button where you can choose to Extract or to Package the required libraries into genratated jar.

Export Runnable JAR File Export

Upvotes: 0

FThompson
FThompson

Reputation: 28687

The driver is com.mysql.jdbc.Driver, not com.mysql.java.Driver.

You receive the ClassNotFoundException, because there is no com.mysql.java.Driver class in the Connector/J library.

Upvotes: 1

raddykrish
raddykrish

Reputation: 1866

When you are running an application from the jar then you may need to check the manifest file way of adding the classpath dependencies. Take a look at this Manifest Classpath

Upvotes: 1

Related Questions