java无法启动程序_无法从Java启动程序(使用ProcessBuilder)

我正在尝试从Java应用程序调用cleartool,但是

即使对于简单的“ -version”参数,cleartool也会挂起。运行cleardiff而不是cleartool

可以正常工作,因此显然cleartool

程序有一些特定的东西(我认为这与其交互功能有关)。

以下程序

import java.io.*;

import java.util.*;

public class ExecTesting extends Thread {

private List command = new ArrayList();

public ExecTesting (List command) {

super();

this.command = command;

}

private void print(String s) {

System.out.println(s);

}

@Override

public void run() {

Process process;

OutputStream stdin;

InputStream stdout;

InputStream stderr;

String line;

try {

String commandString = joinList(command, " ");

print("Executing: " + commandString);

// runtime.exec has several issues (http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1)

// better to use ProcessBuilder (http://java.sun.com/developer/JDCTechTips/2005/tt0727.html#2)

//process = Runtime.getRuntime().exec(commandString);

process = new ProcessBuilder(command).start();

// it fails in both cases though

stdin = process.getOutputStream();

stdout = process.getInputStream();

stderr = process.getErrorStream();

BufferedReader bufferedStderr = new BufferedReader(new InputStreamReader(stderr));

while ((line = bufferedStderr.readLine()) != null) {

print("stderr: " + line);

}

bufferedStderr.close();

BufferedReader bufferedStdout = new BufferedReader(new InputStreamReader(stdout));

while ((line = bufferedStdout.readLine()) != null) {

print("stdout: " + line);

}

bufferedStdout.close();

stdin.close();

stdout.close();

stderr.close();

process.waitFor();

print("Execution finished, exit code " + process.exitValue());

process.destroy();

} catch (IOException e) {

print("IOException: " +e.getStackTrace());

} catch (InterruptedException e) {

print("InterruptedException: " + e.getStackTrace());

}

}

/* assumes a list with at least one element */

private static String joinList(List list, String glue) {

Iterator i = list.iterator();

String ret = i.next();

while (i.hasNext()) {

ret += glue + i.next();

}

return ret;

}

public static void main(String[] args) {

ArrayList cmd1 = new ArrayList();

cmd1.add("c:\\Program Files\\Rational\\ClearCase\\bin\\cleardiff.exe");

cmd1.add("-version");

ExecTesting et1 = new ExecTesting(cmd1);

et1.start();

ArrayList cmd2 = new ArrayList();

//cmd2.add("c:\\Program Files\\Rational\\ClearCase\\bin\\cleardiff.exe");

cmd2.add("c:\\Program Files\\Rational\\ClearCase\\bin\\cleartool.exe");

cmd2.add("-version");

ExecTesting et2 = new ExecTesting(cmd2);

et2.start();

et1 = new ExecTesting(cmd1);

et1.start();

}

}

gives the following output

Executing: [c:\Program Files\Rational\ClearCase\bin\cleardiff.exe, -version]

Executing: [c:\Program Files\Rational\ClearCase\bin\cleartool.exe, -version]

Executing: [c:\Program Files\Rational\ClearCase\bin\cleardiff.exe, -version]

stdout: cleardiff 2003.06.10+ (Tue Jul 13 14:02:05 2004)

Execution finished, exit code 0

hanging on the execution of the cleartool command. If instead cmd2 is changed

to cleardiff the output is as expected

Executing: [c:\Program Files\Rational\ClearCase\bin\cleardiff.exe, -version]

Executing: [c:\Program Files\Rational\ClearCase\bin\cleardiff.exe, -version]

Executing: [c:\Program Files\Rational\ClearCase\bin\cleardiff.exe, -version]

stdout: cleardiff 2003.06.10+ (Tue Jul 13 14:02:05 2004)

Execution finished, exit code 0

stdout: cleardiff 2003.06.10+ (Tue Jul 13 14:02:05 2004)

Execution finished, exit code 0

stdout: cleardiff 2003.06.10+ (Tue Jul 13 14:02:05 2004)

Execution finished, exit code 0

Question: Does anyone know why cleartool is hanging and how to fix?

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值