Java Ant多个主要在同一个JAR中

时间:2015-01-21 22:44:24

标签: java class ant classpath

首先,我必须说我根本不懂Ant;所以我希望你能原谅我。

我正在运行那个非常有效的ant文件。

<?xml version="1.0"?>
<project default="build" basedir=".">
    <property file="build.properties" />
    <property name="name" value="MyProject" />
    <property name="src.dir" location="${basedir}/src" />
    <property name="build" location="${basedir}/build" />
    <property name="build.classes" location="${build}/classes" />

    <target name="checkos">
        <condition property="bin.dir" value="${basedir}/lib/ForexConnectAPIx64/lib">
            <os family="unix" />
        </condition>    
    </target>

    <!-- App build -->
    <target name="build" depends="checkos" description="Builds the ${name} application">
        <!-- Creating directory -->
        <mkdir dir="${build.classes}" />

        <!-- Compiling sources -->
        <javac srcdir="${src.dir}/net/lcb"
               destdir="${build.classes}"
               includeAntRuntime="false"
               debug="${build.debug}"
               deprecation="true"
               verbose="false"
               optimize="true"
               classpath="${bin.dir}/java/fxcore2.jar;${basedir}/lib/joda-time-2.3.jar" >
               <compilerarg value="-Xlint"/> 
        </javac>

        <!-- Creating JAR-file -->
        <jar jarfile="${build}/${name}.jar">
            <fileset dir="${build.classes}" />
            <manifest>
                <attribute name="Main-Class" value="net.lcb.MyProject" />
                <attribute name="Class-Path" value="fxcore2.jar joda-time-2.3.jar mysql-connector-java-5.1.23-bin.jar" />
            </manifest>
        </jar>
    </target>

    <target name="rebuild" depends="clean, build" description="Rebuilds project" />

    <!-- Clean -->
    <target name="clean" description="Removes all temporary files">
        <!-- Deleting files -->
        <delete dir="${build}" />
    </target>

    <target name="run" depends="checkos" description="Rebuilds and run ${name}">
        <java jar="${build}/${name}.jar" fork="true" failonerror='true' dir="${bin.dir}">
            <jvmarg value="-Djava.library.path=${java.library.path}${path.separator}${bin.dir}${path.separator}${bin.dir}/java${path.separator}${basedir}/lib" />
        </java>
    </target>

我显然使用“ant run”运行项目

然后,我尝试修改该代码,以便能够执行2个不同的主类 我想做“ant runMain1”或“ant runMain2”

我写了这段代码,删除了JAR的Main-Class属性,并更改了Run Task。

<target name="runMain1" depends="checkos" description="Rebuilds and run ${name}">
    <java fork="true" failonerror='true' classname="net.lcb.MyMain1">
        <classpath>
            <pathelement location="${build}/${name}.jar" />
        </classpath>
        <jvmarg value="-Djava.library.path=${java.library.path}${path.separator}${bin.dir}${path.separator}${bin.dir}/java${path.separator}${basedir}/lib" />
    </java>
</target>

<target name="runMain2" depends="checkos" description="Rebuilds and run ${name}">
    <java fork="true" failonerror='true' classname="net.lcb.MyMain2">
        <classpath>
            <pathelement location="${build}/${name}.jar" />
        </classpath>
        <jvmarg value="-Djava.library.path=${java.library.path}${path.separator}${bin.dir}${path.separator}${bin.dir}/java${path.separator}${basedir}/lib" />
    </java>
</target>

但正如你猜测的那样,它不起作用。

root@vps-40758:/home/RobotV2# ant runRobotActions
Buildfile: /home/RobotV2/build.xml

checkos:

runRobotActions:
 [java] java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
 [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
 [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 [java]     at java.security.AccessController.doPrivileged(Native Method)
 [java]     at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
 [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
 [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
 [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
 [java]     at java.lang.Class.forName0(Native Method)
 [java]     at java.lang.Class.forName(Class.java:190)
 [java]     at net.lcb.sql.SQLConnexion.<init>(Unknown Source)
 [java]     at net.lcb.RobotActions.<clinit>(Unknown Source)
 [java] Chargement de la Liste.java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
 [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
 [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 [java]     at java.security.AccessController.doPrivileged(Native Method)
 [java]     at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
 [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
 [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
 [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
 [java]     at java.lang.Class.forName0(Native Method)
 [java]     at java.lang.Class.forName(Class.java:190)
 [java]     at net.lcb.sql.SQLConnexion.<init>(Unknown Source)
 [java]     at net.lcb.sql.ListesProduits.<clinit>(Unknown Source)
 [java]     at net.lcb.RobotActions.main(Unknown Source)
 [java] Exception in thread "main" java.lang.NullPointerException
 [java]     at net.lcb.sql.SQLConnexion.query(Unknown Source)
 [java]     at net.lcb.sql.ListesProduits.getList(Unknown Source)
 [java]     at net.lcb.RobotActions.main(Unknown Source)

BUILD FAILED
/home/RobotV2/build.xml:50: Java returned: 1

0 个答案:

没有答案