"The import org.springframework cannot be resolved."

Here is my POM.xml file:

<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:

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 class, and this is the message:

"The import org.springframework cannot be resolved."

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

11

21 Answers

You need to follow a few steps to debug properly.

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

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

You mentioned in a comment "And I run both commands above but I am getting this error". Did mvn clean compile work? 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 m2eclipse plugin. Better still, use IntelliJ as the free version has better maven support than Eclipse ;-)

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-oxm and spring-jdbc both depend on spring-core so you don't need to add that explicitly (for example). mvn clean dependency:tree will show you what is coming in after all of that, but this is more tidying.

spring-batch-test should be test scope.

7

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 newcomer in a Maven project.

0

The solution that worked for me was to right-click on the project → MavenUpdate Project and then click OK.

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>
0

My direct solution for this issue:

Right-click the project → MavenAdd Dependency. Then choose the name or parent name of missing dependency

Enter image description here

2

In my case, I had to delete the JAR files inside the .m2/repository and then did a Maven* → Update Maven Project.

It looks like the JAR files were corrupt and deleting and downloading the fresh JAR file fixed the issue.

Right-click project name in Eclipse, → Maven → select Maven Profiles...

Then tick the Maven profile you want to set. After clicking OK, Eclipse will automatically import the Maven setting to your project. If you check your project's Property, you will find the Maven Dependencies Library has been added.

Add the following JPA dependency.

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

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.

For me, this problem occurred when I forgot to add a Spring web dependency. I checked it from Eclipse's autocomplete that there aren't any org.springframework.web available for my project.

Then from menu ProjectSpringAdd starters, I added web dependency to the pom.xml file.

If you're sure that your pom.xml file is pretty good, then you have just to update the project. Right-click on the project → Maven → *Update project. Or simply Alt + F5.

I imported a project as 'Existing Maven Project' and was getting this issue.

Resolution: Changed Java Build Path of JRE System Library to Workspace defailt JRE [jdk 1.8]

Steps:

Right click on project -> build path -> configure build path -> Libraries Tab -> double click JRE System Library -> change to Workspace defailt JRE [jdk 1.8]

There are few steps you can follow

  1. remove repository folder

    C:/Users/user_name/.m2

  2. Then run command using IDE terminal or open cmd in your project folder

    mvn clean install

  3. Restart your ide

  4. If not solve your problem then run this command

    mvn idea:idea

I fixed mine by adding:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>
</dependency>

I had the same problem in Eclipse STS.

Changing the scope in the pom from "provided" to "compile" fixed the problem and when I changed it back everything was still OK.

1

Right-click the project, then Maven, and in the textbox, write "pom.xml".

1

When I imported the project in IntelliJ, I got this issue. But it got resolved when I created the Project in IntelliJ → menu FileNewProjectSpring Initializr. I added dependencies in that window itself.

I fixed it for me by adding the below dependency.

<dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-xml</artifactId> <version>4.0.4</version>
</dependency>
<dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> <version>4.0.4</version>
</dependency>

The only solution that worked for me was to add the maven-compiler-plugin to the pom.xml file:

<project ...> ... <build> ... <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> <fork>true</fork> <executable>C:\Program Files\Java\jdk1.7.0_79\bin\javac</executable> </configuration> </plugin> </plugins> </build> ...
</project>

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 outside of the Eclipse folder which I pasted in the Eclipse folder and then in eclipse I happened to do this:

  • Open configure build path
  • maven
  • Java EE integration
  • Select Maven archiver. It generates files under the build directory
  • Apply and close

My project is up and running now.

In my case, this issue was resolved by updating Maven's dependencies:

Enter image description here

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like