从Java执行不正确的*.bat
我有一个.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\\
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下面的答案。