我正在学习Ant来运行Java程序,无论是否构建Jar文件。
这是我想要运行的简单程序
public class ForDemo {
int i = 0;
byte b = 0;
short s = 0;
double d = 0;
float f = 0;
public ForDemo() {
for (int i = 0; i <= 1000; i++) {
System.out.println("" + i);
}
}
public static void main(String[] args) {
new ForDemo();
}
}
Ant的build.xml文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="run" basedir="." name="">
<!--this file was created by Eclipse Runnable JAR Export Wizard-->
<!--ANT 1.7 is required -->
<target name="compile">
<javac srcdir="." destdir="." includes="**/*.java" target="1.8">
<classpath refid="classpath.base" />
</javac>
</target>
<target name="run" depends="compile">
<java fork="true" failonerror="yes" classname="ForDemo">
<arg line=" "/>
<classpath refid="classpath.base" />
</java>
</target> <!-- Libraries on which your code depends -->
<path id="classpath.base">
<fileset dir="libs">
<include name="**/*.jar" />
</fileset>
</path>
</project>
但是我收到了这个错误
run:
[java] Error: Could not find or load main class ForDemo
BUILD FAILED
当我在带有oracle jdk 1.8b40的Linux Mint 17.1 x64上的java file命令的父目录中运行ant时。
答案 0 :(得分:2)
您没有在java任务的类路径中包含javac任务的输出目录("."
),因此在java执行时,您编译的ForDemo
类不在类路径上。
您可以在您的java任务中包含该dir,如下所示:
<classpath>
<pathelement path="${classpath.base}"/>
<pathelement location="."/>
</classpath>