Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.out.println removal/comment from multiple java source files

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?

like image 776
Snehika Avatar asked Dec 26 '12 17:12

Snehika


People also ask

How do I get rid of system out Println?

You can do it by find in Project option then give System. out. println( to your search criteria then remove them all.

What is out Println?

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.


3 Answers

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

  • How to remove System.out.println's from codebase
like image 167
jmj Avatar answered Oct 03 '22 16:10

jmj


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'
like image 40
Raffaele Avatar answered Oct 03 '22 17:10

Raffaele


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.

like image 26
aviad Avatar answered Oct 03 '22 16:10

aviad