Java 从所需的 .class 文件间接引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20497813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:21:21  来源:igfitidea点击:

Indirectly referenced from required .class files

javaspringspring-tool-suite

提问by Srinivasan

I'm getting below error in STS:

我在 STS 中遇到以下错误:

The type org.springframework.core.env.EnvironmentCapable cannot be resolved. It is indirectly referenced from required .class files

类型 org.springframework.core.env.EnvironmentCapable 无法解析。它是从所需的 .class 文件间接引用的

回答by liltitus27

This sounds like a transitive dependency issue. What this means is that your code relies on a jar or library to do something - evidently, you depend on Spring framework code. Well, all that Spring code also depends on libraries and jars.

这听起来像是一个传递依赖问题。这意味着您的代码依赖于 jar 或库来做某事 - 显然,您依赖于 Spring 框架代码。好吧,所有 Spring 代码也依赖于库和 jar。

Most likely, you need to add the corerctly versioned org.springframework.corejar to your classpath so that the EnvironmentCapableclass can be found when your IDE attempts to build your project.

最有可能的是,您需要将核心版本控制的org.springframework.corejar添加到您的类路径,以便EnvironmentCapable在您的 IDE 尝试构建您的项目时可以找到该类。

This might also be a jar collision issue as well, although that sounds less likely. When an application experiences jar collision (also known as "dll hell"), the compiler is finding multiple jars and classes with the same fully-qualified name. For example, let's say you added Spring to your classpath, along with the entire Tomcat server library. Well, those two jars may contain the same exact named classes, maybe the same version, maybe different versions. But either way, when the compiler looks for that EnvironmentCapableclass, it finds two (in this contrived example) - one in the Spring jar and one in the Tomcat jar. Well, it doesn't know which one to choose, and so it throws a ClassDefNotFoundException, which would/could manifest itself as the error you experienced.

这也可能是 jar 碰撞问题,尽管这听起来不太可能。当应用程序遇到 jar 冲突(也称为“dll 地狱”)时,编译器会查找多个具有相同完全限定名称的 jar 和类。例如,假设您将 Spring 以及整个 Tomcat 服务器库添加到类路径中。好吧,这两个 jar 可能包含完全相同的命名类,可能是相同的版本,也可能是不同的版本。但是无论哪种方式,当编译器查找EnvironmentCapable该类时,它都会找到两个(在这个人为的示例中)——一个在 Spring jar 中,一个在 Tomcat jar 中。好吧,它不知道该选择哪个,因此它会抛出一个ClassDefNotFoundException,这将/可能表现为您遇到的错误。

回答by Anbu Prakash

I faced same error while i work with spring security on spring-security-config.i jsut deleted that jar in maven repo and gave maven->update Project in eclipse. it is resolved.Please try it once.

当我在 spring-security-config.i 上使用 spring security 时,我遇到了同样的错误,我 jsut 在 maven repo 中删除了那个 jar 并在 eclipse 中给了 maven->update 项目。已解决。请尝试一次。

回答by sinanduman

From command line, run "mvn clean install", you'll see project failed and you'll see artifacts in the logs that cause such a problem. After that, remove artifacts from .m2/repository, then maven update from eclipse.

从命令行运行“mvn clean install”,您将看到项目失败,并且您将在日志中看到导致此类问题的工件。之后,从 .m2/repository 中删除工件,然后从 eclipse 中更新 maven。

回答by Dezso Gabos

To avoid jar collision, make sure you declare your dependency versions under the properties tag in the aggregate pom.xml, and use the property name as a placeholder throughout the project. For example 4.2.5.RELEASE in the parent pom, and then in the child modules just use ${spring.version} instead of 4.2.5.RELEASE. This way you can avoid having two different versions of the same library on the classpath.

为避免 jar 冲突,请确保在聚合 pom.xml 中的属性标记下声明您的依赖项版本,并在整个项目中使用属性名称作为占位符。例如父 pom 中的 4.2.5.RELEASE,然后在子模块中使用 ${spring.version} 而不是 4.2.5.RELEASE。这样你就可以避免在类路径上有两个不同版本的同一个库。

Also it is recommended to be consistent with the version of spring dependencies. Use the same version for spring-core, spring-web etc.

另外建议与spring依赖的版本保持一致。对 spring-core、spring-web 等使用相同的版本。

If you are using maven, then you can use the maven enforcer plugin to ensure dependency convergence, and avoid further issues with transitive dependencies.

如果您使用的是 maven,那么您可以使用 maven 执行器插件来确保依赖收敛,并避免传递依赖的进一步问题。