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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:33:47+00:00 2026-05-14T21:33:47+00:00

I’m running an application from inside another one for testing purposes. I want to

  • 0

I’m running an application from inside another one for testing purposes. I want to redirect the output for the tested app to a file, so I can have a log after each test.

Is there a way to redirect the output of an app to a file from the command line in java?

  • 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-14T21:33:48+00:00Added an answer on May 14, 2026 at 9:33 pm

    You can use the output stream redirector that is supported by the Windows command line, *nix shells , e.g.

    java -jar myjar.jar > output.txt
    

    Alternatively, as you are running the app from inside the vm, you could redirect System.out from within java itself. You can use the method

    System.setOut(PrintStream ps)

    Which replaces the standard output stream, so all subsequent calls to System.out go to the stream you specify. You could do this before running your wrapped application, e.g. calling System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("output.txt"))));

    If you are using a wrapper that you can’t modify, then create your own wrapper. So you have FEST wrapper -> stream redirector wrapper -> tested app.

    For example, you can implement a simple wrapper like this:

    public class OutputRedirector
    {
       /* args[0] - class to launch, args[1]/args[2] file to direct System.out/System.err to */
       public static void main(String[] args) throws Exception
       {  // error checking omitted for brevity
          System.setOut(outputFile(args(1));
          System.setErr(outputFile(args(2));
          Class app = Class.forName(args[0]);
          Method main = app.getDeclaredMethod("main", new Class[] { (new String[1]).getClass()});
          String[] appArgs = new String[args.length-3];
          System.arraycopy(args, 3, appArgs, 0, appArgs.length);
          main.invoke(null, appArgs);
       }
       protected PrintStream outputFile(String name) {
           return new PrintStream(new BufferedOutputStream(new FileOutputStream(name)), true);
       }
    }
    

    You invoke it with 3 additional params – the Main class to run, and the output/error directs.

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

Sidebar

Related Questions

No related questions found

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.