Kyle Schustak
Kyle Schustak

Reputation: 1

"Package * does not exist" when running ant compile

Using ant to compile (ant compile) a project where I have multiple src folders that need to be built each into their own classes folder. Example:

.
|-- classes1/com
|   `-- A.class
|-- classes2/com
|   `-- B.class
|-- src1/com
|   `-- A.java
`-- src2/com
    `-- B.java

The problem that I'm running into is that B.java uses the class defined in A.java, and when compiling B.java it's throwing package com.A does not exist.

Here is what my ant snippet looks like:

<path id="classpath">
    <fileset dir="lib">
        <include name="*.jar" />
    </fileset>
    <fileset dir=".">
        <include name="**/*.classes" />
    </fileset>
</path>

<target name="compile">
    <javac srcdir="src1" destdir="classes1" includeantruntime="false">
        <classpath refid="classpath" />
    </javac>
    <javac srcdir="src2" destdir="classes2" includeantruntime="false">
        <classpath refid="classpath" />
    </javac>
</target>

When I run ant -v compile I can even see classes1/com/A.class in the classpath

Upvotes: 0

Views: 2485

Answers (2)

Berne
Berne

Reputation: 627

The problem might be the way the path classpath is being built. I suppose the dir "." is a folder that contains both classes1 and classes2. In this case, the class "com.A" would be, indeed, "classes1.com.A". The fileset should have two different paths (one for each folder), like:

<path id="classpath">
    <fileset dir="lib">
        <include name="*.jar" />
    </fileset>
    <fileset dir="classes1">
        <include name="**/*.class" />
    </fileset>
    <fileset dir="classes2">
        <include name="**/*.class" />
    </fileset>
</path>

This way, the base folders would be "classes1" and "classes2", instead of parent.

Upvotes: 0

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 78001

First of all each compile has a slightly different classpath so keep them separate for clarity. Secondly jars are individually listed on a classpath whereas classes are referenced via their base directory.

Try the following:

<path id="compileA">
    <fileset dir="lib">
        <include name="*.jar" />
    </fileset>
</path>

<path id="compileB">
    <fileset dir="lib">
        <include name="*.jar" />
    </fileset>
    <pathelement location="classes1"/>
</path>

<target name="compile">
    <javac srcdir="src1" destdir="classes1" includeantruntime="false" classpathref="compileA"/>
    <javac srcdir="src2" destdir="classes2" includeantruntime="false" classpathref="compileB"/>
</target>

Upvotes: 1

Related Questions