Reputation: 51
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?
Upvotes: 5
Views: 5211
Reputation: 407
I've used the jEdit text editor to accomplish tasks like this. There are probably other text editors that can do the same, the important feature is find and replace searching through directories/subdirectories.
I'm not a Java programmer, but this solution should work for any language. Hope that helps. Good luck!
Upvotes: 1
Reputation: 20885
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'
Upvotes: 3
Reputation: 29806
You can make all System.out.println
in your application not to print anything in console. Create a class like:
import java.io.PrintStream;
public class MyStream extends PrintStream {
private static final MyStream INSTANCE = new MyStream();
public static void init() {
System.setOut(INSTANCE);
}
private MyStream() {
super(System.out);
}
@Override
public void println(Object x) {
return;
}
@Override
public void println(String x) {
return;
}
}
Use it as:
public class Test {
public static void main(String... args) {
MyStream.init();
System.out.println("This line will not print");
}
}
The will not be printed anymore.
Upvotes: 0
Reputation: 25950
As Jigar Joshi said, using find & replace approach might help you a lot, especially if you don't interfere the code apart from System.out.println
.
I would propose a different solution if editing/changing the source code is not MUST for you. You can disable the System.out
stream in your driver program, then nothing will be printed by these statements. You only need to set it like this:
System.setOut(new PrintStream(new OutputStream() {
public void write(int b) {
// No operation.
}
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) {}
}));
Upvotes: 2
Reputation: 240900
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
Upvotes: 6
Reputation: 8278
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.
Upvotes: 2