开发者

executing runtime cmd from java program

开发者 https://www.devze.com 2023-03-17 20:45 出处:网络
I want to run my Ant script i.e (build.xml) through my java program , following is part of mycode Runtime rt = Runtime.getRuntime();

I want to run my Ant script i.e (build.xml) through my java program , following is part of mycode

  Runtime rt = Runtime.getRuntime();
  Process proc = rt.exec("build.xml");

but I'm getting the following error

java.io.IOException: Cannot run program "build.xml": CreateProcess error=2, The system cannot find the file specified
    at java.lang.开发者_JS百科ProcessBuilder.start(ProcessBuilder.java:460)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:431)
    at java.lang.Runtime.exec(Runtime.java:328)
    at com.infotech.RunCmd.main(RunCmd.java:12)
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)
    ... 4 more

How to solve it?


You should execute ant -buildfile build.xml, so use rt.exec("ant -buildfile build.xml"); (build.xml is not a command) if build.xml is not in the application folder, you will need to give its actual path.


Is build.xml actually in the directory you are running from, have you tried putting the file directory path in rt.exec()?


You do not invoke "build.xml" from command line, but "ant" that looks for a "build.xml" in your current directory. So change your code (assuming ant launcher is accessible from your PATH) to:

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("ant");


For a start, the executable is ant. The command parameter is build.xml.

Secondly, you need to use absolute paths for your files/executables or be sure of your runtime environment's directory

0

精彩评论

暂无评论...
验证码 换一张
取 消