2010-06-07 90 views
-2

可能重複:
need help about process…如何避免進程的shell執行?

當我開始喜歡process = Runtime.getRuntime().exec("gnome-terminal");一個過程,它開始執行腳本。我想停止shell執行並想從進程中重定向I/O,有人可以告訴我該如何做到這一點?

我的代碼是:

public void start_process() 
    { 
     try 
     { 
      process= Runtime.getRuntime().exec("gnome-terminal"); 
      pw= new PrintWriter(process.getOutputStream(),true); 
      br=new BufferedReader(new InputStreamReader(process.getInputStream())); 
      err=new BufferedReader(new InputStreamReader(process.getErrorStream())); 

     } 
     catch (Exception ioe) 
     { 
      System.out.println("IO Exception-> " + ioe); 
     } 


    } 

public void execution_command() 
    { 

     if(check==2) 
     { 
      try 
      { 
       boolean flag=thread.isAlive(); 
       if(flag==true) 
        thread.stop(); 

       Thread.sleep(30); 
       thread = new MyReader(br,tbOutput,err,check); 
       thread.start(); 

      }catch(Exception ex){ 
       JOptionPane.showMessageDialog(null, ex.getMessage()+"1"); 
      } 
     } 
     else 
     { 
      try 
      { 
       Thread.sleep(30); 
       thread = new MyReader(br,tbOutput,err,check); 
       thread.start(); 
       check=2; 

      }catch(Exception ex){ 
       JOptionPane.showMessageDialog(null, ex.getMessage()+"1"); 
      } 

     } 
    } 

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 
     command=tfCmd.getText().toString().trim(); 

     pw.println(command); 

     execution_command(); 

    }  

當我在文本框,然後按輸入一些命令來執行按鈕,沒有任何顯示在我的輸出textarea的,我怎麼能阻止shell執行和輸入輸出重定向?

+0

重複http://stackoverflow.com/questions/2992801/need-help-about-process - 請不要重新發布相同(或非常相似)的問題。相反,編輯原件。 – 2010-06-07 21:00:30

+0

您已經提問了此問題 – 2010-06-07 21:04:53

回答

0

錯誤我寫了process = Runtime.getRuntime()。exec(「bash」);而不是process = Runtime.getRuntime()。exec(「gnome-terminal」);

+0

請不要將此內容發佈爲答案,您應該添加這個帖子作爲評論。如果您在原始問題中犯了錯誤,您可以**編輯**。 – 2010-06-08 08:23:25