Sandro Rey
Sandro Rey

Reputation: 2989

unused import statement for used ones in IntelliJ IDEA

I am new in IntelliJ IDEA and I have the warning "unused import statement" for an statements that I use. I tried mvn clean from the terminal but it didn't helps

maven

It seems that some libraries hav a broken path, but I don't know how to solve it

enter image description here

Upvotes: 5

Views: 12163

Answers (6)

user12488372
user12488372

Reputation: 11

try the Invalidate Cache and Restart option if nothing else works

Upvotes: 1

Pitto
Pitto

Reputation: 8579

I will assume that you want to use those imports in your code.

The warnings, in gray, are suggesting you to remove imports of libraries that you are then NOT using in your code (in order to save resources and gain other advantages).

The errors, in red, are related to the lack of availability of the specified library in your local environment (in other words Lombok dependency is not properly installed).

The command "mvn clean" will remove local dependencies installed using Maven. This could indeed be the cause of the error itself, you removed Lombok jar file from your local install using "mvn clean".

To know more about this topic and be sure to fix the related error I'd need to see your POM.xml file.

Nonetheless I would like to try to provide help right away.

Assuming that your POM.xml looks good in order to successfully import the missing libraries you'd need to run:
mvn clean install -U

And then run a rebuild (from IntelliJ Rebuild menu), this should take care of the red import errors. Then, to remove the warnings, you should just use the imported libraries in your code.

Here's an example that would remove the warning related to Date, just as an example:

String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

String date = simpleDateFormat.format(new Date());
System.out.println(date);

IntelliJ will scan your code, see that "Date" import is used and stop generating the related warning.

If, on the other hand, you are not using those import statements I would just suggest to simply remove the unused import lines from the class. The end result will be the same, you will no longer get errors and/or warnings.

A very neat way to obtain this result in IntelliJ is the Optimize Imports function.

Here its current shortcut:
Ctrl+Alt+O

This will take care not only of removing the unused imports but also to optimize them in order to minimize resource usage and attack surface.

Please write a comment if you need more clarity or other explanations, I'd be glad to help.

Upvotes: 5

Ori Marko
Ori Marko

Reputation: 58772

Optimize imports in the current file

1.On the main menu, choose Code | Optimize Imports.

2.Press Ctrl+Alt+O.

3.Place the caret at the import statements, click icons actions , and choose Remove unused import.

4.Open the Reformat File Dialog (Ctrl+Shift+Alt+L) and select the Optimize imports checkbox.

Upvotes: 0

arellak
arellak

Reputation: 112

It seems like you didn't add your library to the project.
Go to File -> Project settings -> Library (or something similar) and add it.

But if you don't need any of the imports just remove them.

Upvotes: 1

keser
keser

Reputation: 2642

Try to delete all import statements, then go to usages of imports and try to alt-enter. This way IDE imports them by itself, and would give an error in case you added libraries in a wrong way. Perhaps build project and clean project inbetween steps just to be sure

Upvotes: 0

Vishal Kumar
Vishal Kumar

Reputation: 53

you can change your intelliJ preferences.

Preferences->Editor->Auto Import->Optimize Imports on the Fly will automatically remove unused imports

Upvotes: 0

Related Questions