Author | Message | Time |
---|---|---|
Grok | Like 50 million other people, I am having a problem starting out with MySQL Connector/J for JDBC. I believe I have followed the installation instructions to the letter, as well as tried all the derivations of suggestions in many different forums. First I will display the test source code, then the results. Afterward I will describe the environment. Test source: [code] import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; class MyTest { public static void main(String[] args) { System.out.println("MyTest started."); try { Class.forName("com.mysql.jdbc.Driver"); } catch (Exception ex) { System.err.println("------------------------------------------------"); System.err.println("Exception: " + ex.getMessage()); System.err.println("------------------------------------------------"); } try { String url = "jdbc:mysql:///user"; String uid = "root"; String upwd = "changed"; Connection con = DriverManager.getConnection(url, uid, upwd); } catch (SQLException ex) { System.err.println("------------------------------------------------"); System.err.println("SQLException: " + ex.getMessage()); System.err.println("------------------------------------------------"); } System.out.println("MyTest completed."); } } [/code] Runtime results: S:\Java\Projects>java -classpath S:\Java\jdk1.6.0_13\jre\lib\ext\mysql-connector -java-5.1.7-bin.jar MyTest Exception in thread "main" java.lang.NoClassDefFoundError: MyTest Caused by: java.lang.ClassNotFoundException: MyTest 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.ClassLoader.loadClassInternal(Unknown Source) Could not find the main class: MyTest. Program will exit. Environment: S:\Java\jdk1.6.0_13\jre\lib\ext\ contains mysql-connector-java-5.1.7-bin.jar PATH contains S:\Java\jdk1.6.0_13\bin NOTE: I have tested with and without setting CLASSPATH, as well as putting in on the command line when invoking the runtime. Invoking it WITHOUT classpath set returns (in case you asked for this test): [code] S:\Java\Projects>java MyTest MyTest started. ------------------------------------------------ Exception: com.mysql.jdbc.Driver ------------------------------------------------ ------------------------------------------------ SQLException: No suitable driver found for jdbc:mysql:///user ------------------------------------------------ MyTest completed. [/code] Like I said at the top, this reads to be a very common problem and after hours of reading the same answers on every page and reimplementing them repeatedly, I do not see what I am missing, or know how to properly test for missing assumption. | May 29, 2009, 1:43 AM |
Camel | Overriding the classpath replaces the working directory, preventing MyTest.class from being seen. Just add . to your classpath; [tt]java -classpath .:mysql.jar MyTest[/tt] Additionally, I would strongly recommend that you do not put the driver in lib/ext. One reason is that the VM is not required to observe that directory. An even better reason is that, even if it does, you're asking for trouble if any other Java program on your system requires a different version of that library. | May 29, 2009, 7:25 AM |
Grok | [quote author=Camel link=topic=17964.msg182840#msg182840 date=1243581909] Overriding the classpath replaces the working directory, preventing MyTest.class from being seen. Just add . to your classpath; [tt]java -classpath .:mysql.jar MyTest[/tt] Additionally, I would strongly recommend that you do not put the driver in lib/ext. One reason is that the VM is not required to observe that directory. An even better reason is that, even if it does, you're asking for trouble if any other Java program on your system requires a different version of that library. [/quote] You nailed it. +1 [code] S:\Java\Projects>java -classpath .;S:\MySQL\Tools\connectorJ\mysql-connector-java-5.1.7-bin.jar MyTest MyTest started. MyTest completed. [/code] | May 29, 2009, 11:15 AM |
Myndfyr | [quote author=Grok link=topic=17964.msg182843#msg182843 date=1243595741] You nailed it. +1 [/quote] See, wouldn't it be handy to have Karma enabled? :-O | May 29, 2009, 5:54 PM |
Camel | Incidentally, colon is preferred over semicolon for the path separator. I'm not sure if semicolon even works on *nix, but if it does you'd at least have to escape it. | May 29, 2009, 8:18 PM |
Myndfyr | [quote author=Camel link=topic=17964.msg182850#msg182850 date=1243628298] Incidentally, colon is preferred over semicolon for the path separator. I'm not sure if semicolon even works on *nix, but if it does you'd at least have to escape it. [/quote] Colon is used for drive names in Windows and probably shouldn't be used to separate paths in Windows. But, semicolon is pretty common as item tokenizers in Windows. | May 30, 2009, 4:24 AM |
Camel | It's a Java convention, not a personal one :) | May 30, 2009, 7:20 AM |