3

This may be hard to understand, but I want to get the output of a java application in a console C# app.

When you start a java application with Process.Start in a C# console app, the java application takes control, and all lines are written using System.Out.Println().

I want to start a java application, make it so System.out.println() gets stored in a variable and not printed, and then reprint what the variable is with Console.WriteLine()

Allow me to rephrase. #1 Start java app in c# console app with process.start. #2 Cancel java's attempts to output what it was going to output with System.out.prinln(), and instead store it in a variable/string. #3 reprint that string using Console.WriteLine().

Redirecting standard output does not work for this.

If you are wondering, this is for minecraft bukkit.

6
  • msdn.microsoft.com/en-us/library/… has example Commented May 10, 2016 at 22:38
  • What lashane said... or do something along the lines of Named Pipes, TCP/IP, Filesystem, Mailslots, etc. Commented May 10, 2016 at 22:39
  • @Lashane Redirect standard output does not produce results I am expecting. It doesn't work. Commented May 10, 2016 at 22:40
  • I feel like using filesystem is messy... I want a more programmatic way of going about this Commented May 10, 2016 at 22:41
  • @halpme142 it should not produce results you're expecting, it should return what was printed to standard output, also there is exists RedirectStandardError Commented May 10, 2016 at 22:41

1 Answer 1

4

Java application:

public class JavaApplication {

    public static void main(String[] args) {
        System.out.println("Java application outputs something into stdout");
        System.err.println("Java application outputs something into stderr");
    }
}

C# application

using System;
using System.Diagnostics;

namespace CaptureProcessStdOutErr
{
  public class Program
  {
    public static void Main(string[] args)
    {
      var startInfo = new ProcessStartInfo("java", "JavaApplication") // proper path to java, main java class, classpath, jvm parameters, etc must be specified or use java -jar jarName.jar if packaged into a single jar
      {
        RedirectStandardError  = true,
        RedirectStandardOutput = true,
        UseShellExecute        = false
      };

      var process = Process.Start(startInfo);

      process.WaitForExit();

      Console.WriteLine("Captured stderr from java process:");
      Console.WriteLine(process.StandardError.ReadToEnd());
      Console.WriteLine();
      Console.WriteLine("Captured stdout from java process");
      Console.WriteLine(process.StandardOutput.ReadToEnd());
    }
  }
}

This assumes java.exe is in the PATH.

Compile JavaApplication and put JavaApplication.class file next to C# application (CaptureProcessStdOutErr.exe)

run CaptureProcessStdOutErr.exe

Output:

Captured stderr from java process:
Java application outputs something into stderr

Captured stdout from java process
Java application outputs something into stdout
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.