Annunci
RSS

Execute CMD Commands Using a Simple Java Interface

25 Mar

CMDSimulator

 

package cmd;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

 

public class JavaRunCommand extends JFrame {

   private JTextField CMDField = new JTextField(40);
   private JButton ExecuteButton = new JButton(“Execute Command”);
   private JLabel CommandLabel = new JLabel(“Command: “);
   private JTextArea OutPut = new JTextArea(20, 30);
   private JScrollPane jsp = new JScrollPane(OutPut);

   public JavaRunCommand( ) {
   super(“CMD Simulator”);
   Container contentPane = this.getContentPane( );
   contentPane.setLayout(new BorderLayout( ));
   JPanel labels = new JPanel( );
   labels.setLayout(new GridLayout(4, 1));
   labels.add(CommandLabel);

  JPanel fields = new JPanel( );
  fields.setLayout(new GridLayout(4, 1));
  fields.add(CMDField);

  Box north = Box.createHorizontalBox( );
  north.add(labels);
  north.add(fields);
  contentPane.add(north, BorderLayout.NORTH);
  OutPut.setFont(new Font(“Monospaced”, Font.PLAIN, 12));
  contentPane.add(jsp, BorderLayout.CENTER);

  JPanel south = new JPanel( );
  south.setLayout(new FlowLayout(FlowLayout.CENTER));
  south.add(ExecuteButton);
  ExecuteButton.addActionListener(new ActionButton( ));
  contentPane.add(south, BorderLayout.SOUTH);
  this.pack();
}

  class ActionButton implements ActionListener
{

@Override
  public void actionPerformed(ActionEvent arg0) {
  // TODO Auto-generated method stub
  String CMD = CMDField.getText() ;
  try {
  // Run “netsh” Windows command
  Process process = Runtime.getRuntime().exec(CMD);
  String temp = “”;

// Get input streams
  BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
  BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

// Read command standard output
  String s;
  System.out.println(“Standard output: “);
  while ((s = stdInput.readLine()) != null) {
  System.out.println(s);
  temp +=s + “\n”;
}
 OutPut.setText(temp);

// Read command errors
  System.out.println(“Standard error: “);
  while ((s = stdError.readLine()) != null) {
  System.out.println(s);
  OutPut.setText(s);
  temp +=s + “\n”;
}
  OutPut.append(temp);
} catch (Exception e) {
e.printStackTrace(System.err);
}

}

}

public static void main(String[] args) {
JavaRunCommand client = new JavaRunCommand();
// Next line requires Java 1.3 or later. I want to set up the
// exit behavior here rather than in the constructor since
// other programs that use this class may not want to exit
// the application when the SMTPClient window closes.
client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.show( );
}
}

Annunci
 
Lascia un commento

Pubblicato da su marzo 25, 2013 in Programming

 

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

 
%d blogger hanno fatto clic su Mi Piace per questo: