关于Runtime.exec()的实验

实验结果:通过Runtime.getRuntime().exec("java")执行java命令,需通过新启线程接收process的输出,程序才能正常终止,不接收、通过主进程接收,程序都会挂起。
实验程序:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TestExec {
public static void main(String[] args) {
try {
Process process = Runtime.getRuntime().exec("java");
//方式一、新启线程接收、输出
new MyThread(process.getErrorStream()).start();
new MyThread(process.getInputStream()).start();
//方式二、主进程进行接收、输出
// mainMethod(process.getErrorStream());
// mainMethod(process.getInputStream());
int status = process.waitFor();
if (status == 0) {
System.out.println("exit success");
} else {
System.out.println("exit fail");
}
} catch (Exception e) {
System.out.println("exception occurs......");
e.printStackTrace();
}
}
static void mainMethod(InputStream input){
BufferedReader bf;
bf = new BufferedReader(new InputStreamReader(input));
String line;
try {
line = bf.readLine();
while (line != null) {
System.out.println(line);
line = bf.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class MyThread extends Thread {
BufferedReader bf;
public MyThread(InputStream input) {
bf = new BufferedReader(new InputStreamReader(input));
}
public void run() {
String line;
try {
line = bf.readLine();
while (line != null) {
System.out.println(line);
line = bf.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
打印输出:
------------------------------------
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.
-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print product version and exit
-version:<value>
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -jre-no-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see java.lang.instrument
-splash:<imagepath>
show splash screen with specified image
exit success

你可能感兴趣的:(Runtime)