chadneal
chadneal

Reputation: 926

ANT problem with jar file

I am having a problem producing a working jar file. I would like to end up with a jar file that can run itself by using the command java -jar myjar.jar

So far I have

<project default="jar" name="Create Runnable Jar fecrudbreset">
<target name="clean">
    <delete dir="bin"/>
    <delete dir="build"/>
</target>
<target name="compile">
    <mkdir dir="./build"/>
    <javac srcdir="./src" destdir="./build">
        <classpath>
            <pathelement location="./lib/sqljdbc4.jar"/>
        </classpath>
    </javac>
</target>
<target name="jar" depends="compile">
    <mkdir dir="./bin"/>
    <jar destfile="./bin/fecrudbreset.jar" >
        <manifest>
            <attribute name="Main-Class" value="FecruDBreset"/>
            <attribute name="Class-Path" value="."/> 
            <attribute name="Built-By" value="${user.name}"/>
        </manifest>
        <fileset dir="./build"/>
        <zipfileset dir="./lib" includes="sqljdbc4.jar"/>
    </jar>
</target>
</project>

The compile works however when I try to run the app I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/microsoft/sqlserver/jdbc/SQLServerDataSource
    at FecruDBreset.main(Unknown Source)

update: The main class I have uses the default package. The class is found and starts to run but fails when it cant locate the sql driver it seems. I guess I dont understand why it cant find it.

Here is how the main class is defined:

import java.sql.*;
import com.microsoft.sqlserver.jdbc.*;

public class FecruDBreset
{

/*
 * Used to reset / nuke fecru database
 */
public static void main(String[] args)
{

Also if I unzip the jar file that is produced the contents look like this:

META-INF/
META-INF/MANIFEST.MF
FecruDBreset.class
sqljdbc4.jar

Update 2: My source tree looks like this:

|-- bin
|   `-- fecrudbreset.jar
|-- build
|   `-- FecruDBreset.class
|-- build.xml
|-- lib
|   `-- sqljdbc4.jar
|-- readme.txt
`-- src
    `-- FecruDBreset.java

Anyone have any idea what I am doing wrong? Thanks much in advance. Chad

Upvotes: 3

Views: 1319

Answers (5)

zoobert
zoobert

Reputation: 572

Like you did in your ant file you need to add in your classpath the sqlserver jdbc driver. Otherwise java won't find it.

To do that you need to add the dependency jar file in the manifest of myjar.jar

You should be able to do it with the jar command of ant by passing a manifest.txt file that contains something like:

Manifest-Version: 1.0 Class-Path: sqljdbc4.jar (update the path if necessary) Created-By: 1.6.0 (Sun Microsystems Inc.)

See http://download.oracle.com/javase/tutorial/deployment/jar/downman.html

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168845

@chadneal: Also if I unzip the jar file that is produced the contents look like this:

META-INF/
META-INF/MANIFEST.MF
FecruDBreset.class
sqljdbc4.jar

The main Jar cannot contain the sqljdbc4.jar. Instead, a reference to that Jar should be added to the class-path in the manifest.

Upvotes: 1

David W.
David W.

Reputation: 107090

Taking a quick guess. It looks like it can't find your class com.microsoft.sqlserver/*

Where is the jar that contain this located? Your default classpath is just ".", but it looks like you put sqljdbc4.jar in lib directory inside your jarfile.

You can modify Class-Path to include ./lib, or you can move sqljdbc4.jar jar to the root of the jarfile.

Upvotes: 1

xplanner expert
xplanner expert

Reputation: 204

Your main class FecruDBreset depends on com.microsoft.sqlserver.jdbc.SQLServerDataSource

You should have microsoft sql driver in class path.

so you should run your jar using following command

java -jar myjar.jar -classpath {path_to_microsoft_jdbc_driver}

Upvotes: 0

Christian Kuetbach
Christian Kuetbach

Reputation: 16060

I think you are missing the classpath entry.

You probably want to include you sql-driver directy in the class folder "fatjar" or "uberjar": http://one-jar.sourceforge.net/

This will create a single jarfile, which can be executed directly.

Upvotes: 0

Related Questions