• Breaking News

    Thursday, 1 October 2015

    HOW RUN SYSTEM COMMAND USING JAVA

    This Post about how we run our system command using java programming. For better understanding of this article I create GUI in which I gave one text box to enter system command and button to run command.

    For run system command in java we have to used

    Process systemProcess = Runtime.getRuntime().exec(command);
    

    1. JavaExecutor class in which we create JFrame using swing API.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    package com.kodemaker;
    
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class JavaExecutor extends JFrame {
     
       public JavaExecutor(){
        
        //set size,title,layout of jframe
        setSize(350, 400);
        setTitle("System Command Executor");
        getContentPane().setLayout(new FlowLayout());
            
        //create label
        JLabel commandLabel = new JLabel("System Command",JLabel.CENTER);    
        commandLabel.setSize(10,50);
           
        //create command input text
        JTextField commandInput = new JTextField(20);
        commandInput.setToolTipText("Enter System Command Here");
            
        //create result label
        JLabel resultLabel = new JLabel("Result",JLabel.LEFT);    
        resultLabel.setSize(10,10);
           
        //create result label
        JLabel result = new JLabel("",JLabel.LEFT);
        
        //create coommand button and add listner
        JButton executeCommand = new JButton("Execute");
        executeCommand.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) { 
                //pass command get from user and set result to result label
                result.setText("");
                ExecuteSystemCommand exe = new  ExecuteSystemCommand();
                result.setText(exe.getCommandResult(commandInput.getText()));
               }
        }); 
                
        // add all component to jframe
        add(commandLabel);
        add(commandInput);
        add(executeCommand);
        add(resultLabel);
        add(result);
        
        //show jframe
        setVisible(true);
       } 
    }
    

    2.  In ExecuteSystemCommand class in which I create getCommandResult method which take command as input and return output of command.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    package com.kodemaker;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    public class ExecuteSystemCommand {
    
       public String getCommandResult(String command){
         
           StringBuffer commandOutput = new StringBuffer();
    
           Process systemProcess;
       
           try {
        
              //get system command process
              systemProcess = Runtime.getRuntime().exec(command);
        
              //wait for complete request
              systemProcess.waitFor();
        
              BufferedReader reader = new BufferedReader(new InputStreamReader(systemProcess.getInputStream()));
    
              String readOutputLineByLine = ""; 
                 
              //read input line by line add into output
              while ((readOutputLineByLine = reader.readLine())!= null) {
                 commandOutput.append(readOutputLineByLine + "\n");     
              }
    
          } catch (Exception e) {
               commandOutput.append("Incorrect Command");
          }
    
          return commandOutput.toString();
       }
    }
    

    3. JavaMainExecuter  main class to initiate our application.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    package com.kodemaker;
    
    import javax.swing.JFrame;
    
    public class JavaMainExecuter {
          
      public static void main(String[] args) {
      // open command executer jframe
        JFrame frame = new JavaExecutor();  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
      }
    }
    

    Below is output of app In which you have to enter command, click on execute to run command and wait for system response to see result.



    No comments:

    Post a Comment