Advertisement
Guest User

Console

a guest
Aug 29th, 2016
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.32 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.io.BufferedWriter;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.OutputStream;
  12. import java.io.OutputStreamWriter;
  13. import java.util.concurrent.ConcurrentLinkedQueue;
  14.  
  15. import javax.swing.JButton;
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18. import javax.swing.JScrollPane;
  19. import javax.swing.JTextPane;
  20. import javax.swing.text.BadLocationException;
  21. import javax.swing.text.Document;
  22.  
  23. public class ExeConsole extends JFrame{
  24.    
  25.    
  26.     JTextPane inPane, outPane;
  27.     InputStream inStream, inErrStream;
  28.     OutputStream outStream;
  29.     Process pro;
  30.    
  31.     public ExeConsole(){
  32.         super("Console");
  33.         setPreferredSize(new Dimension(500, 600));
  34.         setLocationByPlatform(true);
  35.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36.  
  37.         // GUI
  38.         outPane = new JTextPane();
  39.         outPane.setEditable(false);
  40.         outPane.setBackground(new Color(20, 20, 20));
  41.         outPane.setForeground(Color.white);
  42.         inPane = new JTextPane();
  43.         inPane.setBackground(new Color(40, 40, 40));
  44.         inPane.setForeground(Color.white);
  45.         inPane.setCaretColor(Color.white);
  46.         JButton btnStop = new JButton("Stop");
  47.        
  48.  
  49.         JPanel panel = new JPanel(new BorderLayout());
  50.         panel.add(outPane, BorderLayout.CENTER);
  51.         panel.add(inPane, BorderLayout.SOUTH);
  52.         panel.add(btnStop, BorderLayout.NORTH);
  53.        
  54.         JScrollPane scrollPanel = new JScrollPane(panel);
  55.  
  56.         getContentPane().add(scrollPanel);
  57.  
  58.         // LISTENER
  59.         inPane.addKeyListener(new KeyListener(){
  60.             @Override
  61.             public void keyPressed(KeyEvent e){
  62.               if(e.getKeyCode() == KeyEvent.VK_ENTER){
  63.                     e.consume();
  64.                     read(inPane.getText());
  65.                 }
  66.             }
  67.             @Override
  68.             public void keyTyped(KeyEvent e) {}
  69.  
  70.             @Override
  71.             public void keyReleased(KeyEvent e) {}
  72.         });
  73.        
  74.         btnStop.addActionListener(new ActionListener() {
  75.             @Override
  76.             public void actionPerformed(ActionEvent e) {
  77.                 if(pro != null)
  78.                     pro.destroy();
  79.             }
  80.         });
  81.  
  82.  
  83.         pack();
  84.         setVisible(true);
  85.     }
  86.  
  87.     private void read(String command){
  88.  
  89.         // Write to Process
  90.         if (outStream != null) {
  91.             println(command);
  92.             System.out.println("Outstream again");
  93.             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream));
  94.            
  95.              try {
  96.                 writer.write(command + "\n");  // add newline so your input will get proceed
  97.                 writer.flush();  // flush your input to your process
  98.             } catch (IOException e1) {
  99.                 e1.printStackTrace();
  100.             }
  101.         }
  102.         // Execute Command
  103.         else {
  104.                
  105.             try {
  106.                 println("v--- Starting Process: " + command + "---v" );
  107.                 exec(command);
  108.             } catch (IOException e) {
  109.                 // Handle the exception here. Mostly this means
  110.                 // that the command could not get executed
  111.                 // because command was not found.
  112.                 println("Command not found: " + command);
  113.             }
  114.             println("^--- Process Ended ---^");
  115.         }
  116.         inPane.setText("");
  117.     }
  118.  
  119.     private void exec(String command) throws IOException{
  120.         pro = Runtime.getRuntime().exec(command, null);
  121.  
  122.         inStream = pro.getInputStream();
  123.         inErrStream = pro.getErrorStream();
  124.         outStream = pro.getOutputStream();
  125.  
  126.         InputStreamLineBuffer outBuff = new InputStreamLineBuffer(inStream);
  127.         InputStreamLineBuffer errBuff = new InputStreamLineBuffer(inErrStream);
  128.  
  129.         // Process output stream reader
  130.         Thread streamReader = new Thread(new Runnable() {      
  131.             public void run() {
  132.                 // start the input reader buffer threads
  133.                 outBuff.start();
  134.                 errBuff.start();
  135.  
  136.                 // while an input reader buffer thread is alive
  137.                 // or there are unconsumed data left
  138.                 while(outBuff.isAlive() || outBuff.hasNext() ||
  139.                     errBuff.isAlive() || errBuff.hasNext()){
  140.  
  141.                     // get the normal output if at least 50 millis have passed
  142.                     if(outBuff.timeElapsed() > 50)
  143.                         while(outBuff.hasNext())
  144.                             println(outBuff.getNext());
  145.                     // get the error output if at least 50 millis have passed
  146.                     if(errBuff.timeElapsed() > 50)
  147.                         while(errBuff.hasNext())
  148.                             println(errBuff.getNext());
  149.                     // sleep a bit bofore next run
  150.                     try {
  151.                         Thread.sleep(100);
  152.                     } catch (InterruptedException e) {
  153.                         e.printStackTrace();
  154.                     }                
  155.                 }
  156.                 System.out.println("Finish reading error and output stream");
  157.             }          
  158.         });
  159.         streamReader.start();
  160.  
  161.         // Thread that waits for process to end
  162.         Thread exitWaiter = new Thread(new Runnable() {
  163.             public void run() {
  164.                 try {
  165.                     int retValue = pro.waitFor();
  166.                     println("Command exit with return value " + retValue);
  167.                     // close outStream
  168.                     outStream.close();
  169.                     outStream = null;
  170.                 } catch (InterruptedException e) {
  171.                     e.printStackTrace();
  172.                 } catch (IOException e) {
  173.                     e.printStackTrace();
  174.                 }
  175.             }
  176.         });
  177.         exitWaiter.start();
  178.     }
  179.  
  180.     public void println(String line) {
  181.         Document doc = outPane.getDocument();
  182.         try {
  183.             doc.insertString(doc.getLength(), line + "\n", null);
  184.         } catch (BadLocationException e) {}
  185.     }
  186.  
  187.     public static void main(String[] args){
  188.         new ExeConsole();
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement