3

I am very new on how to create ant files. Other similar question didn't helped me much so here is what I have.

I have this ant file:

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="UMS-PKS 33 Deploy Scripts for Snapshot" default="main"  basedir="..">

        <property name="src.dir"     value="setup/CopyScriptsToDatabase"/>
        <property name="ant.dir"     value="ant/src"/>
        <property name="build.dir"   value="tmp/buildMySql"/>
        <property name="classes.dir" value="${build.dir}/classes"/>
        <property name="jar.dir"     value="${build.dir}/jar"/>
        <property name="main-class"  value="CopyScriptsToDatabase.test"/>
        <property file="ant/properties/compile.properties" />
        <property file="ant/properties/profile.properties" />
        <property file="ant/properties/${deploy.properties}" />

        <path id="lib.classpath">
            <fileset dir="${ant.dir}">
                <include name="mysql-connector-java-commercial-5.1.21-bin.jar" />
            </fileset>
        </path>

        <target name="clean">

        </target>

        <target name="compile">
            <mkdir dir="${classes.dir}"/>
            <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
        </target>

        <target name="jar" depends="compile">
            <mkdir dir="${jar.dir}"/>
            <jar destfile="${jar.dir}/CopyScriptsToDatabase.jar" basedir="${classes.dir}">
                <manifest>
                    <attribute name="Main-Class" value="${main-class}" />
                </manifest>
            </jar>
        </target>

        <target name="run" depends="jar">
            <java jar="${jar.dir}/CopyScriptsToDatabase.jar" fork="true" classpath="lib.classpath">
                <arg value="${dwh.serverName}"/>
            </java>
        </target>

        <target name="clean-build" depends="clean,jar"/>
        <target name="main" depends="clean,run"/>
    </project>

And I get the following error when trying to run the jar file from ant:

        Buildfile: D:\Projects\UMS-PKS\ant\33_DeployScritps.xml
    clean:
    compile:
        [javac] D:\Projects\UMS-PKS\ant\33_DeployScritps.xml:26: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
        [javac] Compiling 1 source file to D:\Projects\UMS-PKS\tmp\buildMySql\classes
    jar:
          [jar] Building jar: D:\Projects\UMS-PKS\tmp\buildMySql\jar\CopyScriptsToDatabase.jar
    run:
         [java] java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
         [java]     at java.net.URLClassLoader$1.run(Unknown Source)
         [java]     at java.security.AccessController.doPrivileged(Native Method)
         [java]     at java.net.URLClassLoader.findClass(Unknown Source)
         [java]     at java.lang.ClassLoader.loadClass(Unknown Source)
         [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
         [java]     at java.lang.ClassLoader.loadClass(Unknown Source)
         [java]     at java.lang.Class.forName0(Native Method)
         [java]     at java.lang.Class.forName(Unknown Source)
         [java]     at CopyScriptsToDatabase.test.main(Unknown Source)
         [java] localhost
         [java] MySQL Connect Example.
    main:
    BUILD SUCCESSFUL
    Total time: 1 second

What do I need to add for this to work?

Thanks, Sas Gabriel

Sachin Prasad
  • 5,365
  • 12
  • 54
  • 101
Sas Gabriel
  • 1,544
  • 2
  • 20
  • 27
  • 1
    Try to use attribute classpathref instead of classpath in your task. – Robert Jan 14 '13 at 16:26
  • possible duplicate of [Cannot find Main Class in File Compiled With Ant](http://stackoverflow.com/questions/3143567/cannot-find-main-class-in-file-compiled-with-ant) – Mark O'Connor Jan 14 '13 at 20:51
  • i just tried classpathref and now i'm getting: – Sas Gabriel Jan 15 '13 at 09:58
  • clean: compile: [javac] D:\Projects\UMS-PKS\ant\33_DeployScritps.xml:26: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 1 source file to D:\Projects\UMS-PKS\tmp\buildMySql\classes jar: [jar] Building jar: D:\Projects\UMS-PKS\tmp\buildMySql\jar\CopyScriptsToDatabase.jar run: BUILD FAILED D:\Projects\UMS-PKS\ant\33_DeployScritps.xml:39: Reference tmp/buildMySql/jar/mysql-connector-java-commercial-5.1.21-bin.jar not found. and the mysql jar is there – Sas Gabriel Jan 15 '13 at 09:59
  • You can [add the classpath to the manifest of the jar](http://stackoverflow.com/q/858766/402322). This makes it superfluous to specify it when you run the jar. – ceving Nov 28 '13 at 14:07
  • 1
    found my answer here: [tutorial ant with classpathref](http://ant.apache.org/manual/tutorial-HelloWorldWithAnt.html) – Sas Gabriel Jan 15 '13 at 13:47

2 Answers2

0
  1. based on the directory your ant file is in, do you have a mysql-connector-java-commercial-5.1.21-bin.jar file in direcotry ../ant/src?
  2. if you have the file in ../ant/src/, use zip tool to open it and make sure Driver.class is in com/mysql/jdbc/. And I think it's better to name it as lib where you put 3rd party lib files.
Seaward
  • 21
  • 5
0

I just faced the same issue recently. After some investigation, I found the solution. I'll write it down for the next who are looking for this solution.

replace this section:

<target name="run" depends="jar">
    <java jar="${jar.dir}/CopyScriptsToDatabase.jar" fork="true classpath="lib.classpath">
        <arg value="${dwh.serverName}"/>
    </java>
</target>

to this:

<path id="classref1" location="${jar.dir}/CopyScriptsToDatabase.jar"/>
<path id="classref2" location="path/to/jars/mysql-connector-java-commercial-5.1.21-bin.jar"/>
<target name="run" depends="jar">
    <java fork="true classname="lib.classpath">
        <classpath>
            <path refid="classref1"/>
            <path refid="classref2"/>
        </classpath>
        <arg value="${dwh.serverName}"/>
    </java>
</target>

this is like running:

java -cp "mysql-connector-java-commercial-5.1.21-bin.jar:CopyScriptsToDatabase.jar" {lib.classpath} {arg_serverName}

nisanarz
  • 895
  • 1
  • 12
  • 22