I am trying to use JConsole but I am getting some weird results ...
Michael Joyner <[email protected]> Thu, 17 Jul 2014 16:39:17 -0400
| Newsgroups | gmane.comp.java.beanshell.user |
|---|---|
| Message-ID | <CALao4gNSubJ1bD6aisinYe33G2UWkpWEsYMji-xFstUX1u5uTg@mail.gmail.com> |
First up: my environment:
bsh-2.0b4.jar
Eclipse
java version "1.7.0_55"
OpenJDK Runtime Environment (IcedTea 2.4.7) (7u55-2.4.7-1ubuntu1)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
uname -a
Linux michael-desktop 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00
UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
The console starts up and I get: 'JCE (type 'quit' to exit): '
Anything I type is directly echoed but the "readline" loop never loops.
Here is the code at this point:
package com.main;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import javax.swing.JFrame;
import bsh.util.GUIConsoleInterface;
import bsh.util.JConsole;
public class Main {
public static void main(String[] args) {
// new Thread(new Application()).start();
// define a frame and add a console to it
JFrame frame = new JFrame("JConsole example");
JConsole console = new JConsole();
frame.getContentPane().add(console);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setVisible(true);
inputLoop(console, "JCE (type 'quit' to exit): ");
System.exit(0);
}
/**
* Print prompt and echos commands entered via the JConsole
*
* @param console
* a GUIConsoleInterface which in addition to basic input and
* output also provides coloured text output and name
completion
* @param prompt
* text to display before each input line
*/
private static void inputLoop(GUIConsoleInterface console, String
prompt) {
Reader input = console.getIn();
BufferedReader bufInput = new BufferedReader(input);
String newline = System.getProperty("line.separator");
console.print(prompt, Color.BLUE);
String line;
try {
while ((line = bufInput.readLine()) != null) {
console.print("You typed: " + line + newline, Color.ORANGE);
if (line.equals("quit"))
break;
console.print(prompt, Color.BLUE);
}
bufInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
So I changed the code to use the reader directly at which point I seem to
be getting the encoded utf8 escape sequences for anything I type?
Here is the input vs output:
JCE (type 'quit' to exit): hello
You typed: \
JCE (type 'quit' to exit): You typed: u
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: 6
JCE (type 'quit' to exit): You typed: 8
JCE (type 'quit' to exit): You typed: \
JCE (type 'quit' to exit): You typed: u
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: 6
JCE (type 'quit' to exit): You typed: 5
JCE (type 'quit' to exit): You typed: \
JCE (type 'quit' to exit): You typed: u
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: 6
JCE (type 'quit' to exit): You typed: c
JCE (type 'quit' to exit): You typed: \
JCE (type 'quit' to exit): You typed: u
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: 6
JCE (type 'quit' to exit): You typed: c
JCE (type 'quit' to exit): You typed: \
JCE (type 'quit' to exit): You typed: u
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: 6
JCE (type 'quit' to exit): You typed: f
JCE (type 'quit' to exit): You typed: \
JCE (type 'quit' to exit): You typed: u
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: 0
JCE (type 'quit' to exit): You typed: a
JCE (type 'quit' to exit):
Here is the code for the new inputloop:
private static void inputLoop(GUIConsoleInterface console, String prompt) {
Reader input = console.getIn();
BufferedReader bufInput = new BufferedReader(input);
String newline = System.getProperty("line.separator");
console.print(prompt, Color.BLUE);
char buffer[] = new char[1];
String line;
try {
while (input.read(buffer)!=-1) {
console.print("You typed: " + new String(buffer) + newline,
Color.ORANGE);
// if (line.equals("quit"))
// break;
console.print(prompt, Color.BLUE);
}
bufInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Is the sample code I am working with only valid for an older version? Is
this a bug?
I got the starting code from:
http://stackoverflow.com/questions/1255373/create-a-command-console
Thanks for any help in getting myself straight...
-Mike
------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds