0

I have the following code:

import java.io.IOException;

public class DbTest {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace(System.out);
        }   
   }
}

I downloaded the Platform Independent .zip version of the MySQL Connector from the following location:

http://dev.mysql.com/downloads/connector/j/

Then, after unzipping the file downloaded, I placed the resulting folder at the following location:

/java/mysql-connector-java-5.1.36

I am compiling my code from within the directory where it resides with the following command:

javac DbTest.java -cp /java/mysql-connector-java-5.1.36/mysql-connector-java-5.1.36-bin.jar

Finally, I am running my code with the following command:

java DbTest

The full stack trace from the event is as follows:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
    at DbTest.main(DbTest.java:6)

Thanks in advance for any help you can offer!

John Smith
  • 45
  • 1
  • 7

1 Answers1

1

You need the JAR file on the classpath when you run your application; e.g.

 java -cp /longpath/mysql-connector-java-5.1.36-bin.jar:. pkg.pkg.DbTest

(For a database driver, you don't need the JAR at compile time if you load the class using Class.forName(...). However, it does no harm including it on the compile-time classpath anyway.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks for the response! Still no luck, though. "java -cp /longpath/file.jar DbTest" results in the following error: "Error: Could not find or load main class DbTest" – John Smith Sep 19 '15 at 23:43
  • I'm new to Java development in general, so I'm sure this is all boiling down to some fundamental misunderstanding. I'll look into adding it to my runtime classpath via configuration files and hope that takes care of it. Any thoughts on why the command line solution isn't behaving as you're expecting, though? – John Smith Sep 19 '15 at 23:45
  • Please read this. http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean/18093929#18093929. Carefully. The chances are that you are specifying either the class name or the classpath incorrectly. – Stephen C Sep 19 '15 at 23:59
  • Thank you for the link and for all your time today! The answer actually came in the form of a comment in response to that post. It seems I needed to also add the local folder to the classpath, as follows: "java -cp /longpath/file.jar:. DbTest" – John Smith Sep 20 '15 at 00:22
  • Presumably, the reason that comment held the answer will be found in one of the documents you linked regarding classpaths. I'm guessing it is the "effective classpath" that you mentioned and that I'm probably setting the classpath with that argument rather than adding to it? Well, I'll find out soon enough when I read those docs. Thanks again! – John Smith Sep 20 '15 at 00:25
  • So you didn't read the linked Oracle docs on the Classpath ... like the Answer said to do? :-) – Stephen C Sep 20 '15 at 00:35
  • In the process of it, was just hoping to discern the answer in advance to satiate my curiosity a bit. I assure you, I will not neglect to finish reading them tonight. :) – John Smith Sep 20 '15 at 00:42
  • Annnnd there's my answer! "Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable." – John Smith Sep 20 '15 at 00:57
  • Yea. There are lots of "moving parts" in the class loading mechanisms and lots of ways of doing things. The problem for someone like me is knowing which of the many possible ways that you are using, and which of the many possible mistakes have been made. The best outcome is if the OP (e.g. you) can be encouraged to read the official documentation and get your head around the mechanisms ... at least to a sufficient extent. Well done! – Stephen C Sep 20 '15 at 01:21