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.
antcommand should beant.batunder windowsString. This greatly reduces the complexity of arguments that contain spaces, for example...ProcessBuilder, it will reduce the complexity of usingProcessFor example...
I should also point out that
antis 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.batdirectly, then you will need run the batch file via the command processor