ANT error CreateProcess error=2 - ant

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>

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"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Related

Apache Ant: Unable to call Composer's vendor/bin scripts

I am using Apache Ant for my builds. I have some composer scripts belonging to several vendors in vendor/bin folder. I have added this folder to the system path and if I run the commands on my command window in works but in the build file i get an error.
Is there anything I should be doing differently? Before is an example:
<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject" default="build" basedir=".">
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="phpcpd">
<arg value="--version" />
</exec>
</target>
</project>'
I get this when I run ant phpcpd
phpcpd:
BUILD FAILED
C:\xxxxxx\xxxxxxx\build.xml:96: Execute failed: java.io.IO Exce
ption: Cannot run program "phpcpd": CreateProcess error=2, The system
cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041)
at java.lang.Runtime.exec(Runtime.java:617)
at org.apache.tools.ant.taskdefs.launcher.Java13CommandLauncher.exec(Jav
a13CommandLauncher.java:41)
at org.apache.tools.ant.taskdefs.Execute.launch(Execute.java:428)
at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:442)
at org.apache.tools.ant.taskdefs.ExecTask.runExecute(ExecTask.java:628)
at org.apache.tools.ant.taskdefs.ExecTask.runExec(ExecTask.java:669)
at org.apache.tools.ant.taskdefs.ExecTask.execute(ExecTask.java:495)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
...
But phpcpd --version works on command prompt
ANT is not aware where phpcpd is as it doesn't share path with your Command Promt.
One way around it is to create a .bat file to run phpcpd
Create a phpcpd.bat with the following:
#echo off
phpcpd --version
Your build scripts to be updated from :
<exec executable="phpcpd">
<arg value="--version" />
</exec>
To: <exec executable="phpcpd.bat"/>
Above assumed with Windows Command Promt
My solution was to use the .phar files of the scripts. That way, the build file became platform independent to a large extent. So
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="phpcpd">
<arg value="--version" />
</exec>
</target>
Became:
<target name="phpcpd" description="Find duplicate code using PHPCPD">
<exec executable="php">
<arg value="${phpcpd}" />
<arg value="--version" />
</exec>
</target>
Where ${phpcpd} is the path to the phar file
I'm using absolute paths and a configurable executable.properties like this:
build.xml
<project name="build">
<property file="executable.properties" />
<target name="run-phpcd" unless="${phpcpd.skip}">
<exec executable="${phpcpd.executable}"><!-- .. --></exec>
</target>
</project>
executable.dist.properties
phpcpd.skip = no
#phpcpd.executable = C:\path\to\phpcpd.bat
phpcpd.executable = /path/to/phpcpd.sh
Both files can be committed to your VCS, for usage copy the template file (*.dist.properties) and rename it to executable.properties. Add this file to the ignore list of your VCS.

how to run the java class file using ant

I am trying to execute the class file in a jar through ant script. I ran the command through CMD it worked fine
Command is like :-
java com.CodeReview -p “<Project Path>”
I tried to run this command through the ant script
<target name="build">
<java fork="true" failonerror="yes" classname="com.CodeReview">
<arg line="-p"/>
<arg line="D:\Test"/>
</java>
</target>
It is giving me error
build:
[java] Error: Could not find or load main class com.CodeReview
but through command line it is working and no issue with classpath. Do I need to set classpath in script also.
You are missing the classpath within your java task:
<classpath>
<pathelement location="yourjar.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>

How to change the Path variable in ant exec task?

I tried setting the Path variable in ant exec task using this link. But its not working somehow. I am trying to add THRIFT_HOME which is set to basedir currently and I have the thrift.exe file in the basedir.
Also, when I change executable attribute value to thrift.exe, the thrift compiler works properly but the basedir is not added to the Path.
<exec executable="thrift">
<env key="Path" value="${env.Path};${THRIFT_HOME}" />
</exec>
I also tried using the setx command as:
<exec executable="setx">
<arg value="Path"/>
<arg value="%Path%;${THRIFT_HOME}"/>
</exec>
but it still does not work.
Any help would be appreciated!
how about this:
<property environment="env"/>
<!-- to see if you can access it -->
<echo message="${env.Path}" />
<exec executable="thrift">
<env key="Path" value="${env.Path};${THRIFT_HOME}" />
</exec>
I've found a hint here: http://www.factsandpeople.com/facts-mainmenu-5/23-other-software-technologies/125-using-environment-variables-in-ant-environment-variables-are-not-evaluated
I am currently using on Windows 7 and the problem was with spaces in the folder name. That's a big problem when working on Windows.
I tried placing the thrift executable at /d/thrift/ and it worked and assigning THRIFT_HOME = /d/thrift. Then I tried /d/thrift demo/ and there is the problem.
D:\noknok_fido_to_ostp\noknok_fido_to_ostp_workspace\Demo\build.xml:33: Execute failed: java.io.IOException: Cannot run program "thrift-0.9.0.exe": CreateProces
s error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at java.lang.Runtime.exec(Runtime.java:593)
at org.apache.tools.ant.taskdefs.Execute$Java13CommandLauncher.exec(Execute.java:862)
at org.apache.tools.ant.taskdefs.Execute.launch(Execute.java:481)
at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:495)
at org.apache.tools.ant.taskdefs.ExecTask.runExecute(ExecTask.java:631)
at org.apache.tools.ant.taskdefs.ExecTask.runExec(ExecTask.java:672)
at org.apache.tools.ant.taskdefs.ExecTask.execute(ExecTask.java:498)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
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:392)
at org.apache.tools.ant.Target.performTasks(Target.java:413)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:811)
at org.apache.tools.ant.Main.startAnt(Main.java:217)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:280)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
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.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
... 23 more
Another problem is that /d/thrift doesn't work in Eclipse Ant plugin build. I tried it using command prompt and the build was successful.
This teaches a new lesson: Always build from command prompt!
Thanks to all who answered for this question.
And what about searchpath attribute ?
searchpath: When this attribute is true, then system path environment variables will be searched when resolving the location of the executable. since Ant 1.6.3
So I think something like this:
<exec executable="thrift" searchpath="true">
<env key="Path" value="${env.Path};${THRIFT_HOME}" />
</exec>
should work.
I found it works by quote the value of variable
<exec executable="setx">
<arg line="Path "${env.Path};c:\test Path"" />
<arg line="/m" />
</exec>
https://stackoverflow.com/a/23777929/3659861

