Shubham Jain
Shubham Jain

Reputation: 17593

How to execute an external jar with classpath and also having CI argument using ant

I have a situation where I have a jar already with me. I do not need to create another jar using ant. Also, classpath should add as well.

So my main goal is to run below command using ant

Example is as below:

java -cp myjar.jar -DParameterA=A -DParameterB=B com.mypackagename.package.ClassName

I have refer below as well:

https://ant.apache.org/manual/Tasks/java.html

I am using command like:

 ant -Dcom.test=TEST1 targetName

In build.xml of ant I am trying something like below:

<target name="readclass">
    <java classname="${read-class}">
         <classpath>
           <pathelement location="myjar.jar"/>
         </classpath>
         <jvmarg value="-Xlingc"/>
         <arg value="${com.test}"/>
    </java>
</target>

But using my above code I am not able to pass arguments. any suggestion would be helpful!!

Upvotes: -1

Views: 244

Answers (2)

Shubham Jain
Shubham Jain

Reputation: 17593

Extended @Stefan Bodewig answer .. below is the ant block which works for me

<target name="readclass">
    <java classname="${read-class}" fork="true" failonerror="true">
         <classpath>
           <pathelement location="myjar.jar"/>
         </classpath>
         <sysproperty key="com.test" value="${com.test}"/>
         <sysproperty key="com.country" value="${com.country}"/>
    </java>
</target>

Upvotes: 0

user4524982
user4524982

Reputation:

There are different kinds of arguments on the command line. The arguments that come before the class name are the arguments used and consumed by the JVM and you set them via jvmarg in Ant's java task. Arguments after the class name are sent to your class and you set them via arg.

A special case of jvmarg is the system property which is set via -Dname=value before the class name when you use the command line. Ant provides sysproperty for that.

jvmargs require the task's fork attribute to be set to true as the default is to run the class in the same JVM that is executing Ant and thus the arguments to that have already been set. This does not apply to sysproperty which works with all fork settings.

If in doubt run ant -verbose which will provide you with the command line arguments Ant actually uses.

Upvotes: 1

Related Questions