I want to remove/comment all the occurrences of System.out.println
from my java code.
This System.out.println
may be inside if
,if else
,for
, while
or any where. I cannot do it manually due to large source code files.
Please help me automate this process. Is there any refactoring tool available for this task? Can I use eclipse JDT for this?
You can do it by find in Project option then give System. out. println( to your search criteria then remove them all.
println() is used to print an argument that is passed to it. The statement can be broken into 3 parts which can be understood separately as: System: It is a final class defined in the java. lang package.
Update
As mentioned here create a NullOutPut
public final DevNull {
public final static PrintStream out = new PrintStream(new OutputStream() {
public void close() {}
public void flush() {}
public void write(byte[] b) {}
public void write(byte[] b, int off, int len) {}
public void write(int b) {}
} );
}
and replace System.out.print
with DevNull.out.print
and later switch to logging framework that will allow you to handle stuff easily
Linked
You could use raw find/replace functionality in source code files to comment out (or remove) statements, but whatever regular expression can be easily broken, so maybe you'd better be aware of logging frameworks like log4j or slf4j.
Big picture first: instead of using the usual System.out.println()
, you'll end up using a line of code like:
logger.debug("Just entered main");
The logging framework can be configured with a simple property file, so you can have multiple appenders (console, file, database, whatever) and shut down each one separately on demand (for example the console appender). To switch to a logging API you still have to perform raw find/replace on source files, and possibly fix a couple of things by hand, either whithin your IDE, or with a command like:
find src/ -name '*.java' | \
xargs sed -i -e 's/System.out.println/logger.verbose/g'
In case you do not use IDE, on Linux:
sed '/System.out.println/d' %YOUR_PROJ_DIR%/*.java
on Windows you can do the same if you have a cygwin installed.
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