如何在java中为子进程提供输入

时间:2016-02-14 18:54:09

标签: java

我想制作一个程序来运行另一个java程序。 这是我的代码。

        import java.io.*;
 import java.net.*;
 import java.nio.*;
 import java.nio.channels.AsynchronousCloseException;

 class StreamCopier implements Runnable {
private InputStream in;
private BufferedOutputStream out;

public StreamCopier(InputStream in, BufferedOutputStream out) 
{
    this.in = in;
    this.out = out;
}

public void run() 
{
    try 
    {
        int n;
        byte[] buffer = new byte[4096];
        while ((n = in.read(buffer)) != -1) 
        {
            out.write(buffer, 0, n);
            out.flush();
 String s = new String(buffer);
 System.out.print(s);
        }
    }
    catch (IOException e) 
    {
        System.out.println(e);
    }
}
 }

 class InputCopier implements Runnable 
 {

private ObjectInputStream in;
private Process proc;

public InputCopier(ObjectInputStream in, Process proc) 
{
    this.in = in;
    this.proc=proc;
}

public void run() 
{
    try 
    {
    OutputStream out = proc.getOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(out);
        while (true) 
        {
    String s = (String)in.readObject();
    System.out.println(s);

    bos.write(s.getBytes());
    bos.flush();
    bos.close();
        }
    //    out.close();
    } 
    catch (AsynchronousCloseException e) 
    {
    }
    catch (Exception e) 
    {
        System.out.println(e+"hi");
    }
}
 }

 public class Test 
 {
private static Socket socket;

public static void main(String[] args)throws Exception
{
 String command;
    try
    {
        ServerSocket serverSocket=new ServerSocket(5000);
        socket=serverSocket.accept();

 System.out.println("Client Connected.");


 ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
 command = (String)ois.readObject();




 /*     byte buf[] = new byte[500];
    int n=in.read(buf);
    command = new String(buf,5,n);*/
 System.out.println(command);       

 System.out.println("Client Connected.");


        Process process = Runtime.getRuntime().exec(command);

        BufferedOutputStream out=new BufferedOutputStream(socket.getOutputStream());
 //     BufferedInputStream in=new BufferedInputStream(socket.getInputStream());

        Thread outThread = new Thread(new StreamCopier(process.getInputStream(), out));
        outThread.start();

        Thread errThread = new Thread(new StreamCopier(process.getErrorStream(), out));
        errThread.start();

        Thread inThread = new Thread(new InputCopier(ois, process));
        inThread.start();

        process.waitFor();
        System.in.close();
        outThread.join();
        errThread.join();
//        inThread.join();
    }
     catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        try
        {
            socket.close();
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }
}
 }

在while循环中,我接受来自socket的子进程的输入。通过使用bos.write(s.getBytes()),我为子进程提供输入。但是在向子进程终端提供输入后被挂起。

当我在bos.flush()之后使用bos.close()时,子进程获取输入但进程终止。

0 个答案:

没有答案