Wednesday, March 19, 2014

Execute a command at runtime using Java program

At the runtime  give a command as input, the Java program interprets that command and execute it. Such commands are: notepad, msword, calc, mspaint, word etc.

GUI based executing command program:



GUI version Code: 
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 class RobotFrame extends JFrame implements ActionListener,ItemListener  {

    String commandName;
    List cmdList = new List();
    JButton executeButton = new JButton("Execute");
    JButton exitButton = new JButton("Exit");
    JLabel jLabel1 = new JLabel("Cmd Executer");
    JScrollPane jScrollPane1 = new JScrollPane(cmdList);
    Font f = new Font("Verdana", Font.BOLD, 30);

    public RobotFrame() {
        initComponents();
    }  

    public void initComponents() {
this.setSize(350,450);
this.setTitle("Command Executer");
this.getContentPane().setBackground(Color.cyan);
this.getContentPane().setLayout(null);
this.getContentPane().add(executeButton);
this.getContentPane().add(exitButton);
this.getContentPane().add(jLabel1);
this.getContentPane().add(jScrollPane1);
jLabel1.setFont(f);
jLabel1.setForeground(Color.red);
jLabel1.setBounds(50,10,300,30);
jScrollPane1.setBounds(60,60,200,300);
executeButton.setBounds(60,365,80,35);
exitButton.setBounds(180,365,80,35);
cmdList.add("Notepad");
cmdList.add("calc");
cmdList.add("mspaint");
cmdList.add("word");
executeButton.addActionListener(this);
exitButton.addActionListener(this);
cmdList.addItemListener(this);
   }

   public void itemStateChanged(ItemEvent e){
        commandName = (String)cmdList.getSelectedItem();
   }

   public void actionPerformed(ActionEvent e) {
if(e.getSource()==executeButton) {
try {
Runtime runtime = Runtime.getRuntime();
runtime.exec(commandName.toLowerCase());
}catch(Exception ex){}
}
if(e.getSource()==exitButton) {
System.exit(0);
}
}

   public static void main(String args[]) {
RobotFrame robotFrame = new RobotFrame();
robotFrame.setVisible(true);    
   }

 }


Console version Code:
  import java.util.*;
  class ExecuteCommandRuntime {
 public static void main(String args[])throws Exception {
   Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the Command : ");
  String commandName = scanner.next();
  Runtime runtime = Runtime.getRuntime();
  runtime.exec(commandName);
 }
  }

Description:

This Program takes the input at the runtime and executes the command that given at runtime. Object of the Scanner class is created to take the input, System.in is the argument that is passed from the constructor which means we are taking the input from keyboard.
Runtime is a class that extends Object class. The object of Runtime class is created by calling the getRuntime() method by its class name that returns the runtime object associated with current Java application:

Runtime runtime = Runtime.getRuntime();

getRuntime() method is called directly by its class name because it is static method, so it is called without creating the object of the Runtime class.


No comments:

Post a Comment