Fitnesse SymbolicLink not working with external link from ant or command line

We are trying to create a symbolic link in our fitnesse suite to an external folder. It is working perfectly with URL. But not working with command line or with ant script.
Following is the URL (Working):
http://localhost:8080/root?responder=symlink&linkName=FitNesseSuite&linkPath=file://FitNesseRoot/TestSuite
Command line (Not working):
java -jar fitnesse.jar -c "root?responder=symlink&linkName=FitNesseSuite&linkPath=file://FitNesseRoot/TestSuite"
Ant script (Not working):
<target name="my_fitnesse_tests">
<java jar="fitnesse.jar" failonerror="true" fork="true">
<arg value="-c" />
<arg value="root?responder=symlink&linkName=FitNesseSuite&linkPath=file://FitNesseRoot/TestSuite" />
<arg value="-p" />
<arg value="9234" />
</java>
</target>
Getting following exception:
Exception in thread "main" java.lang.IllegalArgumentException: Command
specification
[root?responder=symlink&linkName=FitNesseSuite&linkPath=file://FitNesseRoot/TestSuite]
invalid. Format shold be /cmd or user:pass:/cmd at
fitnesse.http.MockRequestBuilder.validate(MockRequestBuilder.java:48)
Please help us to solve this.
Same problem, I have solve the problem with the following command:
...&linkPath=file%3A///... ==> change the ":" by "%3A"

Determine which file has an error when running YUI compressor from Ant

We compress our javascript (and css files) with YUI compressor during our ant build task running on our Jenkins CI Server. However, it is very difficult to determine which js files YUI compressor is having errors with. We see a bunch of things like:
[minify-js] [ERROR] 3:35:unterminated string literal
[minify-js]
[minify-js] [ERROR] 3:35:syntax error
[minify-js]
[minify-js] [ERROR] 4:8:syntax error
[minify-js]
[minify-js] [ERROR] 1:0:Compilation produced 3 syntax errors.
[minify-js] org.mozilla.javascript.EvaluatorException: Compilation produced 3 syntax errors.
[minify-js] at com.yahoo.platform.yui.compressor.YUICompressor$1.runtimeError(YUICompressor.java:135)
[minify-js] at org.mozilla.javascript.Parser.parse(Parser.java:410)
[minify-js] at org.mozilla.javascript.Parser.parse(Parser.java:355)
[minify-js] at com.yahoo.platform.yui.compressor.JavaScriptCompressor.parse(JavaScriptCompressor.java:312)
[minify-js] at com.yahoo.platform.yui.compressor.JavaScriptCompressor.(JavaScriptCompressor.java:533)
[minify-js] at com.yahoo.platform.yui.compressor.YUICompressor.main(YUICompressor.java:112)
[minify-js] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[minify-js] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[minify-js] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[minify-js] at java.lang.reflect.Method.invoke(Method.java:616)
[minify-js] at com.yahoo.platform.yui.compressor.Bootstrap.main(Bootstrap.java:20)
[minify-js] Result: 2
in the output, but I have no idea which of the hundreds of JS files the error is coming from. Our ant task looks like:
<target name="minify-js">
<apply executable="yuicompressor" parallel="false" dest="${global.dir}/" taskname="minify-js" force="true">
<fileset dir="${global.dir}/" includes="**/*.js">
<exclude name="*.min.js" />
</fileset>
<arg value="--type=js" />
<srcfile />
<arg value="-o" />
<targetfile />
<mapper type="identity" />
</apply>
</target>
Not being an expert on Ant or YUI compressor, is there something we can do so that the filename where there error is happening is output somewhere?
I don't know how the yuicompressor works, I assume that it works on one file at a time.
If this is true, you can do it with for from ant-contrib. You would need to install ant-contrib prior.
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="${global.dir}/bin_data/ant-contrib-0.6.jar"/>
<for param="file">
<path>
<fileset dir="${global.dir}/" includes="**/*.js">
<exclude name="*.min.js" />
</fileset>
</path>
<sequential>
<echo>youcompressor for #{file}</echo> <!-- Will output each file and help debugging -->
<exec executable="yuicompressor"> <!-- I took the args from the official documentation-->
<arg value="--type=js" />
<arg value="-o" />
<arg value="'.js$:-min.js'" />
<arg value="#{file}" />
</exec>
</sequential>
</for>
Try using this option:
-v, --verbose
Display informational messages and warnings.
There is a nice entry in the documentation for a cases like yours:
Don't hesitate to use the -v option. Although not a replacement for
JSLint, it will output some helpful hints when it senses that
something might be wrong with your code.
Did you try
<apply … verbose="true">
Whether to print a summary after execution or not. Since Ant 1.6.
Ideally it would print a statement before attempting to execute on the file, but having a summary afterwards at least lets you see the last successful file, which should point you to the broken file as the filesets are usually alphanumerically sequenced.

Resources