如何从Java应用程序运行批处理文件?

Ama*_*ara 104 java batch-file runtime.exec

在我的Java应用程序中,我想运行一个调用" scons -Q implicit-deps-changed build\file_load_type export\file_load_type" 的批处理文件

似乎我甚至无法执行我的批处理文件.我没有想法.

这就是我在Java中所拥有的:

Runtime.
   getRuntime().
   exec("build.bat", null, new File("."));
Run Code Online (Sandbox Code Playgroud)

以前,我有一个我想运行的Python Sconscript文件,但由于这不起作用,我决定通过批处理文件调用脚本,但该方法尚未成功.

Pau*_*des 168

批处理文件不是可执行文件.他们需要一个应用程序来运行它们(即cmd).

在UNIX上,脚本文件在文件的开头有shebang(#!),用于指定执行它的程序.双击Windows是由Windows资源管理器执行的. CreateProcess什么都不知道.

Runtime.
   getRuntime().
   exec("cmd /c start \"\" build.bat");
Run Code Online (Sandbox Code Playgroud)

注意:使用该start \"\"命令,将打开一个单独的命令窗口,其中包含空白标题,批处理文件中的任何输出都将显示在那里.它也应该只使用`cmd/c build.bat",在这种情况下,如果需要,可以从Java中的子进程读取输出.


小智 21

有时线程执行进程时间高于JVM线程等待进程时间,它会在您调用的进程需要一些时间进行处理时发生,请使用waitFor()命令,如下所示:

try{    
    Process p = Runtime.getRuntime().exec("file location here, don't forget using / instead of \\ to make it interoperable");
    p.waitFor();

}catch( IOException ex ){
    //Validate the case the file can't be accesed (not enought permissions)

}catch( InterruptedException ex ){
    //Validate the case the process is being stopped by some external situation     

}
Run Code Online (Sandbox Code Playgroud)

这样,JVM将停止,直到您正在调用的进程在继续执行线程执行堆栈之前完成.


小智 19

Runtime runtime = Runtime.getRuntime();
try {
    Process p1 = runtime.exec("cmd /c start D:\\temp\\a.bat");
    InputStream is = p1.getInputStream();
    int i = 0;
    while( (i = is.read() ) != -1) {
        System.out.print((char)i);
    }
} catch(IOException ioException) {
    System.out.println(ioException.getMessage() );
}
Run Code Online (Sandbox Code Playgroud)

  • 注释该代码并告诉我们InputStream为何以及读什么,以及我为什么在意,这将很有用。另外,批处理文件的代码运行良好,但是我无法获取它以引发错误Exception。 (2认同)
  • 在我的代码中,有一个令人困惑的变量名称为"is",这让我很难过. (2认同)

小智 14

如果您正在谈论的话,使用java运行批处理文件...

String path="cmd /c start d:\\sample\\sample.bat";
Runtime rn=Runtime.getRuntime();
Process pr=rn.exec(path);`
Run Code Online (Sandbox Code Playgroud)

这应该做到这一点.

  • 已经用一个有效的解决方案回答了这个问题.您应该只提供您知道正在使用的解决方案并描述您认为解决方案可能更好的原因. (10认同)

bas*_*ero 12

ProcessBuilder是运行外部进程的Java 5/6方式.

  • 为什么 ProcessBuilder 是 Java 5/6 的方式? (2认同)
  • 复活旧帖子的有趣选择... ProcessBuilder提供更多控制,特别是能够轻松地将stderr重定向到stdout.我也发现设置更直观,但这是个人的首选 (2认同)

Eli*_*ght 10

用于运行批处理脚本的可执行文件cmd.exe使用该/c标志来指定要运行的批处理文件的名称:

Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "build.bat"});
Run Code Online (Sandbox Code Playgroud)

从理论上讲,你也应该能够以这种方式运行Scons,尽管我没有测试过这个:

Runtime.getRuntime().exec(new String[]{"scons", "-Q", "implicit-deps-changed", "build\file_load_type", "export\file_load_type"});
Run Code Online (Sandbox Code Playgroud)

编辑:阿马拉,你说这不起作用.您列出的错误是从Windows框上的Cygwin终端运行Java时出现的错误; 这是你在做什么的?问题是Windows和Cygwin有不同的路径,因此Windows版本的Java将无法在Cygwin路径上找到scons可执行文件.如果事实证明这是你的问题,我可以进一步解释.


小智 5

Process p = Runtime.getRuntime().exec( 
  new String[]{"cmd", "/C", "orgreg.bat"},
  null, 
  new File("D://TEST//home//libs//"));
Run Code Online (Sandbox Code Playgroud)

使用jdk1.5和jdk1.6进行测试

这对我来说效果很好,希望对其他人也有帮助。为了得到这个我已经奋斗了更多天。:(