One caveat is that it is difficult to cancel the command thread early.
I have been meaning to work it out but I believe it is a known bug.

Ray 

-----Original Message-----
From: Raymond Hooker (rhooker) 
Sent: Friday, September 09, 2005 12:08 PM
To: 'Research Triangle Java User's Group mailing list.'
Subject: RE: [Juglist] launching application in Windows without
DOScommandwindow

Actually it is quite a bit more complex than that.  I assume that you
want to run a command and receive the output.  I do that for running
Python scripts and capturing the output.  Also, I spawn another thread
so that my original menu is not locked up.  The following code may give
you an idea.  Note that it monitors the output (STD and ERR) and send a
progress message to a progress pane in Swing:



import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 * @author rhooker
 *
 */
public class pythonSession extends Thread {
        String args[];
        boolean Cancel = false;  //Signal to cancel
        String codepath;
        String ErrorResult;
        PythonProgress progressPane;
        public int retcode;
        
        pythonSession(String codepath, String args[])
        {
                this.codepath=codepath;
                this.args=args;
        }
        /**
         * @return Returns the cancel.
         */
        public synchronized boolean isCancel() {
                return Cancel;
        }
    String MessageStr() {
        return ErrorResult;
    }
    public void run()
    {
        //String codepath = "\"c:\\Documents and Settings\\rhooker\\My
Documents\\eclipse\\workspace\\ConfigParsers\\";
                //String codepath = "\"c:/mongoose/";
        //String codepath = "";
        String cmd = "python \""+codepath+args[0]+"\" "+args[1];
        System.out.println("<"+cmd+">");
        String osName = System.getProperty("os.name" );
        String[] cmds = new String[3];

        if ( osName.equals( "Windows 95" ))
        {
            cmds[0] = "command.com" ;
            cmds[1] = "/C" ;
            cmds[2] = args[0];
        }
        else if (( osName.equals( "Windows NT" )) || ( osName.equals(
"Windows XP" ) ) ||
                        ( osName.equals( "Windows 2000" ) ) || (
osName.equals( "Windows XP" ) ))
        {
            cmds[0] = "cmd.exe" ;
            cmds[1] = "/C" ;
            cmds[2] = cmd;
        }
        else if ( osName.equals( "Linux" )) 
        {
            cmds[0] = "/bin/bash" ;
            cmds[1] = "-c" ;
            cmds[2] = cmd;
        }
        else if ( osName.equals( "Mac OS X" )) 
        {
            cmds[0] = "/bin/bash" ;
            cmds[1] = "-c" ;
            cmds[2] = cmd;
        }
        try
        {     


                
            Runtime rt = Runtime.getRuntime();
            Process proc = rt.exec(cmds);
            StreamGobblerString outputGobbler = null;
            // any error message?
            FileOutputStream fos = null;
            if (args[2]!=null) {
                fos = new FileOutputStream(args[2]);
                outputGobbler = new
StreamGobblerString(proc.getInputStream(), "OUTPUT", progressPane, fos);
            }
            else {
                outputGobbler = new
StreamGobblerString(proc.getInputStream(), "OUTPUT", progressPane,
null);
            }
            StreamGobblerString errorGobbler = new
StreamGobblerString(proc.getErrorStream(), "ERROR", progressPane);

            // any output?
            
                
            // kick them off
            errorGobbler.start();
            outputGobbler.start();
                       
            // any error???
            int exitVal = proc.waitFor();
            //System.out.println("ExitValue: " + exitVal);
            if (exitVal>0) {
                ErrorResult = errorGobbler.MessageStr();
            }
            if (args[2]!=null) {
                fos.flush();
                fos.close();
            }
              
            retcode=exitVal;
            progressPane.CloseMsg(retcode);
            return;
      
        } catch (Throwable t)
          {
            t.printStackTrace();
            progressPane.CloseMsg(1000);
          }
                return;
    }
        /**
         * @param cancel The cancel to set.
         */
        public synchronized void setCancel(boolean cancel) {
                Cancel = cancel;
        }
        // Designed for 3 inputs
        //     1.  Path/ name of Python script
        //     2.  Python script arguments
        //     3.  STDout redirect file
    
        public void setPPane(PythonProgress PPane) {
                this.progressPane=PPane;
        }
}

class StreamGobbler extends Thread
{
    InputStream is;
    OutputStream os;
    String type;
    
    StreamGobbler(InputStream is, String type)
    {
        this(is, type, null);
    }

    StreamGobbler(InputStream is, String type, OutputStream redirect)
    {
        this.is = is;
        this.type = type;
        this.os = redirect;
    }
    
    public void run()
    {
        try
        {
            PrintWriter pw = null;
            if (os != null)
                pw = new PrintWriter(os);
                
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
            {
                if (pw != null)
                    pw.println(line);
                //System.out.println(type + ">" + line);    
            }
            if (pw != null)
                pw.flush();
        } catch (IOException ioe)
            {
            ioe.printStackTrace();  
            }
    }
}
class StreamGobblerString extends Thread {
    InputStream is;
    OutputStream os=null;
    PythonProgress progressPane;
    String Resultstr;
    String type;
    
    StreamGobblerString(InputStream is, String type, PythonProgress
progressPane)
    {
        this(is, type, progressPane, null);
    }

    StreamGobblerString(InputStream is, String type, PythonProgress
progressPane,OutputStream redirect)
    {
        this.is = is;
        this.type = type;
        this.os = redirect;
        this.progressPane = progressPane;
        
    }
    String MessageStr() {
        return Resultstr;
    }
    
    
    public void run()
    {
        try
        {
            PrintWriter pw = null;
            if (os != null)
                pw = new PrintWriter(os);
                
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
            {
                if (pw != null)
                    pw.println(line);
                //System.out.println(type + ">" + line);  
                //Resultstr= Resultstr+"\n+"+line;
                progressPane.OutputMsg(line, type);
            }
            if (pw != null)
                pw.flush();
        } catch (IOException ioe)
            {
            ioe.printStackTrace();  
            }
    }
}

_______________________________________________
Juglist mailing list
[email protected]
http://trijug.org/mailman/listinfo/juglist_trijug.org

Reply via email to