Ashish Ashish - 1 month ago 8
Java Question

How to add a line in all java files in eclipse

I am not sure if that is possible but I have a old java application projects which have 1000+ java files. I am trying to add log4j support to the application which require me to add

public static Logger logger = Logger.getLogger(MyClass.class.getName());


in every file.

Is there any way I can perform the operation using eclipse. I have tried
source->format
but that is not allowing me to add the line. Do I have to open every file and add that line?

Answer

You could make use of templates in eclipse, but in this case, you need to edit each file and add it.

Update :

Save following content in some file-named with extension ".xml"

<?xml version="1.0" encoding="UTF-8" standalone="no"?><templates><template autoinsert="true" context="java-members" deleted="false" description="adds the logger statement" enabled="true" name="logger">public static Logger logger = Logger.getLogger(${enclosing_type}.class.getName());</template></templates>

Press CTRL+3, Type - "templates" and choose for Templates- Java Editor as shown below

enter image description here

Import the file from menu from right as shown below

enter image description here

now go to any of your file and type "logger"in your class file and do CTRL+space , quick assist will show you the "logger" template as shown below

enter image description here and your logging statement will appear, with your class in which you are editing as shown below

enter image description here

Comments