How to avoid shell execution of a Process?

Posted by adeel amin on Stack Overflow See other posts from Stack Overflow or by adeel amin
Published on 2010-06-07T20:56:31Z Indexed on 2010/06/07 21:02 UTC
Read the original article Hit count: 261

Filed under:

When I start a process like process = Runtime.getRuntime().exec("gnome-terminal");, it starts shell execution. I want to stop shell execution and want to redirect I/O from process, can anybody tell how I can do this?

My code is:

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();

    }    

When I enter some command in textfield and press execute button, nothing is displayed on my output textarea, how I can stop shell execution and redirect input and output?

© Stack Overflow or respective owner

Related posts about java