简体   繁体   English

IntelliJ IDEA 中已使用的未使用导入语句

[英]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.我是 IntelliJ IDEA 的新手,我使用的语句有警告"unused import statement" I tried mvn clean from the terminal but it didn't helps我从终端尝试过mvn clean但它没有帮助

行家

It seems that some libraries hav a broken path, but I don't know how to solve it好像有些图书馆的路径坏了,但我不知道如何解决

在此处输入图片说明

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).红色错误与本地环境中指定库缺乏可用性有关(换句话说,Lombok 依赖项未正确安装)。

The command "mvn clean" will remove local dependencies installed using Maven.命令“mvn clean”将删除使用 Maven 安装的本地依赖项。 This could indeed be the cause of the error itself, you removed Lombok jar file from your local install using "mvn clean".这确实可能是错误本身的原因,您使用“mvn clean”从本地安装中删除了 Lombok jar 文件。

To know more about this topic and be sure to fix the related error I'd need to see your POM.xml file.要了解有关此主题的更多信息并确保修复相关错误,我需要查看您的 POM.xml 文件。

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:假设您的 POM.xml 看起来不错,以便成功导入您需要运行的缺少的库:
mvn clean install -U

And then run a rebuild (from IntelliJ Rebuild menu), this should take care of the red import errors.然后运行重建(从 IntelliJ 重建菜单),这应该处理红色导入错误。 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. IntelliJ 将扫描您的代码,看到使用了“Date”导入并停止生成相关警告。

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.在 IntelliJ 中获得此结果的一种非常巧妙的方法是优化导入功能。

Here its current shortcut:这是它当前的快捷方式:
Ctrl + Alt + O 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.如果您需要更清楚或其他解释,请写评论,我很乐意提供帮助。

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.但是,如果您不需要任何导入,只需删除它们即可。

Optimize imports in the current file优化当前文件中的导入

1.On the main menu, choose Code | 1.在主菜单上,选择代码| Optimize Imports.优化进口。

2.Press Ctrl+Alt+O. 2.按 Ctrl+Alt+O。

3.Place the caret at the import statements, click icons actions 3.将插入符号放在导入语句处,单击图标操作 , and choose Remove unused import. ,然后选择删除未使用的导入。

4.Open the Reformat File Dialog (Ctrl+Shift+Alt+L) and select the Optimize imports checkbox. 4. 打开重新格式化文件对话框 (Ctrl+Shift+Alt+L) 并选中优化导入复选框。

you can change your intelliJ preferences.您可以更改您的智能偏好。

Preferences->Editor->Auto Import->Optimize Imports on the Fly will automatically remove unused imports Preferences->Editor->Auto Import->Optimize Imports on the Fly 将自动删除未使用的导入

Try to delete all import statements, then go to usages of imports and try to alt-enter .尝试删除所有导入语句,然后转到导入的用法并尝试alt-enter This way IDE imports them by itself, and would give an error in case you added libraries in a wrong way.这样 IDE 会自行导入它们,如果您以错误的方式添加库,则会出现错误。 Perhaps build project and clean project inbetween steps just to be sure也许在步骤之间build projectclean project只是为了确定

如果没有其他工作,请尝试无效缓存和重新启动选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM