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) {} 

    } 
} 
相關問題