2012-03-13 62 views
0

我正在开发一个简单的Java IDE,如Netbeans/Eclipse。我的GUI包含两个JTextArea组件,一个用作TextEditor,最终用户可以输入他的程序,另一个用作输出窗口。使用运行时进程和JTextArea实现简单的Java IDE

我正在通过Java Runtime和Process类调用Windows命令提示符来运行用户程序。我还使用方法getInputStream(),getErrorStream(),getOutputStream()来捕获进程的IO流。

如果程序仅包含将某些内容打印到屏幕上的语句,我可以在输出窗口(JTextArea)上显示输出。但是,如果它包含从用户读取输入的语句,则用户必须可以通过输出窗口输入期望的输入值,并且必须将其发送到进程,就像在Netbeans/Eclipse中一样。

我还检查下面的链接 java: work with stdin/stdout of process in same time

使用此代码,我可以只显示等待输入和陈述不能简单的输出语句。此外,一次只能在输出窗口上显示一行。

如果有人能帮助我解决这个问题,那将会很棒。

感谢

Haleema

+0

“一个简单的Java IDE像Netbeans/Eclipse“:) – 2012-03-13 02:11:46

回答

0

我发现很少的修改,以早期的java后的解决办法:在同一时间标准输入/进程的标准输出工作

class RunFile implements Runnable{ 

    public Thread program = null; 
    public Process process = null; 

    private JTextArea console; 
    private String fn; 
    public RunFile(JTextArea cons,String filename){ 
     console = cons; 
     fn=filename; 
     program = new Thread(this); 
     program.start(); 
    } 

    @Override 
    public void run() {  
     try { 
String commandj[] = new String[4]; 
commandj[0] = "cmd"; 
commandj[1]="/C"; 
commandj[2]="java"; 
commandj[3] = fn; 

String envp[] = new String[1]; 
envp[0]="path=C:/Program Files (x86)/Java/jdk1.6.0/bin"; 
File dir = new File("Path to File"); 

Runtime rt = Runtime.getRuntime(); 
process = rt.exec(commandj,envp,dir); 

      ReadStdout read = new ReadStdout(process,console); 
      WriteStdin write = new WriteStdin(process, console); 

      int x=process.waitFor(); 


      console.append("\nExit value: " + process.exitValue() + "\n"); 
     } 
     catch (InterruptedException e) {} 
     catch (IOException e1) {}  
    } 
} 


class WriteStdin implements Runnable{ 

    private Process process = null; 
    private JTextArea console = null; 
    public Thread write = null; 
    private String input = null; 
    private BufferedWriter writer = null; 

    public WriteStdin(Process p, JTextArea t){ 

     process = p; 
     console = t; 
     writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream())); 

     write = new Thread(this); 
     write.start(); 

     console.addKeyListener(new java.awt.event.KeyAdapter() { 

      @Override 
      public void keyTyped(java.awt.event.KeyEvent e){ 

       //save the last lines for console to variable input 
       if(e.getKeyChar() == '\n'){ 

        try {     

         int line = console.getLineCount() -2; 
         int start = console.getLineStartOffset(line); 
         int end = console.getLineEndOffset(line); 

         input = console.getText(start, end - start); 

         write.resume(); 

        } catch (BadLocationException e1) {} 
       } 
      } 
     }); 
     console.addCaretListener(new javax.swing.event.CaretListener() { 

      @Override 
      public void caretUpdate(CaretEvent e) { 
      console.setCaretPosition(console.getDocument().getLength());  
       throw new UnsupportedOperationException("Not supported yet."); 

      } 
     }); 

     console.addFocusListener(new java.awt.event.FocusAdapter() { 
      @Override 
     public void focusGained(java.awt.event.FocusEvent e) 
     { 
           console.setCaretPosition(console.getDocument().getLength());  
     }  

     }); 
    } 


    @Override 
    public void run(){ 
     write.suspend(); 
     while(true){ 
      try { 
       //send variable input in stdin of process 
       writer.write(input); 
       writer.flush(); 

      } catch (IOException e) {} 
      write.suspend(); 
     } 
    } 
} 



class ReadStdout implements Runnable{ 

    public Thread read = null; 
    private BufferedReader reader = null; 
    private Process process = null; 
private JTextArea console = null; 
    public ReadStdout(Process p,JTextArea t){ 

     process = p; 
     reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
console = t; 
     read = new Thread(this); 
     read.start(); 
    } 

    public void run() { 
     String line; 
     try { 
     while((line = reader.readLine())!=null)      
      console.append(line+"\n"); 
      }catch (IOException e) {} 

    } 
} 
相关问题