2
votes

When I try to call simple JAR file via ANT. Whenever i execute I get the following error:

C:\temp\My\My_Ant.xml:20: Execute failed: java.io.IOException: Cannot run program "C:\PROGRA~1\Java\jre7\bin\java.exe -jar C:\temp\My\javatest.jar" (in directory "C:\temp\My"): CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
    at java.lang.Runtime.exec(Runtime.java:617)
    at org.apache.tools.ant.taskdefs.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:58)
    at org.apache.tools.ant.taskdefs.Execute.launch(Execute.java:426)
    at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:440)
    at org.apache.tools.ant.taskdefs.ExecTask.runExecute(ExecTask.java:629)
    at org.apache.tools.ant.taskdefs.ExecTask.runExec(ExecTask.java:670)
    at org.apache.tools.ant.taskdefs.ExecTask.execute(ExecTask.java:496)
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    at org.apache.tools.ant.Task.perform(Task.java:348)
    at org.apache.tools.ant.Target.execute(Target.java:435)
    at org.apache.tools.ant.Target.performTasks(Target.java:456)
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
    at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
    at org.apache.tools.ant.Main.runBuild(Main.java:854)
    at org.apache.tools.ant.Main.startAnt(Main.java:236)
    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:285)
    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:112)

Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(ProcessImpl.java:385) at java.lang.ProcessImpl.start(ProcessImpl.java:136) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028) ... 24 more

The Java program is simply printing "hello world!"

I call the ANT in the following way:

C:\Tools\ANT\apache-ant-1.9.7\bin\ant -buildfile My_Ant.xml

Any idea why I am getting this? when I run this via command line I get the correct message.

C:\temp\My>C:\PROGRA~1\Java\jre7\bin\java.exe -jar JavaTest.jar
hello from Java

Edit:

Ant Script:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Issue Management" default="startActivity"  xmlns:if="ant:if" xmlns:unless="ant:unless">
    <property name="javaPath" value="C:\PROGRA~1\Java\jre7\bin\java.exe"/>
    <property name="AnalyzerPath" value="C:\temp\my"/>
    <property name="Analyzer" value="javatest.jar"/>
    <!--Setting the location of ANT Contrib starts 
    <taskdef resource="net/sf/antcontrib/antlib.xml">
         <classpath>
            <pathelement location="C:/Tools/ANT/apache-ant-1.8.2/ant-contrib-1.0b3.jar" />
         </classpath>
    </taskdef>-->
    <tstamp>
        <format property="current.time" pattern="yyyyMMdd_HHmmss" />        
    </tstamp>
    <target name="startActivity">       
        <echo>Issue Management script started at ${current.time}</echo>
        <exec executable="${javaPath} -jar ${AnalyzerPath}\${Analyzer}" resultproperty="BuildErrorCode" failonerror="true" dir="${AnalyzerPath}">            
            <arg value="--help"/>
        </exec>
        <echo>Issue Management script ended at ${current.time}</echo>
    </target>       
</project>
1
Can you add the buildfile snippet relevant to the error? - martin clayton
@martinclayton Added. Thanks for your time - KK99

1 Answers

2
votes

The executable parameter needs to be set to the name or path of just the executable; command arguments should be specified separately using nested <arg> elements, for example:

<exec executable="${javaPath}" resultproperty="BuildErrorCode"
      failonerror="true" dir="${AnalyzerPath}">  
    <arg value="-jar" />
    <arg value="${AnalyzerPath}\${Analyzer}" />       
    <arg value="--help" />
</exec>

When you specified the whole command line as the executable, it was treated as a single command with embedded spaces, and so was not found:

Cannot run program "C:\PROGRA~1\Java\jre7\bin\java.exe -jar C:\temp\My\javatest.jar"
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^