Results 1 to 8 of 8
Thread: Problem with loading driver
- 01-17-2011, 04:23 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
Problem with loading driver
Hello,
Trying to load Connector/J driver for accesing MySQL server. JDBC
# import java.sql.Connection;
# import java.sql.DriverManager;
# import java.sql.SQLException;
#
# public class LoadDriver {
# public static void main(String[] args) {
# try {
#
# Class.forName("com.mysql.jdbc.Driver").newInstance ();
# } catch (Exception ex) {
# ex.printStackTrace();
# }
# }
# }
i get this error :
Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver
Caused by: java.lang.ClassNotFoundException: LoadDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
Could not find the main class: LoadDriver. Program will exit.
I already created a environment variable named CLASSPATH and give it value = C:\mysql-connector-java-5.1.14\mysql-connector-java-5.1.14-bin.jar.
What can be the problem. Please help. Thnx in advance.
- 01-17-2011, 05:27 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
It's not the mysql it can't find.
It's your LoadDriver class it can't find.
How are you executing this, in what directory, and where is the LoadDriver.class file?
- 01-17-2011, 05:43 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
I keep my LoadDriver.java file in C:\Java\src.
I can't figure out what value should i put in CLASSPATH - the folder location of conncetor/j or the jar file? Should I put the class that i created in that folder too? I'am totaly wracked. Please don't leave me without an answer. I had been on this since yesterday and without results. Thnx in advance for your patience.
- 01-17-2011, 05:45 PM #4
Hi,
You have mistake in classpath. You can run the class as follows.
src/LoadDriver.java
src/mysql-connector-java-5.1.8-bin.jar
Java Code:javac LoadDriver.java java -cp mysql-connector-java-5.1.8-bin.jar: LoadDriver
Last edited by Petr; 01-17-2011 at 05:49 PM.
- 01-17-2011, 05:54 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
The same thing:(
- 01-17-2011, 07:41 PM #6
OK. Let's say your code file LoadDriver.java is in the directory C:\Java\src
You can navigate from the command prompt to that directory and use the javac to compile the java file. Up to here, the things must work fine.
After running above command, the LoadDriver.class file would get generated at the same location.Java Code:C:\Java\src>javac LoadDriver.java
Now, suppose that the jar file that contains your mysql driver, is in the C:\Java\lib directory. Now, while running your program, you need to make sure that your compiler will see both the .class files, the one generated above and the one which is inside the jar file.
For this purpose, you need to use the -cp option of the java command. By using this, you can provide the class path where compiler will search for the .class files required.
So, try following command,
See the class path above carefully. The portion before the semicolon (;) gives the full path to the jar file and portion after it, which is a dot (.), tells the compiler to look into the current directory for class files.Java Code:C:\Java\src>java -cp C:\Java\lib\mysql-connector-java-5.1.8-bin.jar;. LoadDriver
Keep it in mind that, when you are referring to the jar file that contains the required class files, always mention the .jar file name in the command and not the directory path till that jar file.
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-17-2011, 07:46 PM #7
- 07-15-2011, 06:41 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
Loading of JDBC Driver
By Riyaz.hk77 in forum Advanced JavaReplies: 11Last Post: 11-11-2010, 08:53 AM -
ODBC driver problem windows 7
By dewitrydan in forum New To JavaReplies: 0Last Post: 11-07-2010, 11:01 AM -
driver problem
By arjun_duddu in forum JDBCReplies: 3Last Post: 08-24-2010, 08:44 AM -
mysql driver problem
By mokingsu in forum JDBCReplies: 4Last Post: 01-17-2008, 05:27 PM -
JDBC Driver problem
By Swamipsn in forum New To JavaReplies: 3Last Post: 08-09-2007, 03:55 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks