简体   繁体   English

Intellij手动添加jar会出现编译错误

[英]Compilation error occurs if jars are added manually in Intellij

I am using Maven in my project, and for some reasons, some additional jars should be added manually (I have followed the step like Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project ).我在我的项目中使用 Maven,出于某些原因,应该手动添加一些额外的 jars(我已经按照正确的方法将外部 jars (lib/*.jar) 添加到 IntelliJ IDEA 项目)。

The package can be imported successfully.可以成功导入包。 However, the compilation error happens, which indicates the package does not exist and cannot find the symbol of the used object.但是出现编译错误,说明包不存在,找不到使用对象的符号。

I have tried the following tips but it remain unchanged:我尝试了以下提示,但它保持不变:

  1. Invalid caches / restarts无效的缓存/重新启动
  2. reimport重新进口
  3. delete .idea file and .iml file删除 .idea 文件和 .iml 文件

The scenario is quit similar to this one : https://intellij-support.jetbrains.com/hc/en-us/community/posts/206821195--beginner-question-including-external-jar-compile-error .该场景与此类似: https : //intellij-support.jetbrains.com/hc/en-us/community/posts/206821195--beginner-question-包括-external-jar-compile-error

Please see the following sample images.请参阅以下示例图像。 It may run successfully but cannot be compiled well.它可能运行成功,但不能很好地编译。

IntelliJ 截图

Maven编译错误截图

The reason is that when you add a library manually via IntelliJ, only IntelliJ knows about them and when you compile your code using Maven, it can't be find by Maven because Maven only searches for dependencies you defined in pom.xml.原因是当您通过 IntelliJ 手动添加库时,只有 IntelliJ 知道它们,而当您使用 Maven 编译代码时,Maven 无法找到它,因为 Maven 只搜索您在 pom.xml 中定义的依赖项。

You should install your libraries in your (at least) local maven repository and add them as a normal dependency in your pom.xml.您应该将库安装在(至少)本地 maven 存储库中,并将它们添加为 pom.xml 中的常规依赖项。 Then you don't need to add them manually in IntelliJ.那么你不需要在 IntelliJ 中手动添加它们。

You should follow the steps mentioned at Guide to installing 3rd party JARs您应该按照安装 3rd 方 JAR 的指南中提到的步骤进行操作

Update: Also you should note that if you're working as a team, you should install this on the local maven repository of all developers (which is not practical).更新:另外你应该注意,如果你是一个团队,你应该将它安装在所有开发人员的本地 maven 存储库上(这不切实际)。 The best solution is to install a Maven repository (eg Nexus, Artifactory or Archiva) in a server on your local network and upload your private jar files on those servers.最好的解决方案是在本地网络的服务器中安装 Maven 存储库(例如 Nexus、Artifactory 或 Archiva),然后将您的私有 jar 文件上传到这些服务器上。 Then all developers can define the address of the local Maven repository server in their local Maven settings and use artifacts/libraries from that servers.然后,所有开发人员都可以在其本地 Maven 设置中定义本地 Maven 存储库服务器的地址,并使用来自该服务器的工件/库。 Plus it works as a local cache/proxy to fetch any Maven artifacts and prevents unnecessary calls to public maven repositories.此外,它可以作为本地缓存/代理来获取任何 Maven 工件,并防止对公共 Maven 存储库进行不必要的调用。

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

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