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

1
  • can you post the stack trace? how are you sure it's the mySql driver class that is not found? Commented May 31, 2012 at 16:10

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

3 Comments

This makes sense. I tried it but nothing changed. I made sure there was a newline in the manifest file.
Did you bundle the mysql-connector jar inside your jar?
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.
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.

1 Comment

Sorry typo in submission it is jdbc.Driver: that is why it worked in eclipse IDE.
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.

1 Comment

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.
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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.