Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can't access JAR classes

2 views
Skip to first unread message

Mark McKay

unread,
Aug 20, 2002, 9:56:06 PM8/20/02
to

I can't figure out how to access the class files in a JAR archive. I'm
trying to write a simple program to query a database and write the
output to STDOUT. I can't figure out the syntax to get the JRE to find
the databse driver.

I have '.' in my classpath. In the directory containing both the .java
and .jar file, I type:

# javac SQLTest.java -classpath mysql-connector-java-2.0.14-bin.jar

It compiles fine. However, when I try to run it using the command line:

# java SQLTest -classpath mysql-connector-java-2.0.14-bin.jar

I get the following error:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:198)
...
at SQLTest.<init>(SQLTest.java:35)
at SQLTest.main(SQLTest.java:65)
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:532)
...
at SQLTest.<init>(SQLTest.java:43)
at SQLTest.main(SQLTest.java:65)

Am I typing this correctly? How do I get database connectivity with my
application?

Mark McKay

--
http://www.kitfox.com

Yar Hwee Boon

unread,
Aug 21, 2002, 3:33:10 AM8/21/02
to
Mark McKay <ma...@kitfox.com> wrote in message news:<3D62F336...@kitfox.com>...

Check if mysql-connector-java-2.0.14-bin.jar does contain com.mysql.jdbc.Driver

Hwee Boon

Brian Bacon

unread,
Aug 21, 2002, 11:25:58 AM8/21/02
to
Mark McKay <ma...@kitfox.com> wrote in message news:<3D62F336...@kitfox.com>...

Did you register the driver in the code...

import driverclass.*;

Class.forname("mysqldriver");

BB

Mark McKay

unread,
Aug 21, 2002, 6:22:45 PM8/21/02
to

I'm including my code below (passwords have been changed).
com.mysql.jdbc.Driver does exist in the jar. It's also being found
during compile since the second line doesn't cause the compilation to
fail. However, running it via 'java' from the command line can't find
the class.

--
http://www.kitfox.com


import java.sql.*;
import com.mysql.jdbc.Driver;

public class SQLTest {

/** Creates a new instance of SQLTest */
public SQLTest() {
String dbUrl;
dbUrl = "jdbc:mysql://localhost:3306/myDb";

Connection con;
String qry;
qry = "SELECT * FROM region;";
Statement stmt;

ResultSet rs;

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch(Exception e) {
e.printStackTrace();
}

try {
// here i get the exception
con = DriverManager.getConnection(dbUrl, "myLogin", "myPw");
stmt = con.createStatement();
rs = stmt.executeQuery(qry);

while (rs.next())
{
String s = rs.getString("title");
System.err.println("Qry: " + s);
}

stmt.close();
con.close();
}
catch(SQLException ex) {
ex.printStackTrace();
}

}


static public void main(String args[])
{
SQLTest test = new SQLTest();
}
}

masayoshi hayashi

unread,
Aug 25, 2002, 4:16:25 AM8/25/02
to
Hi I also have the same kind of problem and I get the same error using
the same jar file. I am a newbie in Java so some of the things I claim
maybe incorrect.

I somehow managed to run some sample codes in java tutorial in jdbc
but most have problems with "ClassNotFoundException: org.... com....
SQLexception: No suitable Driver", whichever driver (com or org
version) is used. I never had a luck with com version yet.

I suggest you to copy mysql-connector-java-2.0.14.jar file to
$java_home$/jre/lib/ext directory, so that you don't have to import
the driver (remove line: import com.mysql.jdbc.Driver;), or set
classpath (i.e. using jar as extension). Also remove newInstance()
method in the first try statement body. According to sun's java
tutorial, no need to do create another copy of instance as
Class.forName("com.mysql.jdbc.Driver") will automatically instantiate.

I also tried 3.0 beta jar version but the same problem persists.
Anyone else having the same problem? Help!

masayoshi hayashi

unread,
Aug 25, 2002, 6:41:20 AM8/25/02
to
I solved mine! I run the jar in Windows XP. If you run under Windows,
change the PATH order by placing the java_home/bin directory first and
the rest. Here is why:

http://www.yoda.arachsys.com/java/extensions.html

and find more info about extension mechanism at Sun's Java Tutorial:

http://java.sun.com/docs/books/tutorial/ext/index.html

:>

0 new messages