1

I am trying to make a application in java which connects to a mysql database. For this purpose I downloaded the mysql-connector-java-5.1.16-bin.jar connector.

I also added the path of the above jar file in the CLASSPATH environment variable.

I created the application and executed the java files and it executed properly.

Then I created a jar file of the application and tried to execute it then i got the following error

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at CanteenCounter.refresh(CanteenCounter.java:73)
    at CanteenCounter$1.actionPerformed(CanteenCounter.java:30)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

Exception: com.mysql.jdbc.Driver

I think the jar file is not able to find the com.mysql.jdbc.Driver

Please help!!!

Jonah
  • 2,097
  • 4
  • 20
  • 22

2 Answers2

2

Generally you do not want to use the CLASSPATH variable but rather the -cp flag to specify library location.

java  -cp /path/to/jar:/path/to/other/jar Main
zellio
  • 31,308
  • 1
  • 42
  • 61
  • but what if I want to create a executable jar file. I wanna open it witha double click – Jonah May 15 '11 at 18:26
  • 1
    Different question really, but a good answer to it can be found. here: http://stackoverflow.com/questions/2018257/how-to-combine-library-with-my-jar also here http://www.ibm.com/developerworks/library/j-jar/index.html – zellio May 15 '11 at 18:31
  • Thank you Mimisbrunnr the IBM link was very helpful – Jonah May 15 '11 at 18:41
2

If your JAR file is an executable JAR file which you execute by using the -jar option, or by double-clicking it, then the CLASSPATH environment variable as well as the -cp or -classpath option will be ignored.

For executable JAR files, you must specify the classpath in the manifest file of the JAR.

See this page from Oracle's Java Tutorials for more details on specifying the classpath in the manifest file: Working with Manifest Files.

Jesper
  • 202,709
  • 46
  • 318
  • 350