Registering the Driver and Classpath Variable

Hello all

I've been developing a java front-end to a mysql database using netbeans 5.0. For some time now I've been struggling with trying to run my application outside of the IDE. When developing and running in the IDE, I can successfully connect to the database. However when I try to execute from the command line via:

java -jar "C:\myProject.jar"

I would catch a ClassNotFoundException: com.mysql.jdbc.Driver...

I believe that in the IDE the driver is appropriately registered via the call:Class.forName("com.mysql.jdbc.Driver");

However, this is not the case when running my application outside of the IDE.

I've downloaded the following file from the mysql site for connectivity:

"mysql-connector-java-5.0.3.zip"

1) I am experimenting with adding Jars to the libraries directory in the netbeans project window .. no dice ... I'm pretty sure I am adding what I extracted via the connector J zip file from the mysql site. However still no changes.

2) I've tried also to add that into the CLASSPATH environment variable .. still no luck

I've been frusturated with this roadblock for too long and was hoping to gain some useful insight about what is causing this problem.

Any help would be greatly appriciated... thanks

[1326 byte] By [Curtis_Pastorea] at [2007-10-3 4:23:36]
# 1

> Hello all

>

> I've been developing a java front-end to a mysql

> database using netbeans 5.0. For some time now I've

> been struggling with trying to run my application

> outside of the IDE. When developing and running in

> the IDE, I can successfully connect to the database.

> However when I try to execute from the command line

> via:

> java -jar "C:\myProject.jar"

> I would catch a ClassNotFoundException:

> com.mysql.jdbc.Driver...

>

> I believe that in the IDE the driver is appropriately

> registered via the

> call:

> Class.forName("com.mysql.jdbc.Driver");

>

That has nothing to do with class not found though.

> However, this is not the case when running my

> application outside of the IDE.

>

> I've downloaded the following file from the mysql

> site for connectivity:

> "mysql-connector-java-5.0.3.zip"

>

> 1) I am experimenting with adding Jars to the

> libraries directory in the netbeans project window ..

> no dice ... I'm pretty sure I am adding what I

> extracted via the connector J zip file from the mysql

> site. However still no changes.

>

It has nothing to do with netbeans when you are running from the command line.

> 2) I've tried also to add that into the CLASSPATH

> environment variable .. still no luck

>

That is because jar executables have their own rules. They do not use external class paths (neither from the command line nor from env vars.)

> I've been frusturated with this roadblock for too

> long and was hoping to gain some useful insight about

> what is causing this problem.

> Any help would be greatly appriciated... thanks

Either don't use a jar executable or expect doing a LOT of work. For the jar executable you must embed the jdbc driver in the jar and then manually extract it. You can find threads about doing that.

jschella at 2007-7-14 22:25:54 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

> Either don't use a jar executable or expect doing a

> LOT of work. For the jar executable you must embed

> the jdbc driver in the jar and then manually extract

> it. You can find threads about doing that.

I thought that adding a library via the netbeans IDE would do exactly that.... because it writes the manifest file to include that?

if that is incorrect... how would I go about embedding that extra connector jar? or if I search for threads ... could you point me at the correct forum category? .. or shall I stick with this one?

Curtis_Pastorea at 2007-7-14 22:25:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
Also .. what would an alternative be to using a jar executable?
Curtis_Pastorea at 2007-7-14 22:25:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

>

> I thought that adding a library via the netbeans IDE

> would do exactly that.... because it writes the

> manifest file to include that?

>

I don't use NetBeans so I couldn't say.

A resource like an image though (which is standard in a jar) is not the same as a jdbc driver.

> if that is incorrect... how would I go about

> embedding that extra connector jar? or if I search

> for threads ... could you point me at the correct

> forum category? .. or shall I stick with this one?

This forum (jdbc) has discussed it in the past. Mainly on what you need to do once it is in the jar. You still have to get in the jar.

jschella at 2007-7-14 22:25:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5
> Also .. what would an alternative be to using a jar> executable?Just run it. Provide the jar as part of the class path command line option and also provide the class to run.
jschella at 2007-7-14 22:25:55 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...