Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.util.concurrent.ConcurrentLinkedQueue;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextPane;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.Document;
- public class ExeConsole extends JFrame{
- JTextPane inPane, outPane;
- InputStream inStream, inErrStream;
- OutputStream outStream;
- Process pro;
- public ExeConsole(){
- super("Console");
- setPreferredSize(new Dimension(500, 600));
- setLocationByPlatform(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- // GUI
- outPane = new JTextPane();
- outPane.setEditable(false);
- outPane.setBackground(new Color(20, 20, 20));
- outPane.setForeground(Color.white);
- inPane = new JTextPane();
- inPane.setBackground(new Color(40, 40, 40));
- inPane.setForeground(Color.white);
- inPane.setCaretColor(Color.white);
- JButton btnStop = new JButton("Stop");
- JPanel panel = new JPanel(new BorderLayout());
- panel.add(outPane, BorderLayout.CENTER);
- panel.add(inPane, BorderLayout.SOUTH);
- panel.add(btnStop, BorderLayout.NORTH);
- JScrollPane scrollPanel = new JScrollPane(panel);
- getContentPane().add(scrollPanel);
- // LISTENER
- inPane.addKeyListener(new KeyListener(){
- @Override
- public void keyPressed(KeyEvent e){
- if(e.getKeyCode() == KeyEvent.VK_ENTER){
- e.consume();
- read(inPane.getText());
- }
- }
- @Override
- public void keyTyped(KeyEvent e) {}
- @Override
- public void keyReleased(KeyEvent e) {}
- });
- btnStop.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- if(pro != null)
- pro.destroy();
- }
- });
- pack();
- setVisible(true);
- }
- private void read(String command){
- // Write to Process
- if (outStream != null) {
- println(command);
- System.out.println("Outstream again");
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream));
- try {
- writer.write(command + "\n"); // add newline so your input will get proceed
- writer.flush(); // flush your input to your process
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- // Execute Command
- else {
- try {
- println("v--- Starting Process: " + command + "---v" );
- exec(command);
- } catch (IOException e) {
- // Handle the exception here. Mostly this means
- // that the command could not get executed
- // because command was not found.
- println("Command not found: " + command);
- }
- println("^--- Process Ended ---^");
- }
- inPane.setText("");
- }
- private void exec(String command) throws IOException{
- pro = Runtime.getRuntime().exec(command, null);
- inStream = pro.getInputStream();
- inErrStream = pro.getErrorStream();
- outStream = pro.getOutputStream();
- InputStreamLineBuffer outBuff = new InputStreamLineBuffer(inStream);
- InputStreamLineBuffer errBuff = new InputStreamLineBuffer(inErrStream);
- // Process output stream reader
- Thread streamReader = new Thread(new Runnable() {
- public void run() {
- // start the input reader buffer threads
- outBuff.start();
- errBuff.start();
- // while an input reader buffer thread is alive
- // or there are unconsumed data left
- while(outBuff.isAlive() || outBuff.hasNext() ||
- errBuff.isAlive() || errBuff.hasNext()){
- // get the normal output if at least 50 millis have passed
- if(outBuff.timeElapsed() > 50)
- while(outBuff.hasNext())
- println(outBuff.getNext());
- // get the error output if at least 50 millis have passed
- if(errBuff.timeElapsed() > 50)
- while(errBuff.hasNext())
- println(errBuff.getNext());
- // sleep a bit bofore next run
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println("Finish reading error and output stream");
- }
- });
- streamReader.start();
- // Thread that waits for process to end
- Thread exitWaiter = new Thread(new Runnable() {
- public void run() {
- try {
- int retValue = pro.waitFor();
- println("Command exit with return value " + retValue);
- // close outStream
- outStream.close();
- outStream = null;
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- });
- exitWaiter.start();
- }
- public void println(String line) {
- Document doc = outPane.getDocument();
- try {
- doc.insertString(doc.getLength(), line + "\n", null);
- } catch (BadLocationException e) {}
- }
- public static void main(String[] args){
- new ExeConsole();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement