Two Laptop Bag*
The moose likes Ant, Maven and Other Build Tools and the fly likes MySQL with ant: can not find driver class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "MySQL with ant: can not find driver class" Watch "MySQL with ant: can not find driver class" New topic
Author

MySQL with ant: can not find driver class

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
I am testing a MySQL jdbc program. My project looks like this:

--build
-- mysql-jdbc-connector.jar
--classes
-- ddd.class

so I use
-----------------
<jar dest="dd.jar">
<fileset dir="${classes}"/>
<fileset dir="{build}"/> <!-- add MySQL-jdbc-con.jar into it -->
</jar>

<java classname="ddd" classpath="dd.jar" />
-------------

But still have the error, "can not find class com.mysql.jdbc.Driver". I unzip the dd.jar, I do see the MySql driver inside.

I don't know why. Could you help me?

after this
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
To be clear, are you placing a JAR inside of a JAR?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Dear Dirk,

no, I just want to make a single executable jar file.

Actually I solve the part of problem. I add the classpath "ant.java.classpath" to "java" target. It works.

But the problem is, when I go to windows console, to java -jar myjar.jar. It NOT works, still can not find "com.mysql.jdbc.Driver". Actually the mysql driver jar is located in ./jar folder.

How to work out?

Thanks
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
If I understand correctly, you need to add the JAR file to your CLASSPATH.
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Dear Dirk,

Below is what I have tried to do. I have to place the MySQL driver jar into my jar file. My goal is to make the jar file self-executalbe, say,double click to lanuch it.

Error should be here
<attribute name="Main-Class" value="classes/${project-main-class}"/>
<attribute name="Class-Path" value="lib/mysql-connector***-bin.jar"/>

Thanks.

Edward


Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
That NoClassDefFoundError is due to
<attribute name="Main-Class" value="classes/${project-main-class}"/>
which should be
<attribute name="Main-Class" value="${project-main-class}"/>


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
Originally posted by Lasse Koskela:
That NoClassDefFoundError is due to
<attribute name="Main-Class" value="classes/${project-main-class}"/>
which should be
<attribute name="Main-Class" value="${project-main-class}"/>


HI, Lasse, thanks for your response. But it still doesn't work.

The main class , ddd.java , doesn't belong to any package. It is plain. Its class file, ddd.class, just is placed into classes directory.

The question is, I should tell the compiler: "classes\ddd" or just "ddd" ? I guess should be the former one.

But it doesn't work.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
If you've a class that is not part of a package, you can add it to your CLASSPATH by specifying the location of the directory where the class resides.
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Set class path in build.xml of ANT






SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
 
 
subject: MySQL with ant: can not find driver class