Java “无法解析导入 org.springframework。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19871437/
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-12 20:55:43  来源:igfitidea点击:

"The import org.springframework cannot be resolved."

javaeclipsemaven

提问by Vaibhav

Here is my POM.xml file:

这是我的 POM.xml 文件:

<project>
    <properties>
        <jdk.version>1.6</jdk.version>
        <spring.version>3.2.2.RELEASE</spring.version>
        <spring.batch.version>2.2.0.RELEASE</spring.batch.version>
        <mysql.driver.version>5.1.25</mysql.driver.version>
        <junit.version>4.11</junit.version>
    </properties>

    <dependencies>

        <!-- Spring Core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring jdbc, for database -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Spring XML to/back object -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- MySQL database driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.driver.version}</version>
        </dependency>

        <!-- Spring Batch dependencies -->
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>${spring.batch.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-infrastructure</artifactId>
            <version>${spring.batch.version}</version>
        </dependency>

        <!-- Spring Batch unit test -->
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <version>${spring.batch.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>spring-batch</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


And below there is my java class:


下面是我的java类:

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        String[] springConfig  =
            {
                "spring/batch/jobs/job-hello-world.xml"
            };
        ApplicationContext context =
            new ClassPathXmlApplicationContext(springConfig);
        JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
        Job job = (Job) context.getBean("helloWorldJob");
        try {
            JobExecution execution = jobLauncher.run(job, new JobParameters());
            System.out.println("Exit Status : " + execution.getStatus());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I am getting an error in import statements in my App.java classnd this is the message:

我在 App.java 类中的导入语句中遇到错误,这是消息:

"The import org.springframework cannot be resolved."

“无法解析导入 org.springframework。”

I clearly mentioned the dependencies in POM.xml, but my java class still cannot pick the dependency from there.

我清楚地提到了 POM.xml 中的依赖项,但是我的 java 类仍然无法从那里选择依赖项。

采纳答案by Matt Byrne

You need to follow a few steps to debug properly.

您需要遵循几个步骤才能正确调试。

1) mvn clean dependency:treeTake a look at the output to see exactly what you get and verify your dependencies are all there.

1)mvn clean dependency:tree查看输出以确切了解您得到的内容并验证您的依赖项是否都存在。

2) mvn clean compile. Does this fail? If not does that mean you only get the error in Eclipse?

2)mvn clean compile。这会失败吗?如果不是,那是否意味着您只会在 Eclipse 中收到错误?

You mentioned in a comment "And I run both commands above but I am getting this error". Did mvn clean compilework? Or did you get an error for that as well? If it worked then it's just an IDE problem and I'd look at the m2eclipseplugin. Better still, use IntelliJ as the free version has better maven support than Eclipse ;-)

您在评论中提到“我运行了上面的两个命令,但出现此错误”。做了mvn clean compile工作?或者你也有错误?如果它有效那么它只是一个IDE问题,我会看看m2eclipse插件。更好的是,使用 IntelliJ,因为免费版本比 Eclipse 有更好的 Maven 支持;-)

Some style things ...

一些风格的东西...

People often add too many dependencies in their pom file when they don't need to. If you take a look at a couple of links in mavenrepository.com you can see that spring-oxmand spring-jdbcboth depend on spring-coreso you don't need to add that explicitly (for example). mvn clean dependency:treewill show you what is coming in after all of that, but this is more tidying.

人们经常在不需要的时候在他们的 pom 文件中添加太多的依赖项。如果你在一对夫妇在mavenrepository.com链接看看你可以看到,spring-oxmspring-jdbc都依赖于spring-core这样你就需要添加一个明确(做示例)。mvn clean dependency:tree将向您展示所有这些之后的内容,但这更整洁。

spring-batch-testshould be testscope.

spring-batch-test应该是test范围。

回答by Vaibhav

Finally my issue got resolved. I was importing the project as "Existing project into workspace". This was completely wrong. After that I selected "Existing Maven project" and after that some few hiccups and all errors were removed. In this process I got to learn so many things in Maven which are important for a new comer in Maven project.

