[JAVA] Prevent unused imports from being deleted when saving in Eclipse

Why inconvenience

In Eclipse, when I temporarily comment out the source code and save it, the import statement of the class used there disappears.

Of course, it is troublesome to write an import statement when returning from commenting out, but I am afraid of import mistakes. When importing, the import statement is automatically entered just by selecting from the list due to the excellent suggestion function of Eclipse. Some of the class names to be imported have the same class name in different packages, so there is no mistake in selecting them when returning from commenting out.

environment

Eclipse Java EE IDE for Web Developers.

Version: Neon Release (4.6.0) Build id: 20160613-1800

Before improvement

改善前の設定(import文が消える).PNG ↑ Settings before improvement

Try to find the setting item

In the image above "Settings before improvement", check "Additional actions" so that you can press the "Configure" button, and when you press the "Configure" button, a screen like the image below opens.

「追加保管アクション」ダイアログ内に「未使用のインポートの除去」設定項目がある様子.PNG

In the "Unnecessary Code" tab, there is a setting item "Remove unused imports". Make sure it is unchecked and click OK to close the screen.

改善案の設定①(import文が消える).PNG

I think this will improve it, but ** it disappears **.

For some reason it didn't work

(Before the improvement, "Additional action" was not checked and the setting was not specified, so I thought that I could go if I checked it.)

solution

As explained in this page, the import statement did not disappear when "Organize import" was unchecked.

It seems that the ** "Remove unused imports" setting item of "Additional actions" is ignored and the "Organize imports" setting is prioritized **. I don't know what I'm doing in "Organizing Imports".

So, I made this setting.

改善案の設定②(import文が消えない)(成功!).PNG

Of course, you don't have to check "Additional actions" as shown in the image below.

改善案の設定③(import文が消えない)(成功!).PNG

that's all.

Recommended Posts

Prevent unused imports from being deleted when saving in Eclipse
Prevent unused imports from being deleted when saving in Eclipse
How to prevent past dates from being entered in Rails forms
Settings to delete unused Java imports when saving with VS Code