0

After a long period of searching for my problem, I have no other idea, I have to ask here. I search for a method to read the console output or System.out to a JavaFX TextArea but I don't know how I get those strings.

I want to put that into an external Thread:

package Threads;

import Core.Engine;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class ConsoleListenerThread implements Runnable {

    Engine engine;

    public ConsoleListenerThread(Engine engine) {
        this.engine = engine;
    }

    @Override
    public void run() {
        String line;
        while (true){

        }
    }
}

Update:

I really need the console output...what you see when you start a program. And how I get there is unnecessary I think (Startup etx), because I just want a String whenever the console prints a new output.

Example:

System.out.println("Hello");

And then my thread revive this string:

"hello"
Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
Noah Till
  • 53
  • 6
  • 1
    You need to share more details about the setup. Is it external code you want to hijack? Is it already running when your code loads into? Or is it the same project? If so, why not just `System.setOut(...)` to a printer of yours? Or why not reworking your output to first go through some class of yours instead of using `System.out` everywhere? And what has your code to do with the question? Right now your question is **unclear**. – Zabuzard Aug 18 '19 at 19:08
  • What do you mean by "read the console output [...] to a JavaFX `TextArea`"? Do you mean "read the console *input*"? – trylimits Aug 18 '19 at 19:08
  • Have you tried `System.setOut(printStream);`? For more details [How to redirect console content to a textArea in Java?](https://stackoverflow.com/questions/5107629/how-to-redirect-console-content-to-a-textarea-in-java) – Butiri Dan Aug 18 '19 at 19:12

2 Answers2

0

If you are printing directly to the console with the System.out.println() method, then you already have the string. If , on the other hand, you are calling a different method that prints to the console from another object, try having that method return a String instead so that you can store that data.

0

So I solved this problem, so here is the code:

package Threads;

import Core.Engine;

import java.io.*;

public class ConsolListenerThrad implements Runnable {

    Engine engine;

    public ConsolListenerThrad(Engine engine) {
        this.engine = engine;
    }

    @Override
    public void run() {
        String line = null;

        PipedOutputStream pOut = new PipedOutputStream();
        System.setOut(new PrintStream(pOut));
        PipedInputStream pIn = null;
        try {
            pIn = new PipedInputStream(pOut);
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(pIn));
        while (true){
            try {
                line = reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            engine.printConsollogOnGui(line);
        }
    }
}
Noah Till
  • 53
  • 6