java进程间通信 请问大神 如何通过管道实现java进程间通信 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 最简单,文件用socket也可以 http://blog.sina.com.cn/s/blog_5a08b1780100cdvx.html这例子挺不错的。 服务端import java.io.*;import java.net.*;public class ChatServer{ public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8888); while (true) { Socket s = ss.accept(); DataInputStream dos = new DataInputStream(s.getInputStream()); String str1 = dos.readUTF(); System.out.println("qqq"); System.out.println(str1); dos.close(); } } catch (IOException e) { e.getStackTrace(); } catch (Exception e2) { e2.getStackTrace(); } }}客户端import java.awt.*;import java.awt.event.*;import java.net.*;import java.io.*;public class CharClient extends Frame{ Socket s = null; TextField tfTxt = new TextField(); TextArea taContent = new TextArea(); public static void main(String[] args) { new CharClient().LanchFrame(); new CharClient().connect(); } public void LanchFrame() { setSize(200, 100); setLocation(100, 100); add(tfTxt, BorderLayout.SOUTH); add(taContent, BorderLayout.NORTH); pack(); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent arg0) { System.exit(0); } }); tfTxt.addActionListener(new tfListener()); setVisible(true); } public void connect() { try { // s = new Socket("localhost", 8888); } catch (Exception e) { e.getStackTrace(); } } public class tfListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String str = tfTxt.getText().trim(); taContent.setText(str); tfTxt.setText(""); try { s = new Socket("localhost", 8888); DataOutputStream dis = new DataOutputStream(s.getOutputStream()); dis.writeUTF(str); dis.flush(); dis.close(); } catch (Exception e1) { e1.getStackTrace(); } } }} 自己写了一个 但是写入到a类的时候必须关闭流 这样通道就关闭了package Swing1;import java.awt.Button;import java.awt.Color;import java.awt.Frame;import java.awt.Panel;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStreamReader;public class Myframe extends Frame { Panel panel = new Panel(); Button button = new Button( "button1" ); Button button2 = new Button( "run" ); String javaName = "a.java"; private static Process p; private static BufferedReader br; private BufferedOutputStream bw; public Myframe( String lable ) { setLayout( null ); setTitle( lable ); panel.setLayout( null ); panel.setLocation( 50, 50 ); panel.add( button2 ); button2.setSize( 100, 50 ); button.setSize( 50, 50 ); button.setLocation( 110, 110 ); panel.add( button ); panel.setSize( 200, 200 ); panel.setBackground( Color.red ); add( panel ); setBackground( Color.black ); setSize( 500, 500 ); setVisible( false ); button2.addActionListener( new ActionListener() { @Override public void actionPerformed( ActionEvent e ) { Runtime runtime = Runtime.getRuntime(); try { p = runtime.exec( "java a" ); br = new BufferedReader( new InputStreamReader( p.getInputStream() ) ); new Thread() { public void run() { while ( this == Thread.currentThread() ) { bw = new BufferedOutputStream(p.getOutputStream()); if ( bw != null ) { String t = "aaaa"; try { bw.write( t.getBytes() ); bw.close(); bw=null; System.out.println("frame wrote"); } catch ( IOException e ) { e.printStackTrace(); } } try { Thread.sleep( 500 ); } catch ( InterruptedException e ) { e.printStackTrace(); } } } }.start(); new Thread() { public void run() { while ( this == Thread.currentThread() ) { if ( br != null ) { String t = ""; try { if ((t=br.readLine())!=null) { System.out.println("frame:"+t); } } catch ( IOException e ) { // TODO Auto-generated catch block e.printStackTrace(); } } try { Thread.sleep( 500 ); } catch ( InterruptedException e ) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); } catch ( IOException e1 ) { System.out.println( "eeeeeeeeeee" ); e1.printStackTrace(); } } } ); button.addActionListener( new ActionListener() { @Override public void actionPerformed( ActionEvent e ) { File file = new File( javaName ); if ( file != null ) { try { file.createNewFile(); } catch ( IOException e1 ) { e1.printStackTrace(); } } Runtime runtime = Runtime.getRuntime(); try { runtime.exec( "javac " + javaName ); } catch ( IOException e1 ) { e1.printStackTrace(); } } } ); } public static void main( String[] args ) { Myframe frame = new Myframe( "frame" ); frame.setVisible( true ); }}import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class a { private static BufferedReader br; public static void main( String[] args ) { new Thread( ) { public void run( ) { br = new BufferedReader( new InputStreamReader(System.in)); while (Thread.currentThread() == this) { String s=""; try { if (br != null && (s=br.readLine())!= null) { System.out.println(s+"bbb"); } } catch ( IOException e1 ) { // TODO Auto-generated catch block e1.printStackTrace(); } try { Thread.sleep( 500 ); } catch ( InterruptedException e ) { // TODO Auto-generated catch block e.printStackTrace(); } } } }.start(); }}a.java放到工程跟目录下面 先编译再运行 java.io.PipedInputStream 是说这个吗 是不同进程 PipedInputStream 貌似只能在同一进程 不同线程 Java Applet小程序在Google和ff的运行(本机ff 20.0,google 20) 关于JAVA调用QQ的cameraDLL.dll问题? 三维数组应该怎样理解???...立体的??? 关于文件IO的相对路径和绝对路径 在线跪求————怎样处理字符串中的空格!! 即使是SQL NULL,ResultSet的getInt还是返回0?我该怎样区别对待? 关于线程interrupt的问题 请问有谁做过java调用vb的动态库,有不解 Java中使用DOM解析XML,怎么去掉格式化字符(如空格、回车、\t) 求一个C=A+B的程序 关于GC回收对象问题 今天测验的几道题
用socket也可以
这例子挺不错的。
import java.io.*;
import java.net.*;public class ChatServer
{
public static void main(String[] args)
{
try
{
ServerSocket ss = new ServerSocket(8888);
while (true)
{
Socket s = ss.accept();
DataInputStream dos = new DataInputStream(s.getInputStream());
String str1 = dos.readUTF();
System.out.println("qqq");
System.out.println(str1);
dos.close();
}
}
catch (IOException e)
{
e.getStackTrace();
}
catch (Exception e2)
{
e2.getStackTrace();
}
}
}客户端
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;public class CharClient extends Frame
{
Socket s = null;
TextField tfTxt = new TextField();
TextArea taContent = new TextArea(); public static void main(String[] args)
{
new CharClient().LanchFrame();
new CharClient().connect();
} public void LanchFrame()
{
setSize(200, 100);
setLocation(100, 100);
add(tfTxt, BorderLayout.SOUTH);
add(taContent, BorderLayout.NORTH);
pack();
this.addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent arg0)
{
System.exit(0);
} });
tfTxt.addActionListener(new tfListener());
setVisible(true);
} public void connect()
{
try
{
// s = new Socket("localhost", 8888);
}
catch (Exception e)
{
e.getStackTrace();
}
} public class tfListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
// TODO Auto-generated method stub
String str = tfTxt.getText().trim();
taContent.setText(str);
tfTxt.setText("");
try
{
s = new Socket("localhost", 8888);
DataOutputStream dis = new DataOutputStream(s.getOutputStream());
dis.writeUTF(str);
dis.flush();
dis.close();
}
catch (Exception e1)
{
e1.getStackTrace();
}
}
}
}
package Swing1;import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;public class Myframe extends Frame { Panel panel = new Panel(); Button button = new Button( "button1" );
Button button2 = new Button( "run" );
String javaName = "a.java"; private static Process p;
private static BufferedReader br;
private BufferedOutputStream bw; public Myframe( String lable ) {
setLayout( null );
setTitle( lable );
panel.setLayout( null );
panel.setLocation( 50, 50 );
panel.add( button2 );
button2.setSize( 100, 50 );
button.setSize( 50, 50 );
button.setLocation( 110, 110 );
panel.add( button );
panel.setSize( 200, 200 );
panel.setBackground( Color.red );
add( panel );
setBackground( Color.black );
setSize( 500, 500 );
setVisible( false );
button2.addActionListener( new ActionListener() { @Override
public void actionPerformed( ActionEvent e ) {
Runtime runtime = Runtime.getRuntime();
try {
p = runtime.exec( "java a" );
br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
new Thread() {
public void run() {
while ( this == Thread.currentThread() ) {
bw = new BufferedOutputStream(p.getOutputStream());
if ( bw != null ) {
String t = "aaaa";
try {
bw.write( t.getBytes() );
bw.close();
bw=null;
System.out.println("frame wrote");
}
catch ( IOException e ) {
e.printStackTrace();
}
}
try {
Thread.sleep( 500 );
}
catch ( InterruptedException e ) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
while ( this == Thread.currentThread() ) {
if ( br != null ) {
String t = "";
try {
if ((t=br.readLine())!=null) {
System.out.println("frame:"+t);
}
}
catch ( IOException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep( 500 );
}
catch ( InterruptedException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
catch ( IOException e1 ) {
System.out.println( "eeeeeeeeeee" );
e1.printStackTrace();
} }
} );
button.addActionListener( new ActionListener() { @Override
public void actionPerformed( ActionEvent e ) {
File file = new File( javaName );
if ( file != null ) {
try {
file.createNewFile();
}
catch ( IOException e1 ) { e1.printStackTrace();
}
} Runtime runtime = Runtime.getRuntime();
try {
runtime.exec( "javac " + javaName );
}
catch ( IOException e1 ) { e1.printStackTrace();
} }
} );
} public static void main( String[] args ) {
Myframe frame = new Myframe( "frame" );
frame.setVisible( true );
}
}import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;public class a { private static BufferedReader br;
public static void main( String[] args ) {
new Thread( ) {
public void run( ) {
br = new BufferedReader( new InputStreamReader(System.in));
while (Thread.currentThread() == this) {
String s="";
try {
if (br != null && (s=br.readLine())!= null) {
System.out.println(s+"bbb");
}
}
catch ( IOException e1 ) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Thread.sleep( 500 );
}
catch ( InterruptedException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
}
a.java放到工程跟目录下面 先编译再运行
是说这个吗