Recently I was trying to implement the functionality of a normal terminal to a graphically designed Swing-based console project. I love how some people in here made this possible, but yet I stumbled upon another big kind of problem. Some people actually spoke about InpuStreamListener although I am not too fond of this. A sample code of my work (pretty much not exactly mine, but it is the source code of my app) would be the following:
// Making an executor
org.apache.commons.exec.DefaultExecutor exec = new org.apache.commons.exec.DefaultExecutor();
// Creating the streams (pretty much ignore this, I just include it as a general idea of the method)
consoleOutputStream = new ConsoleOutputStream();
consoleInputStream = new JTextFieldInputStream(gsc.mainWindow.getConsoleInput().getJTextField());
// Stream Handler with the customized streams I use for the process
org.apache.commons.exec.PumpStreamHandler streamHandler = new org.apache.commons.exec.PumpStreamHandler(consoleOutputStream, consoleOutputStream, consoleInputStream);
// Setting the handler and finally making command line and executing
exec.setStreamHandler(streamHandler);
org.apache.commons.exec.CommandLine commandline = org.apache.commons.exec.CommandLine.parse(String.valueOf(arg));
exec.execute(commandline);
Now the thing is I generally try to run java application through the java commands through this method. OutputStream works really fine, no flaws whatsoever and gives me all it shall, but applications with Input give me a lot of trouble. I beieve the problem resides in the hardcoding to System.in, the Scanner class, the Console class etc. So here’s what I need some help with (finally):
I want to either be able to directly access the InputStream passed to my application or someone explaining a way to me of how to actually write an InputStreamListener that will occasionaly be used when I run external java applications (yes, I run them through my interface instead of cmd or terminal, I am trying to make a tool here). If this is too complicated, needs a lot of tweaking on my side or is generally quite impossible, can someone help me just to get the passed InputStream so I can actually write a class that will allow me to write applications specific to my interface?
Thanks in advance and thanks really even for putting the time to read this whole text! 🙂
Assuming these Apache libraries implement the
InputStreamandOutputStreaminterfaces, you can usePipedInputStreamandPipedOutputStreamto access information. Here’s a quick example:Oh – and one thing to think about when using PipedStreams: Only one thread can write to the Output and only one can read from the input. Otherwise you get some funky problems (see http://techtavern.wordpress.com/2008/07/16/whats-this-ioexception-write-end-dead/ for more details).