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
首先 我在NetBeans中运行该文件,打印出无数多个null,陷入循环之中.
其次 我若在命令行窗口中游行上面的代码,他会抛出如下的异常:
Exception in thread "main" java.lang.NoClassDefFoundError: MainProcess (wrong na
me: ProcessCommunication/MainProcess)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
最后 我始终没有放弃对程序的测试,当我将这两个类的包名同时去掉后并在命令行窗口中运行:结果令我万分惊喜,这是我想要的结果呀,
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
communication:
我想问的是:为什么?这是NetBeans的问题,还是java虚拟机本身就有问题
在
p=Runtime.getRuntime().exec("java packageName.ChildProcess");
中加上packageName就可以了。
因为在Runtime.getRuntime().exec()中执行java时,是以工程路径为相对路径的(你可以把ChildProcess去掉包放在工程目录下试一试 )。
所以你不加包名的话是找不到ChildProcess的。
不要忘记结贴啊,你的三个帖子我都回了,花了我不少时间噢!
呵呵