Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8637081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:23:29+00:00 2026-06-12T10:23:29+00:00

Recently I was trying to implement the functionality of a normal terminal to a

  • 0

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! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:23:30+00:00Added an answer on June 12, 2026 at 10:23 am

    Assuming these Apache libraries implement the InputStream and OutputStream interfaces, you can use PipedInputStream and PipedOutputStream to access information. Here’s a quick example:

    import java.awt.event.*;
    import java.io.*;
    import javax.swing.*;
    
    public class InputRedirection extends Box{
    
        public InputRedirection() {
            super(BoxLayout.X_AXIS);
    
            //Remap input
            //Create the input stream to be used as standard in
            final PipedInputStream pisIN = new PipedInputStream();
            //Create an end so we can put info into standard in
            PipedOutputStream posIN = new PipedOutputStream();
            //Wrap with a writer (for ease of use)
            final BufferedWriter standardIn = new BufferedWriter(new OutputStreamWriter(posIN));
            //Set standard in to use this stream
            System.setIn(pisIN);
    
            //Connect the pipes
            try {
                pisIN.connect(posIN);
            } catch (IOException e2) {
                e2.printStackTrace();
            }        
    
            //UI element where we're entering standard in
            final JTextField field = new JTextField(20);
            ActionListener sendText = new ActionListener(){
    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    try {
                        //Transfering the text to the Standard Input stream
                        standardIn.append(field.getText());
                        standardIn.flush();
                        field.setText("");
                        field.requestFocus();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }};
    
            field.addActionListener(sendText);
            add(field);
    
            //Why not - now it looks like a real messaging system
            JButton button = new JButton("Send");
            button.addActionListener(sendText);
            add(button);
    
            //Something using standard in
            //Prints everything from standard in to standard out.
            Thread standardInReader = new Thread(new Runnable(){
    
                @Override
                public void run() {
                    boolean update = false;
                    final StringBuffer s = new StringBuffer();
                    while(true){
                        try {
    
                            BufferedInputStream stream = new BufferedInputStream(System.in);
                            while(stream.available() > 0){
                                int charCode = stream.read();
                                s.append(Character.toChars(charCode));
                                update = true;
                            }
                            if(update){
                                //Print whatever was retrieved from standard in to standard out.
                                System.out.println(s.toString());
                                s.delete(0, s.length());
                                update = false;
                            }
    
                        } catch (IOException e) {
                            e.printStackTrace();
                        } 
                    }
                }});
            standardInReader.start();
    
        }
    
        public static void main(String[] args){
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new InputRedirection());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently come across a problem trying to implement Model-View-Controller in Swing .
Recently I was trying some practice programs in python and I came across this
Short Working on login system and trying to implement remember me feature. Recently, l
I’m looking for some advice. I recently wrapped up a project where I inherited
I recently ran across this problem while trying to implement a service that has
Background: I recently completed a tic-tac-toe game in AS3, using some simple function-based code
I'm trying to fix a design flaw that I recently ran across in some
I've recently downloaded Rhino.Security and I am trying to implement permissions on a entity.
I have been trying to implement the new scope features that were recently added
I recently played around with the ASP.NET WebAPI, including some Html-Pages displaying results, trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.