首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

请java

2012-01-19 
请java高手指点package进程间的通信importjava.io.*publicclassMainProcessimplementsRunnable{Processp

请java高手指点
package   进程间的通信;
import   java.io.*;

public   class   MainProcess   implements   Runnable{
        Process   p=null;
        /**   Creates   a   new   instance   of   MainProcess   */
        public   MainProcess()
        {
              //new   Thread(this).start();
            try
            {
                p=Runtime.getRuntime().exec( "java   ChildProcess ");
            }
            catch(Exception   e)
            {
                e.printStackTrace();
            }
            new   Thread(this).start();  
        }  
        public   void   run()
        {
           
              InputStream   ism=p.getInputStream();
              BufferedReader   br=new   BufferedReader(new   InputStreamReader(ism));
              try
                {
                        while(true)
                        {
                              String   str=br.readLine();
                              System.out.println(str+ ": ");
                        }
                     
                    //   br.close();
                }
              catch(Exception   e)
                {
                      e.printStackTrace();
                }
        }
        public   static   void   main(String   args[])
        {
              MainProcess   mp=new   MainProcess();
        }
}


//下面是子进程代码  
package   进程间的通信;

import   java.io.*;
public   class   ChildProcess   {
       
        /**   Creates   a   new   instance   of   ProcessTest   */
        public   static   void   main   (String   args[])
        {
                while(true)
                {


                      System.out.print( "communication\n ");
                }
        }
       
}//为什么读取到的内容为null,而不是communication

[解决办法]
...
[解决办法]
。。。。。 看不懂 要继续学习
[解决办法]
这个问题是父进程执行java ChildProcess时找不到ChildProcess.class引起的

[解决办法]
String line=null;
while((line = br.readLine()) != null)
System.out.println(line);

读数据用上面的方法读

另外 p=Runtime.getRuntime().exec( "java ChildProcess "); 路径有错
你用p=Runtime.getRuntime().exec( "nslookup -q=mx google.com "); 能看到有输出值~~~

热点排行
Bad Request.