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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:58:22+00:00 2026-05-31T00:58:22+00:00

I have to write a program that is required to run several other programs

  • 0

I have to write a program that is required to run several other programs via command line calls. Program 1 produces a file that program 2 then reads. I have followed various examples on how to do this safely but still have one annoying problem . The code below largely works, but only if the system.out after the waitfor() is present. If I take this out I get error (see below). I am somewhat uncomfortable with the code if I don’t know why something is happening.

Any help appreciated.

Code:

public static int executeCommandWithOutputfile(String command,
      String outputFile) {
   int exitVal = 0;

   try {
      FileOutputStream fos = new FileOutputStream(outputFile);
      Runtime rt = Runtime.getRuntime();
      Process proc = rt.exec(command);

      // any error message?
      StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(),
            "ERROR");

      // any output?
      StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(),
            "OUTPUT", fos);

      // kick them off
      errorGobbler.start();
      outputGobbler.start();

      // any error???
      exitVal = proc.waitFor();
      //don't remove this system out, when I did I got errors.
      //System.out.println("ExitValue: " + exitVal);
      fos.flush();
      fos.close();
   } catch (Throwable t) {
      t.printStackTrace();
   }
   return exitVal;
}

Output with System.out line intact:

    ExitValue: 0
    ExitValue: 0
    ExitValue: 0
    Release note complete.

Output with system.out removed:

    [Fatal Error] tmpRelNote2482381115742032425xml:1:1: Premature end of file.
    Error : The XML config file is not valid!
    Exception in thread "main" java.lang.NullPointerException
        at rwe.release.CreateReleaseNote.insertReleaseInfo(CreateReleaseNote.java:96)
        at rwe.release.CreateReleaseNote.writeReleaseNote(CreateReleaseNote.java:81)
        at rwe.release.CreateReleaseNote.<init>(CreateReleaseNote.java:30)
        at rwe.release.CreateReleaseNote.main(CreateReleaseNote.java:19)
    Process exited with exit code 1.
  • 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-31T00:58:23+00:00Added an answer on May 31, 2026 at 12:58 am

    Your code has a race condition. The OutputGobbler may or may not be finished gobbling the output of the process when you call fos.close(). The println slows this thread down enough to give the gobbler time to finish processing the output of the process.

    You must make sure the OutputGobbler has finished transferring all the output to the file before you close the file.

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

Sidebar

Related Questions

I have a program that I run from the command line that looks like
I have a module that needs to run a small .Net command-line program to
I have to write a program that read from a file that contains the
I need to write a relatively simple command line C++ program to be run
I have to write a program that sniffs network packets (part1-the simple part). And
I wanted to write a program that test if two files are duplicates (have
I have been assigned wit the task to write a program that takes a
I have a Perl program, that needs to use packages (that I also write).
I have a program that writes to a FILE *cgiOut and just after it
I have two (UNIX) programs A and B that read and write from stdin/stdout.

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.