请问大神 如何通过管道实现java进程间通信

解决方案 »

  1.   

    最简单,文件
    用socket也可以
      

  2.   

    http://blog.sina.com.cn/s/blog_5a08b1780100cdvx.html
    这例子挺不错的。
      

  3.   

    服务端
    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();
          }
        }
      }
    }
      

  4.   

    自己写了一个  但是写入到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放到工程跟目录下面  先编译再运行
      

  5.   

    java.io.PipedInputStream 
    是说这个吗
      

  6.   

    是不同进程  PipedInputStream 貌似只能在同一进程 不同线程