I'm debugging a Java application that helpfully prints this to the console about a dozen times during startup:
java.awt.Dimension[width=140,height=122]
I want to shut it up but I have no idea where it's coming from. The application is huge and I don't know much about it. I can probably find the culprit by stepping through the code as it runs but I'm wondering is there a cleverer way?
I found my offending print statement (inside a getPreferredSize() method), but I also found a more general solution. By replacing System.out
using System.setOut
it's possible, in theory, to catch a print statement when it happens.
This isn't fully reliable as: (1) class PrintStream has many printing methods for different data types and there isn't a good single method to override. The methods that do the real output are private. (2) If it wanted to, code could split a message up into individual characters, so there would be no easy way to do the String.contains()
check.
Still, as a quick debugging hack, this seems to work nicely:
System.setOut(new java.io.PrintStream(
new java.io.FileOutputStream(java.io.FileDescriptor.out)) {
@Override
public void print(String s) {
super.print(s);
if (s.contains("java.awt.Dimension")) {
throw new RuntimeException("Found you!");
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With