116

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

看来我什至无法让我的批处理文件执行。我没主意了。

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

Runtime.
   getRuntime().
   exec("build.bat", null, new File("."));

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

4

12 回答 12

190

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

在 UNIX 上,脚本文件在文件开头有 shebang (#!) 以指定执行它的程序。Windows 中的双击由 Windows Explorer 执行。 CreateProcess对此一无所知。

Runtime.
   getRuntime().
   exec("cmd /c start \"\" build.bat");

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

于 2009-03-05T18:35:47.303 回答
22

有时线程执行处理时间高于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     

}

这样,JVM 将停止,直到您调用的进程完成,然后再继续线程执行堆栈。

于 2011-11-24T21:40:43.603 回答
20
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() );
}
于 2011-10-31T09:56:25.907 回答
14

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

于 2009-03-05T18:39:00.460 回答
14

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

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

这应该这样做。

于 2012-10-10T18:12:09.733 回答
11

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

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

从理论上讲,您也应该能够以这种方式运行 Scons,尽管我还没有对此进行测试:

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

编辑:阿马拉,你说这行不通。您列出的错误是您在 Windows 机器上从 Cygwin 终端运行 Java 时遇到的错误;这是你在做什么?问题在于 Windows 和 Cygwin 具有不同的路径,因此 Windows 版本的 Java 不会在 Cygwin 路径上找到 scons 可执行文件。如果这是您的问题,我可以进一步解释。

于 2009-03-05T18:28:30.457 回答
5
Process p = Runtime.getRuntime().exec( 
  new String[]{"cmd", "/C", "orgreg.bat"},
  null, 
  new File("D://TEST//home//libs//"));

用jdk1.5和jdk1.6测试

这对我来说很好,希望对其他人也有帮助。为了得到这个,我挣扎了更多天。:(

于 2013-06-13T12:34:32.213 回答
2

我遇到过同样的问题。但是有时 CMD 无法运行我的文件。这就是为什么我在我的桌面上创建一个 temp.bat,接下来这个 temp.bat 将运行我的文件,接下来将删除临时文件。

我知道这是一个更大的代码,但是即使 Runtime.getRuntime().exec() 失败,它也能 100% 地为我工作。

// creating a string for the Userprofile (either C:\Admin or whatever)
String userprofile = System.getenv("USERPROFILE");

BufferedWriter writer = null;
        try {
            //create a temporary file
            File logFile = new File(userprofile+"\\Desktop\\temp.bat");   
            writer = new BufferedWriter(new FileWriter(logFile));

            // Here comes the lines for the batch file!
            // First line is @echo off
            // Next line is the directory of our file
            // Then we open our file in that directory and exit the cmd
            // To seperate each line, please use \r\n
            writer.write("cd %ProgramFiles(x86)%\\SOME_FOLDER \r\nstart xyz.bat \r\nexit");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // Close the writer regardless of what happens...
                writer.close();
            } catch (Exception e) {
            }

        }

        // running our temp.bat file
        Runtime rt = Runtime.getRuntime();
        try {

            Process pr = rt.exec("cmd /c start \"\" \""+userprofile+"\\Desktop\\temp.bat" );
            pr.getOutputStream().close();
        } catch (IOException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);

        }
        // deleting our temp file
        File databl = new File(userprofile+"\\Desktop\\temp.bat");
        databl.delete();
于 2016-06-08T21:33:00.630 回答
1

以下工作正常:

String path="cmd /c start d:\\sample\\sample.bat";
Runtime rn=Runtime.getRuntime();
Process pr=rn.exec(path);
于 2015-03-11T12:26:57.003 回答
0

此代码将执行路径 C:/folders/folder 中存在的两个 commands.bat。

Runtime.getRuntime().exec("cd C:/folders/folder & call commands.bat");
于 2018-01-13T17:04:23.530 回答
0
import java.io.IOException;

public class TestBatch {

    public static void main(String[] args) {
        {
            try {
                String[] command = {"cmd.exe", "/C", "Start", "C:\\temp\\runtest.bat"};
                Process p =  Runtime.getRuntime().exec(command);           
            } catch (IOException ex) {
            }
        }

    }

}
于 2021-03-24T17:21:42.673 回答
-1

要扩展@Isha's anwser,您可以执行以下操作来获取运行的脚本的返回输出(事后不是实时):

try {
    Process process = Runtime.getRuntime().exec("cmd /c start D:\\temp\\a.bat");
    System.out.println(process.getText());
} catch(IOException e) {
    e.printStackTrace();
}
于 2018-03-14T18:08:44.823 回答