0

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

Radu Murzea
  • 10,724
  • 10
  • 47
  • 69
Ephraim
  • 199
  • 1
  • 2
  • 18

4 Answers4

1

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

raddykrish
  • 1,866
  • 13
  • 15
  • This makes sense. I tried it but nothing changed. I made sure there was a newline in the manifest file. – Ephraim May 31 '12 at 18:30
  • Did you bundle the mysql-connector jar inside your jar? – raddykrish May 31 '12 at 21:59
  • No, The docs indicate not to. I placed it in the same directory as the jar.I opened the jar and checked the manifest was there and contained the correct jar name. – Ephraim Jun 01 '12 at 08:25
1

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.

FThompson
  • 28,352
  • 13
  • 60
  • 93
0

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

Community
  • 1
  • 1
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
  • IMHO it is better to build a correct manifest file and reference the libraries instead of building one über-jar which contains all external dependencies. – Mark Rotteveel May 31 '12 at 18:26
0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Thx. The class-path was not being copied into META-INF/MANIFEST.MF. It was in my Manifest.txt. Opening the .jar and your input resolved the issue. – Ephraim Jun 01 '12 at 08:49