Java Console

General discussion on RText.

Moderator: robert

Java Console

Postby theuserbl » Tue Mar 25, 2014 5:55 pm

In Geany existing on Un*x-systems something like a "Console" called "Terminal":
Image

I think something like that would be nice for RText. So that on Windows-systems "C:\Windows\system32\cmd.exe" will be started, on Linux-systems "/bin/bash" and on MacOSX-systems "/bin/tcsh".

And if you do so, please make that terminal modular as RSyntaxTextArea. So that it can be easily embedded in the own program. And that the started program can be choosen. So that instead of the shell, which starts other programs, there will be direct started a Java-program "java -jar program.jar" or so.

And it would be nice, if the console could be colored. For example: from the InputStream all black, from the ErrorStream all red and from the OutputStream all blue.

I have tried to implement a console for Linux. Here the very early beginning:
Code: Select all
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class JavaConsole extends JFrame
{

    JTextPane textPane;
    Process p;
    String s;


    public JavaConsole()
    {
        s = "Hallo";
        setTitle("Java-Console");
        textPane = new JTextPane(); //10, 30);
        textPane.setFont(new Font("Monospaced", Font.PLAIN, 14));
        JScrollPane scrollPane = new JScrollPane( textPane );
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollPane);

        final JViewport viewport = scrollPane.getViewport();
       
       
        try {
          //Process p = Runtime.getRuntime().exec("/home/patrick/jdk1.7.0_07/bin/java -version");
          //p = Runtime.getRuntime().exec("/home/patrick/jdk1.7.0_07/bin/java");
          //p = Runtime.getRuntime().exec("/bin/ls -alF");
          p = Runtime.getRuntime().exec("/bin/bash");

          new InputThread().start();
          new ErrorThread().start();
          new OutputThread().start();
          new OutputThread().start();
 
          //p.destroy();
       
        } catch (Exception e) {
          System.out.println(e);
        }
       
       

    }

    public static void main(String[] args) {
        JavaConsole f = new JavaConsole();
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //f.pack();
        f.setSize(500,400);
        f.setVisible(true);
    }
   
   
   
   
    public class InputThread extends Thread {
      public void run() {
        try {
          InputStream input = p.getInputStream();
       
          int data = input.read();

          while (data != -1) {
            char cdata;
            cdata = (char) data;
            //System.out.print(cdata);
            textPane.setText(textPane.getText()+cdata);
            data = input.read();
          }

          input.close();
        } catch (Exception e) {
          System.out.println("Input error: " + e);
        }
      }
    }
   
 
     public class ErrorThread extends Thread {
      public void run() {
        try {
          InputStream error = p.getErrorStream();
       
          int data = error.read();

          while (data != -1) {
            char cdata;
            cdata = (char) data;
            //System.out.print(cdata);
            textPane.setText(textPane.getText()+cdata);
            data = error.read();
          }
       
          error.close();
        } catch (Exception e) {
          System.out.println("Error error: " + e);
        }
      }
    }

    public class OutputThread extends Thread {
      public void run() {
        try {
          OutputStream output = p.getOutputStream();
       
          //String hjk = "/bin/ls\n";
          String hjk = "ls\n";
          output.write(hjk.getBytes());
          textPane.setText(textPane.getText()+hjk);

//          while (data != -1) {
//            char cdata;
//            cdata = (char) data;
            //System.out.print(cdata);
//            textArea.setText(textArea.getText()+cdata);
//            data = error.read();
//          }
       
          output.close();
        } catch (Exception e) {
          System.out.println("Output error: " + e);
        }
      }
    }

   
}
theuserbl
 
Posts: 12
Joined: Sun Mar 28, 2010 2:37 pm

Re: Java Console

Postby theuserbl » Tue Mar 25, 2014 6:18 pm

