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 8232433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:50:02+00:00 2026-06-07T17:50:02+00:00

Hi I’m new to Java programming. I am trying to execute an external command

  • 0

Hi I’m new to Java programming. I am trying to execute an external command from Java and then show the command prompt output in JTextArea in real time. That external program will generate 1 line of output every second and then exit after 10 seconds.

The following are my Java codes:

original codes have been deleted to save space after reading Kumra's answer

When I run program.exe manually in command prompt window, the output is updated in real time like this:

<some warning message of the .exe program
    which should not affect the output of the program> // shown at t=0
output line 1   //shown at t=1
output line 2   //shown at t=2
output line 3   //shown at t=3
output line 4   //shown at t=4
output line 5   //shown at t=5
output line 6   //shown at t=6
output line 7   //shown at t=7
output line 8   //shown at t=8
output line 9   //shown at t=9
output line 10  //shown at t=10

Done. //shown at t=10.

When I run my Java program at above, I thought JTextArea will be updated in real time to show the command prompt output. Unfortunately, it is not working. The actual output is like this:

<some warning message of the .exe program
    which should not affect the output of the program> // shown at t=0

From t=0 until t=10, the JTextArea get stuck at the above output. At t=11, it suddenly show the complete output:

<some warning message of the .exe program
    which should not affect the output of the program> // shown at t=0
output line 1   //shown at t=11
output line 2   //shown at t=11
output line 3   //shown at t=11
output line 4   //shown at t=11
output line 5   //shown at t=11
output line 6   //shown at t=11
output line 7   //shown at t=11
output line 8   //shown at t=11
output line 9   //shown at t=11
output line 10  //shown at t=11

Done. //shown at t=11

May I know what’s wrong with my codes? Can anyone teach me how to display command prompt output in JTextArea in real time? Thanks.

EDIT 1:
I have edited the codes based on Kumar’s answer but it is still not working. Below are the latest codes.

MyUI.java

public class MyUI extends JFrame
implements ActionListener, KeyListener, ChangeListener, WindowListener
{
    ...
    private JTextArea output;

    public void showMessage(String message)
    {
        output.append(message + "\n");
        output.setCaretPosition(output.getDocument().getLength());
    }
    ...

    public void actionPerformed(final ActionEvent e)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                if (xxxxx)
                {
                    myThreadInstance = new MyThread(xx,xxx,xx,xx,xx);
                    myThreadInstance.start();
                }
            }
        }
    }

}

MyThread.java

public class MyThread extends Thread
{
    ...
    public MyUI myFrame;
    ...

    public void run
    {
        try
        {
            String command = "program.exe arg1 arg2 arg3 arg4";

            List<String> items = Arrays.asList(command.split("\\s+"));

            builder = new ProcessBuilder(items);
            builder.redirectErrorStream(true);

            process = builder.start();

            input = new BufferedReader(new InputStreamReader(process.getInputStream()));

            String inputline = null;
            while ((inputline = input.readLine()) != null)
            {
                myFrame.showMessage(inputline);
            }
        }
        catch(){}
        finally{}
    }
}
  • 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-07T17:50:03+00:00Added an answer on June 7, 2026 at 5:50 pm

    Try this…

    1. Always keep the UI work on the UI thread and Non-UI work on the Non-UI thread.
    2. Event Dispatcher Thread is the UI thread, which is responsible for the Gui.
    3. You are doing the process of counting from t=0 to t=10 on the UI thread, that’s the reason
      you don’t see any output till t=11.
    4. Create a separate thread to do the counting on the cmd, or use SwingWorker provided in
      Swing to synchronize the UI and Non-UI thread.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.