我在执行下面两个代码发现了一个让我想不明白的问题,
===================================================
代码1:
import java.io.IOException;public class test{
public static void main(String[] args) {
try {
byte b[] = new byte[1024];
int r = 0;
Process proc = Runtime.getRuntime().exec("java");
while ((r = proc.getInputStream().read(b, 0, 1024)) > -1) {
System.out.println(new String(b, 0, r));
}
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}====================================================================================
代码2:
import java.io.IOException;public class test{
public static void main(String[] args) {
try {
byte b[] = new byte[1024];
int r = 0;
Process proc = Runtime.getRuntime().exec("java -version");
while ((r = proc.getInputStream().read(b, 0, 1024)) > -1) {
System.out.println(new String(b, 0, r));
}
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}代码1的输出结果是:
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/e
xclude 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代码2没有任何输出,这是为什么???
请各位大侠赐教!!

解决方案 »

  1.   

    又是这样的问题,死锁了
    参考
    http://topic.csdn.net/u/20070228/10/418a365d-cc3f-4ab7-ae4c-ccda40963a11.html
      

  2.   

    经过调试,将proc.getInputStream()改为proc.getErrorStream()是正确的,可以得到输出结果,结果为:
    java version "1.5.0_13"Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_13-b05)Java HotSpot(TM) Client VM (build 1.5.0_13-b05, mixed mode)
    ====================================================================
    代码:
    import java.io.IOException;public class test{
        public static void main(String[] args) {
            try {
                byte b[] = new byte[1024];
                int r = 0;
                Process proc = Runtime.getRuntime().exec("java -version");
                while ((r = proc.getErrorStream().read(b, 0, 1024)) > -1) {
                    System.out.println(new String(b, 0, r));
                }
                proc.waitFor();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }谢谢各位!!!