Gendaful
Gendaful

Reputation: 5802

Getting NoClassDefFoundError for org/springframework/context/support/ClassPathXmlApplicationContext while packaging a jar using Maven

I am sure this question was asked before also. I had already tried other options as mentioned but not successful :(

BackGround: Packaging a jar with all dependencies using maven.

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <id>assembly:package</id>
            <phase>package</phase>
            <goals>
                <!-- Work around for http://jira.codehaus.org/browse/MASSEMBLY-97 
                    as the goal should be attached. -->
                <goal>single</goal>
            </goals>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.Application</mainClass>
            </manifest>
            <manifestEntries>
                <Class-Path>config/</Class-Path>
            </manifestEntries>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
    </configuration>
</plugin> 

classPath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src/main/java" />
    <classpathentry kind="src" path="src/main/resources" />
    <classpathentry exported="true" kind="con"
        path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER" />
    <classpathentry kind="con"
        path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre7" />
</classpath>

When I am running the exported jar using

java -cp Scheduler-0.0.1-SNAPSHOT-exe.jar com.Application

I am getting exception as

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/c
ontext/support/ClassPathXmlApplicationContext
        at com.Application.main(Application.java:13)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.support
.ClassPathXmlApplicationContext
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)

Any ideas on this? I am using spring version 3.0.6 for all dependant jars.

Thanks Gendaful

Upvotes: 1

Views: 8820

Answers (2)

kishore
kishore

Reputation: 1

Try adding this plugin in your pom.xml plugins.

      <!-- Adding depenencies to jar file -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.1</version>
        <configuration>
         <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.handlers</resource>
                </transformer>
                <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                  <resource>META-INF/spring.schemas</resource>
                </transformer>
              </transformers>
            </configuration>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

Upvotes: 0

Vikdor
Vikdor

Reputation: 24134

The classpath you mentioned is that of eclipse project. It is not considered by the maven build tool while preparing the JAR using build target. You should specify the spring jars as dependencies in your pom.xml as follows, for them to be considered by Maven during packaging the application.

<dependencies>
    ...

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>3.1.1.RELEASE</version>
    </dependency>

    ...
</dependencies>

Upvotes: 1

Related Questions