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
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给结构赋值
- 接口不就是interface吗?怎么还要写一个接口啊
- JDK_1.6.0_22中Swing中没有JTabbedPane
- 急急急!!!各位能人指点一个字符串比较的问题
- jdom,dom搞xml文件,要么不能读,要么不能写。
- windows平台用java怎样把文件压缩成.tar.Z文件
- 怎么处理它:String str="0011010a" (等待帮助!)
- java访问sqlserver2000出现的奇怪问题,请高手帮忙!谢谢
- *** vj 和 borland j++ builder 哪个好?***
- 我感觉悲哀与沮丧:这么简单的程序我都出错?????:紧急求救!!!恳请高手看看!!!等待中
- 找兼职(大连java)
- Tomcat下,jsp表单查询问题!!!!
这样在执行p=Runtime.getRuntime().exec("java ChildProcess")
是找不到ChildProcess.calss的,所以执行结果为null
你把代码拷到才C盘下,在命令行中执行C:\>java 进程间的通信(packageName).MainProcess
就可以了
while((line = br.readLine()) != null)
System.out.println(line);读数据用这个读,不然你还是显示null