简体   繁体   English

用ant编译java源代码

[英]compiling java source code with ant

I have a Java source code package which is known to successfully compile. 我有一个Java源代码包,已知成功编译。 I am attempting to compile the package with ant (the build.xml file is present). 我试图用ant编译包(build.xml文件存在)。 When I run ant all the Java import statements generate an error message of the type: 当我运行ant时,所有Java import语句都会生成一个类型的错误消息:

The import java.awt.geom.Path2D cannot be resolved" (the specific message depends on what is being imported) 导入java.awt.geom.Path2D无法解析“(具体消息取决于导入的内容)

Clearly the package paths are not defined and I have no idea where the requested imports live in the filesystem. 很明显,包路径没有定义,我不知道请求的导入在文件系统中的位置。 I assume if I know the paths that setting $CLASSPATH will get the program to compile. 我假设我知道设置$CLASSPATH将使程序编译的路径。 I am using opensuse 13.2 and my installed java info is: 我正在使用opensuse 13.2,我安装的java信息是:

java -version
openjdk version "1.8.0_45"
OpenJDK Runtime Environment (build 1.8.0_45-b14)
OpenJDK 64-Bit Server VM (build 25.45-b02, mixed mode)

Can anyone point me to where to find the java packages and how to get ant to compile the sources? 任何人都可以指出我在哪里找到java包以及如何获取ant来编译源代码?

The command line used to compile sources is: ant 用于编译源代码的命令行是:ant

build.xml is in the same directory that ant is run from which also contains the src directory. build.xml与运行ant的目录相同,其中也包含src目录。 This is a working project, I just don't know how to configure standard library locations (I'm not a java type by any means). 这是一个工作项目,我只是不知道如何配置标准库位置(我不是任何方式的java类型)。

Update. 更新。

I have located the path to the openjdk libraries at /usr/lib64/jvm/java-1.8.0-openjdk-1.8.0/jre/lib/rt.jar and have tried to compile 2 different ways but still get imports cannot be resolved error messages. 我找到了/usr/lib64/jvm/java-1.8.0-openjdk-1.8.0/jre/lib/rt.jar中的openjdk库的路径,并试图编译2种不同的方法,但仍然得到导入不能解决了错误消息。

Compiling with -lib option does not work: 使用-lib选项进行编译不起作用:

ant -lib /usr/lib64/jvm/java-1.8.0-openjdk-1.8.0/jre/lib/rt.jar ant -lib /usr/lib64/jvm/java-1.8.0-openjdk-1.8.0/jre/lib/rt.jar

Setting the CLASSPATH environment variable does not work: 设置CLASSPATH环境变量不起作用:

export CLASSPATH=/usr/lib64/jvm/java-1.8.0-openjdk-1.8.0/jre/lib/rt.jar export CLASSPATH = / usr / lib64 / jvm / java-1.8.0-openjdk-1.8.0 / jre / lib / rt.jar

ant 蚂蚁

The content of build.xml is: build.xml的内容是:

 <?xml version="1.0"?> <project name="MazR" default="jar" basedir="."> <property name="jar.file" value="${ant.project.name}.jar"/> <property name="src.dir" value="src"/> <property name="build.dir" value="build"/> <property name="dist.dir" value="dist"/> <property name="applet.html" value="applet.html"/> <taskdef resource="checkstyletask.properties"/> <target name="init"> <mkdir dir="${build.dir}"/> <mkdir dir="${dist.dir}"/> </target> <target name="compile" depends="init"> <javac srcdir="${src.dir}" destdir="${build.dir}" debug="on" includeantruntime="false"> <compilerarg value="-Xlint"/> </javac> </target> <target name="jar" depends="compile"> <jar destfile="${dist.dir}/${jar.file}" basedir="${build.dir}"> <manifest> <attribute name="Main-Class" value="com.nullprogram.maze.RunMaze"/> </manifest> </jar> <copy file="${applet.html}" tofile="${dist.dir}/index.html"/> </target> <target name="run" depends="jar"> <java jar="${dist.dir}/${jar.file}" fork="true"/> </target> <target name="clean"> <delete dir="${build.dir}"/> <delete file="${jar.file}"/> </target> <target name="check"> <checkstyle config="checkstyle.xml"> <fileset dir="src" includes="**/*.java"/> </checkstyle> </target> <target name="format" description="Run the indenter on all source files."> <apply executable="astyle"> <arg value="--mode=java"/> <arg value="--suffix=none"/> <fileset dir="${src.dir}" includes="**/*.java"/> </apply> </target> <target name="applet" depends="jar" description="Run the applet version."> <exec executable="appletviewer"> <arg value="${dist.dir}/index.html"/> </exec> </target> <target name="javadoc" description="Generate Javadoc HTML."> <javadoc destdir="${dist.dir}/javadoc"> <fileset dir="${src.dir}" includes="**/*.java" /> </javadoc> </target> </project> 

Any idea why import does not seem to work? 知道为什么导入似乎不起作用?

Thanks! 谢谢!

Further update. 进一步更新。

I added a classpath definition to the build.xml as below, still no joy. 我在build.xml中添加了一个类路径定义,如下所示,仍然没有乐趣。 I have not used Java in many, many years so I am way out of my depth here, help desperately appreciated. 我已经很多年没有使用过Java了,所以我在这里深入了解,帮助我们非常感激。

  <property name="java.class.path" location="/usr/lib64/jvm/java-1.8.0-openjdk-1.8.0/jre/lib/rt.jar"/> <target name="compile" depends="init"> <javac srcdir="${src.dir}" destdir="${build.dir}" debug="on" includeantruntime="false"> <compilerarg value="-Xlint"/> <classpath> <pathelement path="${java.class.path}/"/> </classpath> </javac> </target> 

Could be that your javac and java symlinks are pointing at different places 可能是你的javac和java符号链接指向不同的地方

Compare these two commands and see if they point into the roughly the same area: 比较这两个命令,看看它们是否指向大致相同的区域:

which javac
which java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM