Adnan
Adnan

Reputation: 110

java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext maven

I am new to spring and maven. I have a simple hello world project using Spring . The project builds successfully but i face the following error at runtime:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext

This is the main app: App.java

package com.test.SpringMaven2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;



/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        System.out.println( "Hello World!" );
        ApplicationContext context = new AnnotationConfigApplicationContext(HelloWorldConfig.class);

        HelloWorld hello = (HelloWorld) context.getBean("helloWorld"); 
        hello.setName("Adnan");
        hello.getName();
    }
}

This is the HelloWorld bean

package com.test.SpringMaven2;

public class HelloWorld{


    private String name; 

    public void setName(String name){
        this.name = name;
    }

    public void getName(){
        System.out.println("Hello, " + name);
    }
}

This is the annotation based configuration file:

package com.test.SpringMaven2;


import org.springframework.context.annotation.*;

@Configuration
public class HelloWorldConfig{

    @Bean
    public HelloWorld helloWorld(){
        return new HelloWorld(); 
    }

}

This is my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.test</groupId>
  <artifactId>SpringMaven2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringMaven2</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>4.3.3.RELEASE</version>
</dependency>
  </dependencies>
</project>

I am running the following command in CMD to run the application: java -cp {nameofresultjarfile} com.test.SpringMaven2.App

But getting the error messsage above

Any advice?

Thanks

Upvotes: 7

Views: 52073

Answers (5)

sumit singh
sumit singh

Reputation: 57

I was facing the same issue. I am using the intellij 2021 but the above solution didn't work. Also in eclipse it was working fine . The solution that worked for me was: 1)Go to edit run/debug configuraiton 2)Modify option 3)Check "include dependency with provided scope" enter image description here

configure setting

Upvotes: 0

Shtefan
Shtefan

Reputation: 808

The below fixed similar issue for me.

build plugins section should be put right after /dependencies:

<build>
  <plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.5.4</version>
            <configuration>
                <layout>ZIP</layout>
            </configuration>
            <executions>
                <execution>
                  <goals>
                    <goal>repackage</goal>
                  </goals>
                </execution>
            </executions>
    </plugin>
  </plugins>
</build>

Upvotes: 0

Oleksii Kyslytsyn
Oleksii Kyslytsyn

Reputation: 2426

In my similar case both @jaysee answer or:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.example.MyMainClass </mainClass>
        </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
</plugin>

or

<plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.5.0</version>

helped.

Upvotes: 0

Abhishek Galoda
Abhishek Galoda

Reputation: 3054

I was getting the same error when running from Intellij as a main class (In Spring Boot project)

After opening Module settings, In the Deopendencies tab I saw that for spring-context for some reason the scope was provided, changing to compile/ even removing the scope fixed the issue for for me

 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>## Put your desired version here ##</version>   
    </dependency>

Upvotes: 0

jaysee
jaysee

Reputation: 466

Your pom.xml creates a jar file which contains only the classes defined in your project. But all the other dependencies (spring-core, spring-context-support) which are required by your application aren't included.

If your application is started within eclipse, eclipse integrates these required jar files to the classpath of the application and so it is able to run.
If your application is started from CMD the spring classes can't be found in the classpath and you get a java.lang.NoClassDefFoundError.

It's possible to name all the required jars manually when the application is started from CMD (classpath), but is much easier to created a so called self contained jar file, which has all of them already included. This can be done using maven-assembly-plugin.

An example how to create a self contained jar file can be found here.

The application in a self contained jar can be started now from CMD:
java -jar name_of_your_project-jar-with-dependencies.jar

Upvotes: 17

Related Questions