2023年11月29日发(作者:)

time().exec...)使⽤⽅法

time().exec(...)使⽤⽅法

如果想要了解更多的信息,参阅代码⾥⾯给的链接

下⾯是这个正确的例⼦

public class RuntimeExec {

/**

* Runtime execute.

*

* @param cmd the command.

* @return success or failure

* @see {@link /javaworld/jw-12-2000/?page=4}

* @since 1.1

*/

public static boolean runtimeExec(String cmd) {

try {

Process proc = time().exec(new String[]{"/bin/sh", "-c", cmd});

// any error message?

StreamGobbler errorGobbler = new StreamGobbler(orStream(), "ERROR");

// any output?

StreamGobbler outputGobbler = new StreamGobbler(utStream(), "OUTPUT");

// kick them off

();

();

if (r() != 0) {

n("执⾏"" + cmd + ""时返回值=" + lue());

return false;

} else {

return true;

}

} catch (Exception e) {

tackTrace();

return false;

}

}

static class StreamGobbler extends Thread {

InputStream is;

String type;

StreamGobbler(InputStream is, String type) {

this.is = is;

this.type = type;

}

public void run() {

try {

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

String line = null;

while ((line = ne()) != null)

n(type + ">" + line);

} catch (IOException ioe) {

tackTrace();

}

}