I want to run a simple ant -v command through a java program. After searching, I came to a point where I know I have to use shell execute instead of process, since ant is not really an exe format. I have a program that works for executing simple JAVAC command, but ant never works. Here is what my code looks like.
Runtime runtime = Runtime.getRuntime() ;
Process shellProcess = runtime.exec("D:\\installs\\apache-ant-1.9.2\\bin\\ant -v") ;
shellProcess.waitFor() ;
I get following exception when I run this
Exception in thread "main" java.io.IOException: Cannot run program "D:\installs\apache-ant-1.9.2\bin\ant": CreateProcess error=193, %1 is not a valid Win32 application
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at com.check.SystemCheck.main(SystemCheck.java:14)
Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
The reason for specifying the whole path is, if I don't I get file not found error. Any idea on how to get this working??
A number of things jump out at me.
ant
command should beant.bat
under windowsString
. This greatly reduces the complexity of arguments that contain spaces, for example...ProcessBuilder
, it will reduce the complexity of usingProcess
For example...
I should also point out that
ant
is a linux/unix shell script, which Windows won't know what to do with ;)Updated
I've tested this using Windows 7 and Java 7, works just fine
I've tested this using Windows 7 and Java 6, works just fine...
If you do continue to have issues running the
ant.bat
directly, then you will need run the batch file via the command processorParameters to the executable must be passed in separately:
In your attempt, the executable file is literally
ant -v
(ie with a space in the filename and ending with dash v).I would skip the bat file and run the launcher jar directly
But as suggested in other answers you'll probably get a better result by calling ant's Java API directly rather than using an external process. Given your example in the question uses
-v
I presume you're trying to capture something from the verbose output of the build. If you invoke ant via its API you can register a listener on theProject
and ant will deliver log messages directly to your listener rather than you having to parse them out of the process output.Ant is written in Java. Why not directly call its classes in the same Java process instead of launching an external process?
There was an interesting answer posted here.
Here is the code as posted on that answer:
It will executed the default target of the build.xml file. Cool isn't it?
For me, the main advantage is that you can control the execution better (you can easily know if something failed for example) and it is of course possible to get the output the script would generate.
Try
You can also do the following
In addition to Aniket's answer, to make things even simplier: