john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

INCOMPLETE SystemCommand

// 2011-12-31 johnpfeiffer INCOMPLETE

import java.io.BufferedReader;
import java.io.InputStreamReader;

class SystemCommand
{
    private String command;
    private COMMANDRESULT result;

    private enum COMMANDRESULT
    {
        NOT_YET_RUN,
        NULL_COMMAND,
        EMPTY_COMMAND,
        ERROR,
        SUCCESS
    }

    SystemCommand( String cmd )
    {
        command = cmd;

        if( command == null )
        {       result = COMMANDRESULT.NULL_COMMAND;
        }
        else if( command != null )
        {
            if( command.isEmpty() )
            {       result = COMMANDRESULT.EMPTY_COMMAND;
            }else
            {       result = COMMANDRESULT.NOT_YET_RUN;
            }
        }
    } //end constructor()

    protected void run( )
    {
        int runresult = 0;

        if( result != null && result != COMMANDRESULT.NULL_COMMAND && result != COMMANDRESULT.EMPTY_COMMAND )
        {
            try{
                Runtime myRuntime = Runtime.getRuntime();
                Process myProcess = myRuntime.exec( command );
                InputStreamReader myInputStreamReader = new InputStreamReader( myProcess.getInputStream() );
                BufferedReader myBufferedReader = new BufferedReader( myInputStreamReader );

                String line = null;
                while( ( line = myBufferedReader.readLine() ) != null )
            {       System.out.println( line );
            }

                runresult = myProcess.waitFor();

                if( runresult == 0 )
                {           result = COMMANDRESULT.SUCCESS;
                }
                else
                {           result = COMMANDRESULT.ERROR;
                }

            }catch( Exception e )
            {       e.printStackTrace();
            }
        }
    }

    protected String returnLastCommandResult()
    {       return result.toString();
    }

} //end class

  • « Random String
  • eclipse java roll back jre »

Published

Feb 13, 2012

Category

java-classes

~107 words

Tags

  • classes 92
  • incomplete 22
  • java 252
  • systemcommand 1