2014-01-24 55 views
0

所以我有这样的代码:执行命令的过程

String[] command = new String[] { "java", "-jar", file.getName() }; 
try { 
    System.out.println("Loading input/output"); 
    final Process process = Runtime.getRuntime().exec(command); 
    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
    final OutputStreamWriter out = new OutputStreamWriter(process.getOutputStream()); 
    final BufferedReader consolein = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.println("Loaded input/output"); 
    new Thread() { 
     @Override 
     public void run() { 
      while (true) { 
       System.out.println("hi"); 
       try { 
        String temp = consolein.readLine(); 
        out.write(temp); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }.start(); 
    while (true) { 
     System.out.println(in.readLine()); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

,我试图执行命令的运行罐子。输入很好,我看到控制台中的所有消息。我添加了“hi”消息来测试它是否工作。无论什么时候我输入什么都会显示但是,无论我输入什么内容都不会被发送到正在运行的程序。我在这里做错了什么?

+0

你有没有尝试添加一个新行输入的结束? – MadProgrammer

+0

正如\ n?或者是什么? – PaulBGD

回答

0

我做了一个快速测试。我写了这个小程序,它是由我的其他类中运行...

import java.util.Scanner; 

public class RunMe { 

    public static void main(String[] args) { 
     System.out.println("Hello world"); 
     Scanner scanner = new Scanner(System.in); 
     String text = scanner.nextLine(); 
     System.out.println("You said: " + text); 
     System.out.println("Bye!"); 
    } 

} 

在我写这篇运行它...

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 

public class Runner { 

    private static BufferedReader br; 
    private static BufferedWriter bw; 

    public static void main(String[] args) { 
     ProcessBuilder pb = new ProcessBuilder("java", "-jar", "RunMe.jar"); 
     pb.redirectError(); 

     Scanner scanner = new Scanner(System.in); 
     try { 
      Process p = pb.start(); 
      br = new BufferedReader(new InputStreamReader(p.getInputStream())); 
      bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream())); 

      new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         String text = null; 
         while ((text = br.readLine()) != null) { 
          System.out.println(text); 
         } 
        } catch (IOException ex) { 
         ex.printStackTrace(); 
        } 
       } 
      }).start(); 

      String text = scanner.nextLine(); 
      bw.write(text); 
      bw.newLine(); 
      bw.flush(); 

      p.waitFor(); 

     } catch (IOException | InterruptedException ex) { 
      ex.printStackTrace(); 
     } finally { 
      try { 
       br.close(); 
      } catch (Exception e) { 
      } 
      try { 
       bw.close(); 
      } catch (Exception e) { 
      } 
     } 
    } 

} 

它输出...

Hello world 
This is a test 
You said: This is a test 
Bye! 

(第二行是我输入的内容,第三行是RunMe类的输出)。

所以我建议你需要发送一个新的行。您可以尝试使用\n,但最好使用line.separator属性