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

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

看起来我甚至不能执行我的批处理文件,我没办法了。

This is what I have in Java:

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

之前,我有一个想要运行的 Python Sconscript 文件,但是由于这个文件不能工作,我决定通过一个批处理文件来调用这个脚本,但是这个方法到目前为止还没有成功。

321588 次浏览

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

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

理论上你也可以用这种方式运行 Scon,虽然我还没有测试过这个:

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 路径上找到可执行的 scon。如果这是你的问题,我可以进一步解释。

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

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

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

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

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

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

Sometimes the thread execution process time is higher than JVM thread waiting process time, it use to happen when the process you're invoking takes some time to be processed, use the waitFor() command as follows:

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 将停止,直到您调用的进程完成,然后继续执行线程执行堆栈。

使用 java 运行批处理文件,如果你说的是这个的话..。

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

这个应该够了。

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

tested with jdk1.5 and jdk1.6

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

The following is working fine:

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

我也有同样的问题,但是有时候 CMD 无法运行我的文件。 这就是为什么我在我的桌面上创建了一个 temp.bat,接下来这个 tem.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();

这段代码将执行路径 C:/files/file 中的两个 commands.bat。

Runtime.getRuntime().exec("cd C:/folders/folder & call commands.bat");

要在 @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();
}
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) {
}
}


}


}