user496949
user496949

Reputation: 86185

In the ANT build.xml, why can't we use both jar and classpath together in the java task

In the ANT build.xml, why can't we use both jar and classpath together in the Java task?

Upvotes: 0

Views: 823

Answers (2)

matt
matt

Reputation: 79813

I assume you mean why the java task ignores your classpath settings when you use it with a jar attribute. From the ant java docs:

When using the jar attribute, all classpath settings are ignored according to Sun's specification.

and from that link:

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

So the answer is simply that it's like that because it's following the spec.

If this is an issue for you, you can either add the jar file in question to the classpath and then use the normal classname attribute to specify the class to run, or if you're building the jar yourself you can add a Class-Path entry to the manifest file when you create it. The manifest and manifestclasspath tasks may be helpful there.

Upvotes: 1

CoolBeans
CoolBeans

Reputation: 20820

Unless I understood you question wrong you should be able to do that easily. I am assuming you want to include jars in a classpath and a set of jars in another folder.

You can do something like this:-

<property name="classpath" value="./WEB-INF/lib"/>
<property name="dependency" value="./libs"/>
<path id = "my-classpath">
        <fileset dir = "${classpath}">
                <include name = "**/**.jar"/>
        </fileset>
        <fileset dir = "${dependency}">
                <include name = "**/**.jar"/>
        </fileset>      
</path>

<javac destdir="${build.dir}">
  <src path="${src.dir}"/>
  <classpath refid="my-classpath"/>
</javac>

EDIT

As you also mentioned java task. The below example should be what you are looking for I think. Let me know if this is not it.

 <path id="project.class.path">  
     <pathelement location="somejar.jar"/>  
     <pathelement path="${java.class.path}/"/>  
     <pathelement path="${additional.dependencies}"/>  
 </path>  
 <java jar="test.jar" classpathref="project.class.path" ... />   

Upvotes: 1

Related Questions