1

in a project I need to execute a java application from another java application. So far so good. Now I am stuck at the point, where the former needs to fetch the latters logs from the console.

The execution of the second is handled by the following snipped of code:

ArrayList<String> commands = new ArrayList<>();
    commands.add("javaw");
    commands.addAll(data.returnPropertiesAsPrefixedStringList());
    commands.add("-cp");
    commands.add(data.getClasspathJar().toString());
    commands.add("[PathToMainClass]");

    //data.getArgs for example contains the path to the second java (jnlp) file
    commands.addAll(data.getArgs());

    ProcessBuilder java = new ProcessBuilder().inheritIO().command(commands);
    Process start = java.start();

How can I now fetch the logs "start" writes to the console? As I did not quite know what I had to search for, I did not manage to find fitting answers.


Edit:

I have tried getting the logs like that, but it did not work. The Logs weren't recognised at all.

Process start = java.start();
    BufferedReader input = new BufferedReader(new InputStreamReader(start.getInputStream()));
    String line;
    while ((line = input.readLine()) != null) {
        System.out.println("It Did Work: " + line);
    }

Thank you in advance for your help.

-IHaveNoClue

1 Answers1

1

Process has a getInputStream() operation, so you could use an inputstream reader to access this data. Also maybe this discussion will help you: printing-runtime-exec-outputstream-to-console

Andre Albert
  • 1,386
  • 8
  • 17
  • Well I have tried that with the following code: Process start = java.start(); BufferedReader input = new BufferedReader(new InputStreamReader(start.getInputStream())); String line; while ((line = input.readLine()) != null) { System.out.println("It Did Work: " + line); } input.close(); – Ihavenoclue Jun 28 '18 at 09:01
  • I AM SORRY - I DONT GET THE FORMATTING IN THE COMMENTS RIGHT – Ihavenoclue Jun 28 '18 at 09:07
  • 1
    You could edit the original post with those additional information @Ihavenoclue – Andre Albert Jun 28 '18 at 09:09