For example i want that the

System.out.println("blablabla");

wont be displayed on the screen of the console....


how can I do it?

Dani AI

Generated

When printlns originate inside third-party code you call, a safe way to keep your console clean without touching global state is to run that work in a separate JVM and discard its stdout. Using ProcessBuilder you can redirect the child process output so nothing reaches your console:

Process p = new ProcessBuilder("java", "-cp", classpath, "com.example.YourMain")
    .redirectOutput(ProcessBuilder.Redirect.DISCARD)   // Java 9+
    .redirectError(ProcessBuilder.Redirect.INHERIT)    // keep errors visible
    .start();
int exitCode = p.waitFor();

See the Redirect options in the JDK docs for details (ProcessBuilder).

If your goal is only to suppress output during tests, consider capturing stdout programmatically and then ignoring it. The System Lambda library does this reliably and restores the original streams automatically:

import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut;

String ignored = tapSystemOut(() -> {
    callApiThatPrints();
});

Dependency and usage are documented on the project page (System Lambda). This avoids race conditions that can occur when mutating global stdout in multi-threaded applications.

Recommended Answers

All 6 Replies

Where do want it to be displayed because I don't think that it can be done.

I would suggest you look at the API for
PrintStream

Because the System.out is type of PrintStream.

This will behave exactly like the System.out.println

PrintStream print = new PrintStream(System.out);
        print.println("aaaaa");

the print outs are beeing called from some API's calling.... so i want to disable/ re route the standart print out before calling the API and enable it after finished using the API.....

I think you do not understand what you want to do. System.out.println() are system calls you placed there for some reason by programmer (in this case by you, and you may not complitelly understand them). If you want to threat data handled by system calls differently you have to tell us if you wish to store them (file read/writing), show them to user in different form (GUI or just simple dialogue po-ups) or you do not wish to see any warning messages from your system in console.
So please explain your self as above doesn't make any sense...

Peter is right. If you don't want the message to printed at the console, don't call that method.

And as I asked in my post:

Where do want it to be displayed?

> how can I do it?

By reassigning the out member of the System class using setOut(PrintStream) .

System.setOut(new PrintStream(new OutputStream() {
  public void write(int b) {
    // NO-OP
  }
}));

Or simply redirect the output to a file or /dev/null [unix] when running the Java command:

java your.pkg.your.class > log.txt
java your.pkg.your.class > /dev/null

The kind of functionality you are seeking i.e. conditional enable/disable of print statements and redirection to a different source are screaming out for the want of a logger library. Look into Log4j for more details.

thanks ~s.o.s~

U pointed me with the solution! :)

here what i did:

System.out.println("text is visible");
		PrintStream printStreamOriginal=System.out;


                System.setOut(new PrintStream(new OutputStream(){
			public void write(int b) {
			}
		}));
		System.out.println("this text will be lost");



                System.setOut(printStreamOriginal);
		System.out.println("text is visible again....");
commented: Bruh thank you so much, saved my courswork +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.