In my Java application, I want to run a batch file that calls "scons -Q implicit-deps-changed build\file_load_type export\file_load_type
"
在我的Java应用程序中,我想运行一个批处理文件,它调用“scons -Q implicit-deps-changed构建\file_load_type export\file_load_type export\file_load_type”
It seems that I can't even get my batch file to execute. I'm out of ideas.
似乎我甚至不能执行我的批处理文件。我的想法。
This is what I have in Java:
这是我在Java里面的东西:
Runtime.
getRuntime().
exec("build.bat", null, new File("."));
Previously, I had a Python Sconscript file that I wanted to run but since that didn't work I decided I would call the script via a batch file but that method has not been successful as of yet.
以前,我有一个我想要运行的Python Sconscript文件,但是由于它不起作用,我决定通过批处理文件调用脚本,但是这个方法还没有成功。
11 个解决方案
#1
157
Batch files are not an executable. They need an application to run them (i.e. cmd).
批处理文件不是可执行文件。它们需要一个应用程序来运行它们(例如cmd)。
On UNIX, the script file has shebang (#!) at the start of a file to specify the program that executes it. Double-clicking in Windows is performed by Windows Explorer. CreateProcess
does not know anything about that.
在UNIX上,脚本文件在文件的开始处有shebang(#!)来指定执行它的程序。在Windows中双击由Windows资源管理器执行。CreateProcess对此一无所知。
Runtime.
getRuntime().
exec("cmd /c start \"\" build.bat");
Note: With the start \"\"
command, a separate command window will be opened with a blank title and any output from the batch file will be displayed there. It should also work with just `cmd /c build.bat", in which case the output can be read from the sub-process in Java if desired.
注意:使用start \“\”命令,将打开一个带有空白标题的单独命令窗口,其中将显示来自批处理文件的任何输出。它也应该只与“cmd /c构建”一起工作。在这种情况下,如果需要,可以从Java中的子进程中读取输出。
#2
20
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:
有时线程执行进程时间高于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
}
This way the JVM will stop until the process you're invoking is done before it continue with the thread execution stack.
这样,JVM将停止,直到您调用的进程在线程执行堆栈上继续之前完成。
#3
18
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() );
}
#4
13
To run batch files using java if that's you're talking about...
要使用java运行批处理文件,如果您正在讨论……
String path="cmd /c start d:\\sample\\sample.bat";
Runtime rn=Runtime.getRuntime();
Process pr=rn.exec(path);`
This should do it.
这应该这样做。
#5
#6
9
The executable used to run batch scripts is cmd.exe
which uses the /c
flag to specify the name of the batch file to run:
用于运行批脚本的可执行文件是cmd。exe,使用/c标志指定要运行的批处理文件的名称:
Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "build.bat"});
Theoretically you should also be able to run Scons in this manner, though I haven't tested this:
从理论上讲,你也应该能够以这种方式运行Scons,尽管我还没有对此进行测试:
Runtime.getRuntime().exec(new String[]{"scons", "-Q", "implicit-deps-changed", "build\file_load_type", "export\file_load_type"});
EDIT: Amara, you say that this isn't working. The error you listed is the error you'd get when running Java from a Cygwin terminal on a Windows box; is this what you're doing? The problem with that is that Windows and Cygwin have different paths, so the Windows version of Java won't find the scons executable on your Cygwin path. I can explain further if this turns out to be your problem.
编辑:阿玛拉,你说这行不通。您列出的错误是您在Windows box上从Cygwin终端运行Java时遇到的错误;这就是你要做的吗?问题是Windows和Cygwin有不同的路径,所以Windows版本的Java在Cygwin路径上找不到scons可执行文件。如果这是你的问题,我可以进一步解释。
#7
3
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
使用jdk1.5和jdk1.6进行测试
This was working fine for me, hope it helps others too. to get this i have struggled more days. :(
这对我来说很好,希望它也能帮助别人。为了得到这个,我挣扎了更多的日子。:(
#8
2
I had the same issue. However sometimes CMD failed to run my files. That's why i create a temp.bat on my desktop, next this temp.bat is going to run my file, and next the temp file is going to be deleted.
我也有同样的问题。然而,有时CMD无法运行我的文件。这就是为什么我在我的桌面上创建了一个temp.bat,下一个这个temp.bat将运行我的文件,下一个temp文件将被删除。
I know this is a bigger code, however worked for me in 100% when even Runtime.getRuntime().exec() failed.
我知道这是一个更大的代码,但是即使运行时. 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();
#9
0
The following is working fine:
以下操作正常:
String path="cmd /c start d:\\sample\\sample.bat";
Runtime rn=Runtime.getRuntime();
Process pr=rn.exec(path);
#10
0
This code will execute two commands.bat that exist in the path C:/folders/folder.
这段代码将执行两个命令。在路径C:/文件夹/文件夹中存在的bat。
Runtime.getRuntime().exec("cd C:/folders/folder & call commands.bat");
#11
0
To expand on @Isha's anwser you could just do the following to get the returned output (post-facto not in rea-ltime) of the script that was run:
要扩展@Isha的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();
}
#1
157
Batch files are not an executable. They need an application to run them (i.e. cmd).
批处理文件不是可执行文件。它们需要一个应用程序来运行它们(例如cmd)。
On UNIX, the script file has shebang (#!) at the start of a file to specify the program that executes it. Double-clicking in Windows is performed by Windows Explorer. CreateProcess
does not know anything about that.
在UNIX上,脚本文件在文件的开始处有shebang(#!)来指定执行它的程序。在Windows中双击由Windows资源管理器执行。CreateProcess对此一无所知。
Runtime.
getRuntime().
exec("cmd /c start \"\" build.bat");
Note: With the start \"\"
command, a separate command window will be opened with a blank title and any output from the batch file will be displayed there. It should also work with just `cmd /c build.bat", in which case the output can be read from the sub-process in Java if desired.
注意:使用start \“\”命令,将打开一个带有空白标题的单独命令窗口,其中将显示来自批处理文件的任何输出。它也应该只与“cmd /c构建”一起工作。在这种情况下,如果需要,可以从Java中的子进程中读取输出。
#2
20
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:
有时线程执行进程时间高于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
}
This way the JVM will stop until the process you're invoking is done before it continue with the thread execution stack.
这样,JVM将停止,直到您调用的进程在线程执行堆栈上继续之前完成。
#3
18
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() );
}
#4
13
To run batch files using java if that's you're talking about...
要使用java运行批处理文件,如果您正在讨论……
String path="cmd /c start d:\\sample\\sample.bat";
Runtime rn=Runtime.getRuntime();
Process pr=rn.exec(path);`
This should do it.
这应该这样做。
#5
#6
9
The executable used to run batch scripts is cmd.exe
which uses the /c
flag to specify the name of the batch file to run:
用于运行批脚本的可执行文件是cmd。exe,使用/c标志指定要运行的批处理文件的名称:
Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", "build.bat"});
Theoretically you should also be able to run Scons in this manner, though I haven't tested this:
从理论上讲,你也应该能够以这种方式运行Scons,尽管我还没有对此进行测试:
Runtime.getRuntime().exec(new String[]{"scons", "-Q", "implicit-deps-changed", "build\file_load_type", "export\file_load_type"});
EDIT: Amara, you say that this isn't working. The error you listed is the error you'd get when running Java from a Cygwin terminal on a Windows box; is this what you're doing? The problem with that is that Windows and Cygwin have different paths, so the Windows version of Java won't find the scons executable on your Cygwin path. I can explain further if this turns out to be your problem.
编辑:阿玛拉,你说这行不通。您列出的错误是您在Windows box上从Cygwin终端运行Java时遇到的错误;这就是你要做的吗?问题是Windows和Cygwin有不同的路径,所以Windows版本的Java在Cygwin路径上找不到scons可执行文件。如果这是你的问题,我可以进一步解释。
#7
3
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
使用jdk1.5和jdk1.6进行测试
This was working fine for me, hope it helps others too. to get this i have struggled more days. :(
这对我来说很好,希望它也能帮助别人。为了得到这个,我挣扎了更多的日子。:(
#8
2
I had the same issue. However sometimes CMD failed to run my files. That's why i create a temp.bat on my desktop, next this temp.bat is going to run my file, and next the temp file is going to be deleted.
我也有同样的问题。然而,有时CMD无法运行我的文件。这就是为什么我在我的桌面上创建了一个temp.bat,下一个这个temp.bat将运行我的文件,下一个temp文件将被删除。
I know this is a bigger code, however worked for me in 100% when even Runtime.getRuntime().exec() failed.
我知道这是一个更大的代码,但是即使运行时. 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();
#9
0
The following is working fine:
以下操作正常:
String path="cmd /c start d:\\sample\\sample.bat";
Runtime rn=Runtime.getRuntime();
Process pr=rn.exec(path);
#10
0
This code will execute two commands.bat that exist in the path C:/folders/folder.
这段代码将执行两个命令。在路径C:/文件夹/文件夹中存在的bat。
Runtime.getRuntime().exec("cd C:/folders/folder & call commands.bat");
#11
0
To expand on @Isha's anwser you could just do the following to get the returned output (post-facto not in rea-ltime) of the script that was run:
要扩展@Isha的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();
}