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

  • SEARCH
  • Home
  • 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 5934539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:07:41+00:00 2026-05-22T15:07:41+00:00

I have the following code and I can’t figure out why it won’t work:

  • 0

I have the following code and I can’t figure out why it won’t work:

final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final String p1 = "HELLO WORLD";
process(p1, bos);
Assert.assertEquals("BOS value should be: "+p1, p1, bos.toString("UTF-8"));

It prints:

junit.framework.ComparisonFailure: BOS value should be: HELLO WORLD expected:<[HELLO WORLD]> but was:<[]>
at junit.framework.Assert.assertEquals(Assert.java:81) etc…

and process looks like this:

public static void process(final String p1, final OutputStream os) {
    final Runtime rt = Runtime.getRuntime();
    try {
        final String command = "echo " + p1;
        log.info("Executing Command: " + command);
        final Process proc = rt.exec(command);

        // gobble error and output
        StreamGobbler.go(proc.getErrorStream(), null);
        StreamGobbler.go(proc.getInputStream(), os);

        // wait for the exit
        try {
            final int exitVal = proc.waitFor();
            log.info("Command Exit Code: " + exitVal);
        } catch (InterruptedException e) {
            log.error("Interrupted while waiting for command to execute", e);
        }
    } catch (IOException e) {
        log.error("IO Exception while executing command", e);
    }
}

private static class StreamGobbler extends Thread {
    private final InputStream is;
    private final OutputStream os;

    private static StreamGobbler go(InputStream is, OutputStream os) {
        final StreamGobbler gob = new StreamGobbler(is, os);
        gob.start();
        return gob;
    }

    private StreamGobbler(InputStream is, OutputStream os) {
        this.is = is;
        this.os = os;
    }

    public void run() {
        try {
            final PrintWriter pw = ((os == null) ? null : new PrintWriter(os));
            final InputStreamReader isr = new InputStreamReader(is);
            final BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                if (pw != null) {
                    pw.println(line);
                }
                log.info(line); // Prints HELLO WORLD to log
            }
            if (pw != null) {
                pw.flush();
            }
        } catch (IOException ioe) {
            log.error("IO error while globbing", ioe);
        }
    }

When I run the jUnit test I get an empty string as the actual. I don’t understand why this wont work.

EDIT: I am using RHEL5 and eclipse 3.6 if that makes a difference at all.

  • 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-05-22T15:07:42+00:00Added an answer on May 22, 2026 at 3:07 pm

    maybe you should wait on the thread filling the stream:

        Thread thr = StreamGobbler.go(proc.getInputStream(), os);
    
        // wait for the exit
        try {
            final int exitVal = proc.waitFor();
            log.info("Command Exit Code: " + exitVal);
            thr.join();//waits for the gobbler that processes the stdout of the process
        } catch (InterruptedException e) {
            log.error("Interrupted while waiting for command to execute", e);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code and I can't seem to get it to work.
I have a simple 3D cube that I can rotate using the following code:
According to what I have found so far, I can use the following code:
Following on from this question I now have code that can attach to a
We have similar code to the following in one of our projects. Can anyone
I have the following code: $bind = new COM(LDAP://CN=GroupName,OU=Groups,OU=Division,DC=company,DC=local); When I execute it from
I have the following code: As you can see, i need to pass a
I have the following code and I can't understand what does it mean: var1
I have following code: Tools::Logger.Log(string(GetLastError()), Error); GetLastError() returns a DWORD a numeric value, but
I have following code in VS2008 If Linq.Enumerable.Contains(Of String)(selectedUsersRoles, RoleCheckBox.Text) Then RoleCheckBox.Checked = True

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.