有人帮我用java执行命令吗

有人帮我用java执行命令吗,java,windows,cmd,Java,Windows,Cmd,当通过java web应用程序执行批处理文件时,出现如下错误描述 我不知道为什么只有案例1按预期工作,在案例2、3、4中,只执行批处理文件的一部分。有人能向我解释为什么吗?非常感谢。 使用Runtime.getruntime().exec(命令) case1. cmd /c start C:\mytest.bat case2. cmd /c start /b C:\mytest.bat case3. cmd /c C:\mytest.bat case4. C:\mytest.bat mytes

当通过java web应用程序执行批处理文件时,出现如下错误描述

我不知道为什么只有案例1按预期工作,在案例2、3、4中,只执行批处理文件的一部分。有人能向我解释为什么吗?非常感谢。

使用
Runtime.getruntime().exec(命令)

case1. cmd /c start C:\mytest.bat
case2. cmd /c start /b C:\mytest.bat
case3. cmd /c C:\mytest.bat
case4. C:\mytest.bat
mytest.bat

echo line1 >>%~dp0test.txt
echo line2 >>%~dp0test.txt
echo line3 >>%~dp0test.txt
echo line4 >>%~dp0test.txt
echo line5 >>%~dp0test.txt
echo line6 >>%~dp0test.txt
echo line7 >>%~dp0test.txt
echo line8 >>%~dp0test.txt
echo line9 >>%~dp0test.txt
echo line10 >>%~dp0test.txt
echo line11 >>%~dp0test.txt
echo line12 >>%~dp0test.txt
echo line13 >>%~dp0test.txt
echo line14 >>%~dp0test.txt
echo line15 >>%~dp0test.txt
echo line16 >>%~dp0test.txt
echo line17 >>%~dp0test.txt
echo line18 >>%~dp0test.txt
echo line19 >>%~dp0test.txt
echo line20 >>%~dp0test.txt
exit
结果测试.txt

echo line1 >>%~dp0test.txt
echo line2 >>%~dp0test.txt
echo line3 >>%~dp0test.txt
echo line4 >>%~dp0test.txt
echo line5 >>%~dp0test.txt
echo line6 >>%~dp0test.txt
echo line7 >>%~dp0test.txt
echo line8 >>%~dp0test.txt
echo line9 >>%~dp0test.txt
echo line10 >>%~dp0test.txt
echo line11 >>%~dp0test.txt
echo line12 >>%~dp0test.txt
echo line13 >>%~dp0test.txt
echo line14 >>%~dp0test.txt
echo line15 >>%~dp0test.txt
echo line16 >>%~dp0test.txt
echo line17 >>%~dp0test.txt
echo line18 >>%~dp0test.txt
echo line19 >>%~dp0test.txt
echo line20 >>%~dp0test.txt
exit
案例1:

line1 
line2 
line3 
line4 
line5 
line6 
line7 
line8 
line9 
line10 
line11 
line12 
line13 
line14 
line15 
line16 
line17 
line18 
line19 
line20 
案例2、3、4:

line1
line2
line3
line4
line5

这可能是因为您的程序在底层进程(执行
mytext.bat
)完成之前终止。在第一种情况下,您使用
start
,它在自己的环境中开始执行,因此即使其父级终止,执行也会继续。所有其他命令在当前环境中执行批处理文件,并随应用程序终止

要解决此问题,您必须等待执行
mytext.bat
完成。有几种方法可以做到这一点,但我建议使用Process Builder:

ProcessBuilder b = new ProcessBuilder("cmd", "/c", "C:\\mytest.bat");
Process p = b.start();
p.waitFor();
要使用您的方法:

Process p = Runtime.getruntime().exec(command)
p.waitFor();

当您在没有打开带有/b选项的窗口或没有启动的情况下执行命令时,需要保持暂停。在这里,我暂停了一秒钟,程序运行正常

public class MyTest{
    public static void main(String args[]) throws Exception{
        //Runtime.getRuntime().exec("cmd /c start D:\\mytest.bat");//No pause required
        Runtime.getRuntime().exec("cmd /c start /b D:\\mytest.bat");//pause required
        //Runtime.getRuntime().exec("cmd /c D:\\mytest.bat");//pause required
        //Runtime.getRuntime().exec("D:\\mytest.bat");//pause required
        Thread.sleep(1000);//Pause for one second
    }
}

只需从第二个命令开始添加/等待命令,如下所示:

 cmd /c start C:\mytest.bat
 case2. cmd /c start /wait /b C:\mytest.bat
 case3. cmd /c /wait C:\mytest.bat
 case4. C:\mytest.bat

暂停一秒钟仅在相关命令在该秒钟内终止时有效。任何超过此值的命令都将遭受意外终止。确实如此,但基于OP要求,这就足够了
Process.waitFor()
也没有工作。
Process.waitFor()
应该已经工作了。为什么没有呢?(您是否检查了正确的终止?)您应该注意到
p.waitFor()
可以抛出一个
InterruptedException
,然后应该处理该异常。