失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > java 运行批处理文件_如何从Java应用程序运行批处理文件?

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

时间:2023-12-01 23:37:59

相关推荐

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

在我的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中的子进程读取输出。

对我而言,它说Windows无法找到"build.bat"。那么我应该把这个文件放在哪里?或者我应该如何给出路径。有什么建议?

假设我有一个命令数组,然后迭代该数组以执行所有命令(i = 0到commands.length){Runtime.getRuntime()。exec("cmd / c start buil.bat");然后对于每次迭代(对于每个命令),命令窗口都会被打开,这很明显。如何避免我的意思是在一个窗口上执行所有命令。

我们有一些代码直接调用"gradlew.bat"而不在其前放置"cmd / c"内容,并且该代码以某种方式工作。所以我猜Java或Windows在某些方面解决了部分问题。如果我们试图执行"gradlew",那就失败了,所以很明显仍然需要".bat"。

Win+R(运行时)可以直接执行批处理文件。

有时线程执行进程时间高于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() );

}

评论此代码并告诉我们为什么以及InputStream正在阅读的内容以及我关心的原因将会很有用。此外,批处理文件的代码运行良好,但我不能让它引发错误异常。

在我的代码中,有一个令人困惑的变量名称为"is",这让我很难过。

我假设他想打印脚本的输出....

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

String path="cmd /c start d:\\sample\\sample.bat";

Runtime rn=Runtime.getRuntime();

Process pr=rn.exec(path);`

这应该做到这一点。

已经用一个有效的解决方案回答了这个问题。您应该只提供您知道正在运行的解决方案,并描述您认为解决方案可能更好的原因。

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

为什么ProcessBuilder成为Java 5/6的发展方向?

复活旧帖子的有趣选择... ProcessBuilder提供更多控制,特别是能够轻松地将stderr重定向到stdout。我也发现设置更直观,但这是个人的首选

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

谢谢。它仍然不起作用 - 这段代码甚至不在我的应用程序中执行。我会尝试你提出的其他选项。再次感谢。

当我尝试第二种替代方法时它给了我这个错误:线程"main"中的异常java.io.IOException:无法运行程序"scons":CreateProcess error = 2,系统找不到指定的文件

不,我没有Cygwin终端。我使用Windows Command终端。这很奇怪 - 我不知道它为什么不起作用。这完全让我感到困惑。

Process p = Runtime.getRuntime().exec(

new String[]{"cmd","/C","orgreg.bat"},

null,

new File("D://TEST//home//libs//"));

用jdk1.5和jdk1.6测试

这对我来说很好,希望它也能帮助别人。

为了得到这个,我已经挣扎了更多的日子。 :(

add this ==> BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = reader.readLine(); while(line!= null){System.out.println(line); line = reader.readLine(); }

我遇到过同样的问题。但有时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

writer.write("cd %ProgramFiles(x86)%\\SOME_FOLDER

start xyz.bat

exit");

} 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的含义是什么?

要扩展@ 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();

}

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

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

如果觉得《java 运行批处理文件_如何从Java应用程序运行批处理文件?》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。