Use Ant to launch Java program with cmd.exe "start" command - java

<exec dir="${basedir}" executable="cmd.exe">
<arg line="start cmd /c java -jar ${jar.file} "/>
</exec>
In Eclipse this does not work, I would like to open my app.
The reason is io.Console because this I can't execute the app in Eclipse.

cmd appears in two places: In the executable attribute and in the <arg>. It should only appear in the executable attribute.
Further, the /c option of cmd.exe should appear before the start command, not after.
Try the following:
<exec executable="cmd.exe" failonerror="true">
<arg value="/c"/>
<arg value="start"/>
<arg value="java"/>
<arg value="-jar"/>
<arg value="${jar.file}"/>
</exec>

Related

Adding autoprefix-cli to ANT build

I am trying to add autoprefix-cli to my ANT build. Below is my code.
<target name="auto">
<apply executable="autoprefixer-cli.bat" verbose="true" force="true" failonerror="true">
<arg value="-d" /> <!-- Turn on verbose -->
<arg value="prefix" />
<arg value="*.css" />
</apply>
</target>
When i do a ant build, it gives me an error saying resource not specified.
BUILD FAILED
D:\tempTest\AntTestProject\build.xml:25: no resources specified
Note: I can access autoprefix-cli from command line, its installed with -g flag and also it works when i directly use it from commandline.
The apply task basically loops the exec task on a batch of resources (files, directories, URLs, etc). If you only want to run one command, use exec instead.
However, you will likely also need to alter your command. From Ant's exec task documentation:
Note that .bat files cannot in general by executed directly. One
normally needs to execute the command shell executable cmd using the
/c switch.
https://ant.apache.org/manual/Tasks/exec.html
So instead you should have:
<exec executable="cmd" verbose="true" force="true" failonerror="true">
<arg value="/c" />
<arg value="autoprefixer-cli.bat" />
<arg value="-d" />
<arg value="prefix" />
<arg value="*.css" />
</exec>

Getting illegal character error while using closure-compiler.jar using ANT for r.js optimization

I am using closure compiler jar for minification in r.js optimization in windows environment.
While running this task using ANT exec, getting illegal character error but while running same task using .bat file it working fine.
ANT exec task
<target name="do-optimization" description="It will do optimization using r.js.">
<exec dir="." executable="java" failonerror="true">
<arg value="-jar" />
<arg path="${src.dir}/r-js/lib/rhino/js.jar" />
<arg path="${src.dir}/r-js/lib/closure/compiler.jar" />
<arg path="${src.dir}/r-js/dist/r.js" />
<arg value="-o"/>
<arg path="${src.dir}/r-js/build.js" />
</exec>
</target>
console output
do-optimization:
[exec] js: "C:\workspace\test\ui\r-js\lib\closure\compiler.jar", line 2: illegal character
[exec] js: ╝MOC ♦ META-INF/■╩ PK♥♦
[exec] js: ^
[exec] js: "C:\workspace\test\ui\r-js\lib\closure\compiler.jar", line 1: Compilation produced 1 syntax errors.
[exec]
BUILD FAILED
optimize.bat
java -classpath "r-js\lib\rhino\js.jar";"r-js\lib\closure\compiler.jar" org.mozilla.javascript.tools.shell.Main r-js/dist/r.js -o build.js
It doesn't seem that the Ant script is calling java the same way as the batch file. The exec task is calling the following command:
java -jar ${src.dir}/r-js/lib/rhino/js.jar ${src.dir}/r-js/lib/closure/compiler.jar ${src.dir}/r-js/dist/r.js -o ${src.dir}/r-js/build.js
which is clearly different than the one in the .bat file, i.e. the jars are not being added to the classpath properly.
In Ant, you can simply use the java task to run a Java class. Try using the following:
<java classname="org.mozilla.javascript.tools.shell.Main" failonerror="true">
<arg path="${src.dir}/r-js/dist/r.js" />
<arg value="-o"/>
<arg path="${src.dir}/r-js/build.js" />
<classpath>
<pathelement location="${src.dir}/r-js/lib/rhino/js.jar" />
<pathelement location="${src.dir}/r-js/lib/closure/compiler.jar" />
</classpath>
</java>

Ant command line arguments

Program works fine when run with eclipse run configurations, but when run with ant, it is unable to parse int from args[0], which I do not understand. Full code is available here https://gist.github.com/4108950/e984a581d5e9de889eaf0c8faf0e57752e825a97
I believe it has something to do with ant,
target name="run" description="run the project">
java dir="${build.dir}" classname="BinarySearchTree" fork="yes">
<arg value="6 in.txt"/>
/java>
/target>
the arg value will be changed via the -D flag, as in ant -Dargs="6 testData1.txt" run.
Any help would be much appreciated, it is very frustrating.
You need to supply the arguments as two different arg values:
<target name="run" description="run the project">
<java dir="${build.dir}" classname="BinarySearchTree" fork="yes">
<arg value="6" />
<arg value="in.txt" />
</java>
</target>
You can also use the line attribute; From the ANT docs:
<arg value="-l -a"/>
is a single command-line argument containing a space character, not separate commands "-> l" and "-a".
<arg line="-l -a"/>
This is a command line with two separate arguments, "-l" and "-a".
Expanding epoch 's answer.
java task supports sysproperty and jvmarg.
For example (from ant java task page)
<java classname="test.Main"
fork="yes" >
<sysproperty key="DEBUG" value="true"/>
<arg value="-h"/>
<jvmarg value="-Xrunhprof:cpu=samples,file=log.txt,depth=3"/> </java>
So you could construct the args from the command line passed to ant.
<target name="run" description="run the project">
<java dir="${build.dir}" classname="BinarySearchTree" fork="yes">
<sysproperty key="testarg" value="${testarg}"
<arg value="${arg1}" />
<arg value="${arg2}" />
</java>
</target>
Now if you call ant with ant -Dtestarg=test1234 -Darg1=6 -Darg2=in.txt, then testarg will be available via property. Others will become normal arguments to the java program.

Ant & Mercurial

I am trying to add some tag information in my ant script using the following target but I get an error (Result=-1) and hg tells me it does not recognise the command:
<target name="-post-init">
<exec outputproperty="hg.tag" executable="hg">
<arg value="parents --template {latesttag}+{latesttagdistance}" />
</exec>
</target>
If I only include value="parents" it works fine.
If I run the command line hg parents --template {latesttag}+{latesttagdistance} it works fine too.
Any ideas on what is wrong in my syntax?
Just tried this and it works fine:
<exec outputproperty="hg.tag" executable="hg">
<arg value="parents" />
<arg value="--template" />
<arg value="{latesttag}+{latesttagdistance}" />
</exec>
I needed to split the arguments.

How to pass a variable number of arguments to ant exec

I have a ant target that takes a variable number of arguments that are to be passed to an exec task. Using the old mechanism it is trivial:
<exec command="cmd /c ${_full-path-to-exec}" osfamily="windows" failonerror="true">
</exec>
However, use of 'command' is deprecated in favor of nested elements. like this:
<exec executable="cmd" osfamily="windows" failonerror="true">
<arg value="/c"/>
<arg file="${_full-path-to-exec}"/>
<arg value="${_param-one}"/>
<arg value="${_param-two}"/>
<arg value="${_param-three}"/>
</exec>
which makes variable argument lists impossible.
How to solve this problem?
How about this:
<arg line="whatever args you need"/>

Categories