Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java执行不正确的*.bat_Java_Batch File - Fatal编程技术网

从Java执行不正确的*.bat

从Java执行不正确的*.bat,java,batch-file,Java,Batch File,我有一个.bat文件,其中包含以下内容,例如: mkdir testDir Runtime.getRuntime().exec("cmd /c start C:\\temp\\test.bat", null, new File("c:\\temp")); 现在我把它放在文件夹C:\temp中 然后我想使用java运行它,因此我执行以下操作: Runtime.getRuntime().exec("cmd /c start C:\\temp\\

我有一个.bat文件,其中包含以下内容,例如:

mkdir testDir
Runtime.getRuntime().exec("cmd /c start C:\\temp\\test.bat", null,
                          new File("c:\\temp"));
现在我把它放在文件夹C:\temp中 然后我想使用java运行它,因此我执行以下操作:

Runtime.getRuntime().exec("cmd /c start C:\\temp\\test.bat");
我希望在手动执行此文件时,该文件夹将在C:\temp中创建,但在我的工作区中创建的文件夹是错误的。
如何修复它?

运行
cmd
时,需要指定工作目录

Runtime.exec()
的重载允许您指定工作目录。例如:

mkdir testDir
Runtime.getRuntime().exec("cmd /c start C:\\temp\\test.bat", null,
                          new File("c:\\temp"));
或者,您可以使用更明确地控制正在启动的流程的各个方面。

具体来说,该副本中的内容可能是最好的,并且符合Jon下面的答案。