java - 使用ANT编译后无法运行Java代码

标签 java ant build

我有以下项目结构:

ProjectDir
-src
-lib
-data

src is the Java source files, lib contains several external JARs that my project uses, and data contains a variety of text files that the program uses to store and reference data. Up until now I've done all my development in an IDE (obviously) and everything's been fine, but I'm trying to write a build file that compiles the code (with the JARs in the classpath) and can be run with the data files. It's been a while since I've done a build with ANT so I'm sure this is a simple problem but I can't get the build script to work properly.

Below is the ant file:

<project name="CMSC491Project" basedir="." default="main">

    <property name="src.dir"     value="src"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>
    <property name="lib.dir"     value="lib"/>

    <property name="main.class"  value="parse.Driver"/>
    <property name="args"     value="?"/>



    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <fileset id="lib" dir="${lib.dir}">
        <include name="*.jar"/>
    </fileset>

    <path id="build.classpath">
        <fileset refid="${lib.dir}"/>
    </path>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>

        <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="build.classpath"/>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
        <attribute name="Class-Path" value="${lib.dir}/javahash ${lib.dir}/twitter4j-core-2.2.5 ${lib.dir}/twitter4j-stream-2.2.5"/>
            </manifest>
        </jar>
    </target>

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

    <target name="main" depends="clean,jar"/>

    <target name="run" depends="compile" description="run the project">
        <java dir="${classes.dir}" classname="${main.class}" fork="yes">
            <arg line="${args}"/>
        </java>
    </target>

</project>

它编译得很好,但如果我尝试 ant run 我收到一条错误,指出 java 无法从数据目录中的 JAR 之一中找到其中一个类。

不确定它需要我做什么,但提前致谢。

最佳答案

您在 java ant 任务中使用 dir 。这告诉您的 JVM 调用该目录中的进程。查看文档 here 。您应该通过 jarclasspath 属性引用该 jar。尝试将构建配置更改为:

<path id="build.classpath">         
    <fileset refid="lib"/>
    <fileset dir="${classes.dir}"/>     
</path>

<target name="run" depends="compile" description="run the project">
    <java classpathref="build.classpath" classname="${main.class}" fork="yes">
        <arg line="${args}"/>
    </java>
</target>

编辑(在编译中包含 classpathref)

关于java - 使用ANT编译后无法运行Java代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10569210/

相关文章:

docker - docker build输出的目的是什么?

java - 如何以线程安全的方式实现AWS S3客户端?

java - 使用我自己的 hashcode 作为 hashmap java

java - 将 9 位整数解析为颜色数组

java - 使用 ant build 生成 war 时替换 jnlp 中的字符串

.net - nuget安装退出代码9009

java - 为什么 Jersey 提供过时的数据?

java - 如何使用 Ant 创建捆绑的可运行 jar

java - Apache Ant 在 Solaris 11 上找不到 tools.jar

Android Studio 不编译签名的 apk 文件