Hmmm.. Tested my little program now on Windows with cmd, instead of Linux with ls. And on Windows it works not so good, like on Linux. :(
Code: Select all
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class JavaConsole extends JFrame
{

    JTextPane textPane;
    Process p;
    String s;


    public JavaConsole()
    {
        s = "Hallo";
        setTitle("Java-Console");
        textPane = new JTextPane(); //10, 30);
        textPane.setFont(new Font("Monospaced", Font.PLAIN, 14));
        JScrollPane scrollPane = new JScrollPane( textPane );
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollPane);

        final JViewport viewport = scrollPane.getViewport();
       
       
        try {
          //Process p = Runtime.getRuntime().exec("/home/patrick/jdk1.7.0_07/bin/java -version");
          //p = Runtime.getRuntime().exec("/home/patrick/jdk1.7.0_07/bin/java");
          //p = Runtime.getRuntime().exec("/bin/ls -alF");
          p = Runtime.getRuntime().exec("C:\\Windows\\system32\\cmd.exe");

          new InputThread().start();
          new ErrorThread().start();
          new OutputThread().start();
          new OutputThread().start();
 
          //p.destroy();
       
        } catch (Exception e) {
          System.out.println(e);
        }
       
       

    }

    public static void main(String[] args) {
        JavaConsole f = new JavaConsole();
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //f.pack();
        f.setSize(500,400);
        f.setVisible(true);
    }
   
   
   
   
    public class InputThread extends Thread {
      public void run() {
        try {
          InputStream input = p.getInputStream();
       
          int data = input.read();

          while (data != -1) {
            char cdata;
            cdata = (char) data;
            //System.out.print(cdata);
            textPane.setText(textPane.getText()+cdata);
            data = input.read();
          }

          input.close();
        } catch (Exception e) {
          System.out.println("Input error: " + e);
        }
      }
    }
   
 
     public class ErrorThread extends Thread {
      public void run() {
        try {
          InputStream error = p.getErrorStream();
       
          int data = error.read();

          while (data != -1) {
            char cdata;
            cdata = (char) data;
            //System.out.print(cdata);
            textPane.setText(textPane.getText()+cdata);
            data = error.read();
          }
       
          error.close();
        } catch (Exception e) {
          System.out.println("Error error: " + e);
        }
      }
    }

    public class OutputThread extends Thread {
      public void run() {
        try {
          OutputStream output = p.getOutputStream();
       
          String hjk = "dir\n";
          output.write(hjk.getBytes());
          textPane.setText(textPane.getText()+hjk);

//          while (data != -1) {
//            char cdata;
//            cdata = (char) data;
            //System.out.print(cdata);
//            textArea.setText(textArea.getText()+cdata);
//            data = error.read();
//          }
       
          output.close();
        } catch (Exception e) {
          System.out.println("Output error: " + e);
        }
      }
    }   
   
}


Seems that all needs a lot of improvements.
theuserbl
 
Posts: 12
Joined: Sun Mar 28, 2010 2:37 pm

Re: Java Console

Postby robert » Wed Mar 26, 2014 1:50 am

You're in luck! This already exists in RText; click on the "Console" tab in the bottom docked pane (or from the menus, View -> Docked Windows -> Console if you don't see the tab). When "System" is selected as the shell, it mimics a system console like you desire. Selecting "JavaScript" allows you to execute JavaScript directly in RText's JVM (not as useful in general; I use it for quickly evaluating scripts).

You can open files in RText directly from the console as well, via "open <filename>" or "edit <filename>".

The code isn't easily used outside of RText however.

You can find the code in the Console plugin code in GitHub.
User avatar
robert
 
Posts: 794
Joined: Sat May 10, 2008 5:16 pm

Re: Java Console

Postby theuserbl » Wed Mar 26, 2014 2:28 pm

robert wrote:You're in luck! This already exists in RText; click on the "Console" tab in the bottom docked pane (or from the menus, View -> Docked Windows -> Console if you don't see the tab). When "System" is selected as the shell, it mimics a system console like you desire.

Oh, you are right. Nice.

But it is incomplete or buggy (call it how ever you want).

Here an example program for the Console/Terminal:
Code: Select all
import java.util.Scanner;

public class ConsoleTest {
  public static void main(String[] args) {
    System.out.println("Test for InputStream");
    System.err.println("Test for ErrorStream");
    System.out.println("And now test the OutputStream:\n");
    System.out.print("What is your name? ");
    Scanner scan = new Scanner(System.in);
    String s = scan.next();
    System.out.println("Hello " + s + ", how are you?");
  }
}


Compile it and run it in a normal console/terminal and the Terminal in RText.
You see, that it don't run really right.
That the error message comes on the end, is ok. But it takes no input from the console.

So, there existing three Streams in Java: InputStream, ErrorStream and OutputStream. But your Terminal in RText only supports the first two.

Greatings
theuserbl
theuserbl
 
Posts: 12
Joined: Sun Mar 28, 2010 2:37 pm

Re: Java Console

Postby robert » Sun Mar 30, 2014 7:09 pm

That's right, the console doesn't support piping input into launched processes.

Feel free to add a feature request on GitHub. Or, if you're so inclined, patches/pull requests welcome!
User avatar
robert
 
Posts: 794
Joined: Sat May 10, 2008 5:16 pm


Return to Open Discussion

Who is online

Users browsing this forum: No registered users and 2 guests

cron