Membuat Program Menjalankan Command Linux dengan Java Swing / Desktop

Sudah cukup lama saya belum memposting artikel tentang Java Swing. Kali ini saya membuat program sederhana menjalankan command Linux di Java Swing. Cara kerja aplikasi ini adalah anda input command Linux di textfield 1 kemudian klik tombol Proses, maka hasilnya akan ditampilkan di textfield 2. Untuk membersihkan textfield klik tombol Refresh.

Berikut ini source code Javanya

package JavaConsole;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.swing.JTextField;
import java.awt.Color;

@SuppressWarnings("serial")
public class frm extends JFrame {

	private JPanel contentPane;
	String s;
	Process p;
	private JTextField textField;
	private JButton btnNewButton;
	private JTextField txt;
	private JButton btnNewButton_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frm frame = new frm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public frm() {
		setTitle("Java Console");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 164);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(0, 51, 204));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		textField = new JTextField();
		textField.setBounds(12, 54, 424, 30);
		contentPane.add(textField);
		textField.setColumns(10);

		txt = new JTextField();
		txt.setBounds(12, 12, 424, 30);
		contentPane.add(txt);
		txt.setColumns(10);

		btnNewButton = new JButton("Proses");
		btnNewButton.setBackground(Color.ORANGE);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {

					p = Runtime.getRuntime().exec(txt.getText());
					BufferedReader br = new BufferedReader(
							new InputStreamReader(p.getInputStream()));
					while ((s = br.readLine()) != null)
						textField.setText("" + s);
					p.waitFor();
					System.out.println("exit: " + p.exitValue());
					p.destroy();

				} catch (Exception e) {
				}
			}

		});
		btnNewButton.setBounds(103, 102, 106, 24);
		contentPane.add(btnNewButton);

		btnNewButton_1 = new JButton("Refresh");
		btnNewButton_1.setBackground(Color.ORANGE);
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textField.setText("");
				txt.setText("");
				txt.requestFocus();
			}
		});
		btnNewButton_1.setBounds(246, 102, 106, 24);
		contentPane.add(btnNewButton_1);

	}
}

Seperti ini tampilan programnya.

Input perintah uname -a

Screenshot

Hasil ditampilkan di textfield 2

Screenshot-1

Menampilkan waktu hari ini

Screenshot-2

Selamat mencoba dan semoga bermanfaat🙂

Untuk pertanyaan, saran dan kritik silahkan ditulis di kolom komentar.

2 thoughts on “Membuat Program Menjalankan Command Linux dengan Java Swing / Desktop

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s