0

I wanted to try using SQL with Java, so I downloaded mysql-connector-java-5.1.18-bin.jar.

I'm using Eclipse, so I went into Eclipse, Project -> Properties and added the jar files.

I also added the following to my classpath:

  1. C:\Users\shahin\Desktop\mysql-connector-java-5.1.18\com\mysql\jdbc\;
  2. C:\Users\shahin\Desktop\mysql-connector-java-5.1.18\mysql-connector-java-5.1.18-bin.jar;

It runs fine in Eclipse, but when I run my. Jar file I get this error:

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(Unknown Source)
        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 Main.main(Main.java:15)
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
user1021085
  • 729
  • 3
  • 10
  • 28

2 Answers2

0

That's because mysql-connector-java-5.1.18-bin.jar isn't in your classpath when you run your jar.

Inside your .jar file there should be a META-INF/MANIFEST.MF file. You need an entry in that file for your classpath that points to where the mysql-connector-java-5.1.18-bin.jar file is located:

Class-Path: path/to/mysql-connector-java-5.1.18-bin.jar

Note this can be local to your jar. A common way to do it is to have a lib subdirectory where your jar is located that contains the other jars yours depends on:

Class-Path: lib/mysql-connector-java-5.1.18-bin.jar
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • If I have it in my jar, how would that look? ../the-jar-file.jar ? – user1021085 Oct 31 '11 at 16:14
  • You can't have it in your jar. – Brian Roach Oct 31 '11 at 16:15
  • Well, for the sake of completeness, you don't *need* that, you just need to make sure it's on the classpath when executing. This is one way of doing that. (And unless it's being distributed, not sure it's the best, but that's a different discussion :) – Dave Newton Oct 31 '11 at 16:17
  • If someone else wants to run it I have to put the jar and the mysql...jar into a folder? That sucks – user1021085 Oct 31 '11 at 16:19
  • Well this was weird.. When I add the Class-Path: to my manifest file, it gives me a "Cannot find the main class "Main"..... If I don't include the Class-Path in the manifest file it will say classNotFound com.mysql.Driver – user1021085 Oct 31 '11 at 16:25
  • @Dave - the problem is that if you're running it with -jar I believe any externally set classpath is ignored, so ... it depends on how you're doing it. – Brian Roach Oct 31 '11 at 16:27
  • Why can't it find the main-class "Main" when I add the Class-Path? – user1021085 Oct 31 '11 at 16:34
  • @BrianRoach Yep, if you're running it with -jar. – Dave Newton Oct 31 '11 at 16:42
  • @user1021085 See [this SO post](http://stackoverflow.com/questions/1635136/how-to-set-default-main-class-in-java) regarding the `Main-Class` manifest property. – Dave Newton Oct 31 '11 at 16:42
  • But my manifest file already includes "Main-Class: Main". The file is called Main, the methos called main. And as I said, if I remove the Class-Path from the manifest file, I get the class not found (which means it found the main class?). – user1021085 Oct 31 '11 at 16:45
  • I tried making a new java file, which just outputs something, created a new manifest and made it into .jar and it works. Problem seems to be with the sql java program only.. – user1021085 Oct 31 '11 at 16:58
0

Your classpath for the mysql .jar is most likely off. Is your jar located nearby the mysql jar? (If its not being pointed to it) To test this, you can place the two in the same directory and see if it runs.

Max
  • 5,799
  • 3
  • 20
  • 27