java Spring导入错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30211201/
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
java Spring import error
提问by weaver
I've downloaded spring-framework-4.1.6.RELEASE from http://maven.springframework.org/release/org/springframework/spring/
我已经从http://maven.springframework.org/release/org/springframework/spring/下载了 spring-framework-4.1.6.RELEASE
Then I followed the Spring Website http://spring.io/guides/gs/rest-service/to start a project,but I found there is a problem in it.
然后我跟着Spring网站http://spring.io/guides/gs/rest-service/启动了一个项目,但是发现里面有问题。
//The import org.springframework.boot cannot be resolved
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
It seems like that it can't find the jar files so can't import it,but I've add all jar files to build path.
How can I solve it? Is Spring and SpringFramework the same one?
似乎找不到 jar 文件,因此无法导入它,但我已将所有 jar 文件添加到构建路径。
我该如何解决?Spring 和 SpringFramework 是同一个吗?
采纳答案by Ceiling Gecko
It looks like you are trying to access classes which are part of the Spring Boot project, it might be the case that your Spring libraries do not contain the Spring Boot project related libraries.
看起来您正在尝试访问属于 Spring Boot 项目的类,可能是您的 Spring 库不包含与 Spring Boot 项目相关的库。
Naturally I'd recommend for you to import it via Maven or Gradle as suggested by the Spring Boot Quickstart.
当然,我建议您按照Spring Boot Quickstart 的建议通过 Maven 或 Gradle 导入它。
But if you want to add it manually, the latest release jar currently for SpringApplication
is spring-boot-1.2.3.RELEASE.jar
但是如果要手动添加,目前最新发布的jarSpringApplication
是spring-boot-1.2.3.RELEASE.jar
Find it HERE
在这里找到
And the SpringBootApplication
class is found within spring-boot-autoconfigure-1.2.3.RELEASE.jar
而SpringBootApplication
类是弹簧启动自动配置,1.2.3.RELEASE.jar中发现
Find it HERE
在这里找到
So check if you have similar jars among your current ones and download them if not, also remember since you are dealing with dependencies manually, it's quite possible that these two new libraries will have dependencies of their own and you'll have to accommodate to them as well.
因此,请检查您当前的 jars 中是否有类似的 jars,如果没有,请下载它们,还请记住,由于您正在手动处理依赖项,这两个新库很可能会有自己的依赖项,您必须适应它们以及。
回答by Sajan Chandran
Spring framework zip
artifact you downloaded off the maven repo doesnt have the Spring boot
jar, thats why its not compiling.
Download the spring boot jar and add it to your classpath.
Spring framework zip
您从 Maven 存储库下载的工件没有Spring boot
jar,这就是它没有编译的原因。下载 spring boot jar 并将其添加到您的类路径中。
This link has the maven dependency defined.
此链接定义了 maven 依赖项。
回答by Dirk Schumacher
I had the problem that just anything in package org.springframework.boot.autoconfigure.* could not be resolved/compiled though proper jar was included in classpath.
我遇到的问题是,尽管类路径中包含了正确的 jar,但 org.springframework.boot.autoconfigure.* 包中的任何内容都无法解析/编译。
My solution: wipe the local maven repository
我的解决方案:擦除本地maven仓库
Running command line 'mvn clean compile' showed me a line like
运行命令行 'mvn clean compile' 向我展示了一行
[ERROR] error reading /Users/greyshine/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/1.4.3.RELEASE/spring-boot-autoconfigure-1.4.3.RELEASE.jar; invalid LOC header (bad signature)
That gave me the guess that the artefact was not properly downloaded in the first run. I wiped my maven spring boot repository artefacts (i.e located at ~/Users/MYHOME/.m2/repository/org/springframework/boot) then I rebuild with 'mvn clean compile' and it all worked.
这让我猜测在第一次运行时没有正确下载人工制品。我擦除了我的 maven spring boot 存储库人工制品(即位于 ~/Users/MYHOME/.m2/repository/org/springframework/boot),然后我用 'mvn clean compile' 重建,一切正常。
回答by tatari
I had a similar problem and fixed it by tweaking the pom.xml
我有一个类似的问题,并通过调整 pom.xml 修复它
from
从
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
to:
到:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
回答by Cathy
As an alternative to Dirk Schumacher's approach of removing the entire local maven repo, I only remove the .m2/repository/org/springframework folder to force that part to redownload.
作为 Dirk Schumacher 删除整个本地 maven 存储库的替代方法,我只删除了 .m2/repository/org/springframework 文件夹以强制该部分重新下载。