2

Im trying to open a terminal console, and be able to read / write commands to it.

I read some questions like: Java Process with Input/Output Stream

With that was able build a little app that opens the terminal and pass commands to the console and print the result back, it works well with any system comand like browsing folders, deleting files and stuff like that.

The problem I have is that I need to load another java program from that console and read its output but that program uses java.util.logging.Logger to send most of its output and for some reason my launching app can't read what Logger prints.

Basically Im trying to build like a wrapper for another java app, because I want to interact with it but cant modify it.

Thanks for your help.

EDIT

Here is the code, but its basically taken from another questions, also as I said it works for things in the "normal" stdout, but not for the output Logger prints to the console.

package launcher;

import java.io.*;
import java.util.Scanner;

public class Launcher {

    public static void main(String[] args) throws IOException {

        String line;
        Scanner scan = new Scanner(System.in);

        Process process = Runtime.getRuntime().exec("/bin/bash");

        OutputStream stdin = process.getOutputStream();
        InputStream stderr = process.getErrorStream();
        InputStream stdout = process.getInputStream();

        BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

        while (scan.hasNext()) {
            String input = scan.nextLine();
            if (input.trim().equals("exit")) {

                writer.write("exit\n");
            } else {
                writer.write("((" + input + ") && echo --EOF--) || echo --EOF--\n");
            }
            writer.flush();

            line = reader.readLine();
            while (line != null && !line.trim().equals("--EOF--")) {
                System.out.println("Stdout: " + line);
                line = reader.readLine();
            }
            if (line == null) {
                break;
            }
        }

    }
}
Community
  • 1
  • 1
josephfley
  • 31
  • 1
  • 5
  • You haven't given us much to go on here, we're basically guessing... If the logger is printing to STD OUT, then this should work, but it may be logging to file, or printing to STD ERROR, or doing a mixture of these things. – Jon Mar 26 '12 at 09:00

2 Answers2

1

Without seeing any code/config, I would guess that either the logger is configured to write to stderr (System.err) and you're only reading stdout (System.out), or else the logger is configured to write to a file.

dty
  • 18,795
  • 6
  • 56
  • 82
0

Per dty's answer, I think by default java.util.logging uses stderr, so you should redirect stderr to stdout like this:

ProcessBuilder builder = new ProcessBuilder("ls", "-l"); // or whatever your command is
builder.redirectErrorStream(true);
Process proc = builder.start();

FWIW in my experience, you'd be better off trying to use the other Java program by starting its main method in your own program, than trying to wrestle with input/output streams etc., but that depends on what the other program does.

artbristol
  • 32,010
  • 5
  • 70
  • 103