47

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
  • And you're sure that your .m2 directory has the appropriate JAR? Did you run mvn clean compile or mvn clean dependency:resolve? What did that give you?
    – Makoto
    Commented Nov 9, 2013 at 1:47
  • Are you running this in an IDE or the command line? Commented Nov 9, 2013 at 1:48
  • 1
    I am facing this on Eclipse IDE. And I run both commands above but still I am getting this error.
    – Vaibhav
    Commented Nov 9, 2013 at 1:55
  • 1
    I just checked in .m2 directory and jars for or.springframework.batch are there
    – Vaibhav
    Commented Nov 9, 2013 at 1:57
  • Try creating a build and running it, and if it works, you have an issue with Eclipse plugin for Maven. If it doesn't, look what libs are packaged in the build.
    – alterfox
    Commented Nov 9, 2013 at 2:02

24 Answers 24

52

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
  • When I run mvn clean and mvn clean dependency:tree commands, both of them were success. About m2eclipse plugin, I installed it two days back and it was working fine.
    – Vaibhav
    Commented Nov 9, 2013 at 2:36
  • 3
    Try mvn eclipse:eclipse or mvn m2eclipse:m2eclipse. Can't remember the exact syntaxes because I use IntelliJ. After doing these, refresh from the root of Eclipse and it should sort itself out from memory.
    – Matt Byrne
    Commented Nov 9, 2013 at 3:36
  • I can see this message for help in eclipse. Can anyone helps me how to open artifact search dialog box. "Opens the artifact search dialog to search for 'org.springframework.batch.core' to add to your target. "
    – Vaibhav
    Commented Nov 11, 2013 at 23:28
  • You're on your own now with Eclipse. The reason someone downvoted your question by the way was because it was not clearly marked that it was an IDE problem. Maven itself is resolving and your project is building so it's just Eclipse tweaking you need.
    – Matt Byrne
    Commented Nov 12, 2013 at 0:22
  • 1
    Like I said mate, I use IntelliJ so can't help any further. My opinion ... install and use free version IntelliJ. No stuffing around with plugins - plays nicely with Maven, but I'm sure there are people out there who can help with Eclipse. If you post another question that is well-worded and clearly states you need Eclipse help you may get more luck.
    – Matt Byrne
    Commented Nov 12, 2013 at 5:07
30

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
23

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

11

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
7

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
  • thanks, this was my issue as well. I had created a maven project using a webapp archetype at the cli, then imported into intelliJ. Looks like intelliJ was looking for the dependencies, so I did what you did to resolve the issue: right click on the project > add framework support > select "maven".
    – verboze
    Commented Oct 22, 2015 at 21:33
  • In my case the missing JAR is already included in the library as part of project dependency hierarchy but during Maven compile it is not found. So adding it explicitly does resolve the issue but I am not sure why this behavior. Have to dig in. Commented Jul 31, 2018 at 8:47
4

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.

4

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.

3

Add the following JPA dependency.

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

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.

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

2

I fixed mine by adding:

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

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.

1

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.

0

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]

0

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

0

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
0

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

1
0

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.

0

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

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

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.

0

In my case, I am using the DiUS/java-faker dependency and it internally references old snake-yaml lib which is not available in the maven repo. I have resolved this by adding the below snippet in the gradle.build.kts file.

implementation("com.github.javafaker:javafaker:1.0.2"){ exclude ( "org.yaml") }
implementation (group= "org.yaml", name= "snakeyaml", version= "2.0")
0

In my case, I had my spring-context dependency scope as runtime in pom.xml. So the compiler doesn't bring the spring classes. I had to remove the runtime scope in pom, then it worked.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jul 3 at 13:23
0

I fixed this error in VS Code by adding these under dependencies in pom.xml

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

1
-1

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

Enter image description here

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.