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

11个解决方案

163 votes

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

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

Runtime.

getRuntime().

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

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

Paulo Guedes answered 2019-07-21T02:06:41Z

21 votes

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

Juan Carlos Alpízar answered 2019-07-21T02:07:14Z

18 votes

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

}

Isha answered 2019-07-21T02:07:31Z

14 votes

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

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

Runtime rn=Runtime.getRuntime();

Process pr=rn.exec(path);`

这应该做到这一点。

Abbia answered 2019-07-21T02:07:57Z

12 votes

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

basszero answered 2019-07-21T02:08:21Z

10 votes

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

Eli Courtwright answered 2019-07-21T02:09:00Z

3 votes

Process p = Runtime.getRuntime().exec(

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

null,

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

用jdk1.5和jdk1.6测试

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

Suren answered 2019-07-21T02:09:33Z

2 votes

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

Ben Jost answered 2019-07-21T02:10:05Z

1 votes

以下工作正常:

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

Runtime rn=Runtime.getRuntime();

Process pr=rn.exec(path);

bharath answered 2019-07-21T02:10:29Z

0 votes

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

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

رامي عبد الله answered 2019-07-21T02:10:54Z

0 votes

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

}

NoodleOfDeath answered 2019-07-21T02:11:20Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值