Snehika
Snehika

Reputation: 51

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?

Upvotes: 5

Views: 5211

Answers (7)

Noah Dyer
Noah Dyer

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.

  1. Open up a find and replace dialogue.
  2. Make sure the search folders and subdirectories options are checked. Again, this is the key to getting things handled across your entire project.
  3. Write "System.out.println" in the find field. Write a regular expression to handle the arguments.
  4. Write //System.out.println or perhaps more safely "/*System.out.println*/" in the replace field (or just leave it blank if that's preferable). Use the regular expression matches to replace the original arguments to the function.
  5. I suggest you replace the first few occurrences manually to make sure everything is going as expected. Once you're convinced it is, hit replace all and celebrate.

I'm not a Java programmer, but this solution should work for any language. Hope that helps. Good luck!

Upvotes: 1

Raffaele
Raffaele

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

Tapas Bose
Tapas Bose

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

Juvanis
Juvanis

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

Jigar Joshi
Jigar Joshi

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

aviad
aviad

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

Ivo
Ivo

Reputation: 450

Do a simple find and replace. That should do.

Upvotes: 0

Related Questions