简体   繁体   English

运行时进程执行问题

[英]Problem with Runtime Process Execution

Can somebody find what is wrong with this code: 有人可以找到这段代码有什么问题吗:

Runtime rt = Runtime.getRuntime();
Process pr;
File myFolder = new File("C:\\Temp");
pr = rt.exec("myExec.bat", null, myFolder);
pr.waitFor();
pr.destroy();

When I run this code, I get following exception (while file and folder used exist as specified): 当我运行此代码时,出现以下异常(使用的文件和文件夹按指定存在):

java.io.IOException: Cannot run program "myExec.bat" (in directory "C:\Temp"): CreateProcess 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 java.lang.Runtime.exec(Runtime.java:431)
    at com.radml.radmlp.main(Test.java:10)

rt.exec expects a file with no path information to be in the user dir and not in the directory you specify to use as working directory. rt.exec希望没有路径信息的文件位于用户目录中,而不位于您指定用作工作目录的目录中。 Using it this way 用这种方式

    Runtime rt = Runtime.getRuntime();
    Process pr;
    File myFolder = new File("C:\\Temp");
    pr = rt.exec(new File(myFolder, "myExec.bat").getAbsolutePath(), null, myFolder);
    pr.waitFor();
    pr.destroy();

should work as long as your file c:\\Temp\\myExec.bat exists. 只要文件c:\\ Temp \\ myExec.bat存在,它就应该起作用。

Greetz, GHad 加的特Greetz

Have you made sure that your bat file is located in " C:\\Temp\\myExec.bat "? 您确定您的bat文件位于“ C:\\Temp\\myExec.bat ”中吗?

(Just a guess, but make sure the file isn't actually called C:\\Temp\\myExec.bat .txt ) (只是一个猜测,但请确保该文件实际上未称为 C:\\Temp\\myExec.bat .txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM