在我的 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() );
}
ProcessBuilder 是运行外部进程的 Java 5/6 方式。
使用 java 运行批处理文件(如果您正在谈论的话)...
String path="cmd /c start d:\\sample\\sample.bat";
Runtime rn=Runtime.getRuntime();
Process pr=rn.exec(path);`
这应该可以。
用于运行批处理脚本的可执行文件是
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);
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) {
}
}
}
}
此代码将执行路径C:/folders/folder中存在的两个commands.bat。
Runtime.getRuntime().exec("cd C:/folders/folder & call commands.bat");
当您从 Java(或者我猜是任何其他语言)执行批处理文件时,它从
Windows/System32/
所在的目录运行。要使其从自己的目录运行,请将 cmd.exe
添加到批处理文件的开头。
像这样:cd /D "%~dp0"