3
votes

I have a Java source code package which is known to successfully compile. I am attempting to compile the package with ant (the build.xml file is present). When I run ant all the Java import statements generate an error message of the type:

The import java.awt.geom.Path2D cannot be resolved" (the specific message depends on what is being imported)

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. I am using opensuse 13.2 and my installed java info is:

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?

The command line used to compile sources is: ant

build.xml is in the same directory that ant is run from which also contains the src directory. 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).

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.

Compiling with -lib option does not work:

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:

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

ant

The content of build.xml is:

<?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. I have not used Java in many, many years so I am way out of my depth here, help desperately appreciated.

  <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>
1
maybe set $JAVA_HOME? - ZhongYu
If you are using Eclipse, try this: stackoverflow.com/questions/16918959/import-cannot-be-resolved or instead, have a look at this: stackoverflow.com/questions/16664591/… - vphilipnyc
Please show the ant task specific to the compile (javac) as used by you. Also refer the javac ant documentation and notice that you have 3 options top provide the classpath: 1. classpath attribute 2. classpath nested element 3. classpathref attribute - YoYo
$JAVA_HOME is setup correctly. I am not using any IDE, just ANT from the command line using build.xml. - dexter foster
If you want help with the question "why does my build.xml not work" then you need to show us at least the relevant fragment of build.xml - the <javac> call and the definition of its classpath if it is referenced by a classpathref. Setting the CLASSPATH environment variable will not help, the classpath for a javac task in an Ant build is set by the build.xml - Ian Roberts

1 Answers

0
votes

Could be that your javac and java symlinks are pointing at different places

Compare these two commands and see if they point into the roughly the same area:

which javac
which java