在我的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文件,但由于这不起作用,我决定通过批处理文件调用该脚本,但该方法尚未成功。

有帮助吗?

解决方案

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

在UNIX上,脚本文件在文件的开头有shebang(#!),用于指定执行它的程序。双击Windows是由Windows资源管理器执行的。 CreateProcess 对此一无所知。

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

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

其他提示

有时线程执行进程时间高于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将停止,直到您正在调用的进程在继续执行线程执行堆栈之前完成。

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() );
}

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

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

这应该这样做。

ProcessBuilder 是Java 5 / 6种方式来运行外部流程。

用于运行批处理脚本的可执行文件是 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可执行文件。如果事实证明这是你的问题,我可以进一步解释。

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

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

这对我来说很好,希望它也能帮助别人。 为了得到这个,我已经挣扎了更多的日子。 :(

我有同样的问题。但有时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();

以下工作正常:

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

此代码将执行路径C:/ folders / folder。

中存在的两个commands.bat
Runtime.getRuntime().exec("cd C:/folders/folder & call commands.bat");

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

try {
    Process process = Runtime.getRuntime().exec("cmd /c start D:\\temp\\a.bat");
    System.out.println(process.getText());
} catch(IOException e) {
    e.printStackTrace();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top