我有以下项目结构:
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 。您应该通过 jar
或 classpath
属性引用该 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/