最后我的问题得到了解决。我将项目作为“现有项目到工作区”导入。这是完全错误的。之后,我选择了“Existing Maven project”,之后一些小问题和所有错误都被删除了。在这个过程中,我学到了很多 Maven 的东西,这些东西对于 Maven 项目的新手来说很重要。

回答by Feras

My direct solution for this issue : right click the project --> Maven ---> Add Dependency == then choose the name or parent name of missing dependency

我对此问题的直接解决方案:右键单击项目--> Maven ---> 添加依赖项== 然后选择缺少的依赖项的名称或父名称

enter image description here

在此处输入图片说明

回答by Shun

Right click project name in Eclipse, -->Maven-->Select Maven Profiles... Then tick the maven profile you want to set. After click OK, Eclipse will automatically import the maven setting to your project. If you check your project's Property, you will find Maven Dependencies Library has been added.

在Eclipse中右键项目名称,-->Maven-->Select Maven Profiles... 然后勾选你要设置的maven profile。单击确定后,Eclipse 会自动将 maven 设置导入到您的项目中。如果您检查项目的属性,您会发现已添加 Maven 依赖项库。

回答by Osama Al-Banna

This answer from here helped me:

这里的答案对我有帮助:

You should take a look at the build path of your project to check whether the referenced libraries are still there. So right-click on your project, then "Properties -> Java Build Path -> Libraries" and check whether you still have the spring library JARs in the place that is mentioned there. If not, just re-add them to your classpath within this dialog.

您应该查看项目的构建路径以检查引用的库是否仍然存在。因此,右键单击您的项目,然后“属性-> Java 构建路径-> 库”并检查您是否在那里提到的地方仍然有 spring 库 JAR。如果没有,只需在此对话框中将它们重新添加到您的类路径中。

http://forum.spring.io/forum/spring-projects/springsource-tool-suite/98653-springframework-cannot-be-resolved-error

http://forum.spring.io/forum/spring-projects/springsource-tool-suite/98653-springframework-cannot-be-resolved-error

回答by Shamik Guha Ray

Add these dependencies

添加这些依赖项

</dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
</dependencies>

回答by mummadi venkata ravi teja

org.springframework.beans.factory.support.beannamegenerator , was my error. I did a maven clean, maven build etc., which was not useful and I found that my .m2 folder is not present in my eclipse installation folder. I found the .m2 folder out side of the eclipse folder which I pasted in the eclipse folder and then in eclipse I happened to do this :-

org.springframework.beans.factory.support.beannamegenerator ,是我的错误。我做了一个 maven clean、maven build 等,这没什么用,我发现我的 .m2 文件夹不在我的 eclipse 安装文件夹中。我在 eclipse 文件夹之外找到了 .m2 文件夹,我将其粘贴到 eclipse 文件夹中,然后在 eclipse 中我碰巧这样做了:-

Open configure build path maven Java EE integration Select Maven archiver generates files under the build directory apply and close

打开配置构建路径 maven Java EE 集成 选择 Maven 归档器在构建目录下生成文件 apply 并关闭

My project is up and running now.

我的项目现已启动并运行。

回答by Moh Lamine

if you're sure that your pom.xml is pretty good, then you have just to update the poject. right click on the project - Maven - update project. or simply alt+F5.

如果您确定您的 pom.xml 非常好,那么您只需更新项目。右键单击项目 - Maven - 更新项目。或简单地 alt+F5。

回答by Deepak Sharma

In my case I had to delete the jars inside .m2/repositoryand then did a Maven->Update Maven Project

在我的情况下,我不得不删除里面的罐子.m2/repository,然后做了一个Maven->Update Maven Project

Looks like the jars were corrupt and deleting and downloading the fresh jar fixed the issue.

看起来 jar 已损坏,删除并下载新的 jar 解决了该问题。

回答by moisu

The solution that worked for me was to right click on the project --> Maven --> Update Project then click OK.

对我有用的解决方案是右键单击项目 --> Maven --> 更新项目,然后单击确定。