64

I'm trying to create a spring mvc application. This is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.acme</groupId>
    <artifactId>test</artifactId>
    <name>springmvc</name>
    <packaging>jar</packaging>
    <version>1.0.0-BUILD-SNAPSHOT</version>
    <properties>
        <org.springframework-version>3.1.1.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>

    <repositories>
        <repository>
            <id>spring-milestone</id>
            <url>http://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestone</id>
            <url>http://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>

        <!-- Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>0.5.0.M6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
            <version>0.5.0.M6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>0.5.0.M6</version>
        </dependency>


    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <!-- com.ilppa.cocktail.Application -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.ilppa.cocktail.Application</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

when I run mvn clean install the .jar is generated but when I launch it java -jar target/test-1.0.0-BUILD-SNAPSHOT.jar

I have an error

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
    at com.acme.test.Application.main(Application.java:12)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

I don't understand, I have no errors in eclipse, I think the .jar is in maven depences

sumit
  • 3,210
  • 1
  • 19
  • 37
Ajouve
  • 9,735
  • 26
  • 90
  • 137

18 Answers18

42

If you would like to stay in spring boot space just set the pom packaging to jar

<packaging>jar</packaging>

and add the spring-boot-maven-plugin to you build properties in the pom.xml file:

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

then a simple mvn package command will create a complete executable jar file.

See the very good spring reference doc for more details (doing it gradle style also) spring reference doc

stoffer
  • 2,317
  • 2
  • 18
  • 24
27

Your jar does not contain the dependencies such as spring-boot which it is looking for during runtime. You can build a consolidated jar with all the dependencies as outlined in this question.

Community
  • 1
  • 1
Saket
  • 3,079
  • 3
  • 29
  • 48
  • It worked. But, then without dependency in command line when I do "gradle clean build", it successfully create JAR, but not working from STS. How it is possible? – Satish Patro Mar 04 '20 at 04:46
19

If you are running spring cloud application, make sure your boot version matches cloud version. You can check the version compatibility matrix at the bottom of this page : https://spring.io/projects/spring-cloud.

snowfox
  • 1,978
  • 1
  • 21
  • 21
  • 3
    Thank you snowfox, this worked for me. My spring-boot version + spring-cloud version did not get along – Manu Feb 25 '21 at 00:09
  • 2
    This was it 100% for me. Good call. – fiidim Jun 04 '21 at 19:13
  • You have right. Spring cloud version must be compatibile with spring boot version : Spring Cloud Release -> Spring Boot Compatibility Version 2021.0.x aka Jubilee -> 2.6.x 2020.0.x aka Ilford -> 2.4.x, 2.5.x (Starting with 2020.0.3) Hoxton -> 2.2.x, 2.3.x (Starting with SR5) Greenwich -> 2.1.x Finchley -> 2.0.x Edgware -> 1.5.x Dalston -> 1.5.x – Tomasz Mar 28 '22 at 09:27
12

Mine was caused by a corrupt Maven repository.

I deleted everything under C:\Users\<me>\.m2\repository.

Then did an Eclipse Maven Update, and it worked first time.

So it was simply spring-boot.jar got corrupted.

David Lavender
  • 8,021
  • 3
  • 35
  • 55
8

The answer to the above question is "none of the above". When you download new STS it won't support the old Spring Boot parent version. Just update parent version with latest comes with STS it will work.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

If you have problem getting the latest, just create a new Spring Starter Project. Go to File->New->Spring Start Project and create a demo project you will get the latest parent version, change your version with that all will work. I do this every time I change STS.

kryger
  • 12,906
  • 8
  • 44
  • 65
6

Clean your maven cache and rerun:

mvn dependency:purge-local-repository
clemens
  • 16,716
  • 11
  • 50
  • 65
3

Here the packaging is jar type, hence you need to use manifest plugin, in order to add dependencies into the Manifest.mf

The problem here is that maven could find the dependencies in pom file and compile the source code and create the output jar. But when executing the jar, manifest.mf file contains no details of dependencies. Hence you got this error. This is a case of classpath errors.

Here you can find the details on how to do it.

Himanshu Bhardwaj
  • 4,038
  • 3
  • 17
  • 36
1

Another option is to use the Apache Maven Shade Plugin: This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.

add this to your build plugins section

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-shade-plugin</artifactId>
</plugin>
Francois
  • 1,851
  • 1
  • 13
  • 15
1

May be your dependencies not build correctly. Check compilation issue in project.

Clean and rebuild project.

For maven project: mvn clean install

For gradle projects: gradle clean build or gradlew clean build

ankit
  • 2,591
  • 2
  • 29
  • 54
1

Adding below to pom.xml solved my problem

<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">
Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
jyotinadda
  • 51
  • 3
0

I had the same problem and tried most of the solutions suggested above, but none worked for me. Eventually, I rebuild my entire com.springframework (maven) repository (by simply deleting .m2/org/springworkframework directory).

It worked for me.

user1693207
  • 119
  • 2
  • 11
0

In my case, I did remove maven nature manually from .project file while having the project opened in Eclipse. So what I'd to do was to add maven nature again using the contextual menu (roght click on the project > configuration > add maven nature). Afterwards, everything worked nice :D

joninx
  • 1,775
  • 6
  • 31
  • 59
0

Clearing metadata helped me or change the workspace.

0

You need to also copy the required dependencies next to your resulting jar and include them in the classpath. An alternative is to use maven-shade-plugin.

Snippet for the first option:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-dependency-plugin</artifactId>
   <version>3.3.0</version>
   <executions>
      <execution>
         <phase>initialize</phase>
         <goals>
            <goal>copy-dependencies</goal>
         </goals>
         <configuration>
            <overWriteReleases>false</overWriteReleases>
            <includeScope>runtime</includeScope>
            <outputDirectory>${project.build.directory}/lib</outputDirectory>
         </configuration>
      </execution>
   </executions>
</plugin>

....

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <version>3.2.2</version>
   <configuration>
      <archive>
         <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>your.class</mainClass>
         </manifest>
      </archive>
   </configuration>
</plugin>
Adrian B.
  • 1,592
  • 1
  • 20
  • 38
0

In my case, I followed these steps

  1. Clean your .m2 folder in your c:/users/your-host-name/.m2

  2. Ensure your pom.xml has this for the buile

    org.springframework.boot spring-boot-maven-plugin 3.0.1
  3. Update your maven, in IntelliJ, this is done by clicking [![enter image description here][1]][1]

  4. Finally, build your jar by using the Maven package button

0

I got this error when using Docker together with Spring boot.

The solution turned out to be to edit Dockerfile

Dockerfile used to look like this:
FROM openjdk:11
MAINTAINER Sergey Zavyalov
COPY /target/classes/ /app
WORKDIR /app
EXPOSE 3000
CMD java project.Application

I changed it like this:
FROM openjdk:11
MAINTAINER Sergey Zavyalov
COPY out/artifacts/KameleoonTestTask_jar/KameleoonTestTask.jar /app/KameleoonTestTask.jar
WORKDIR /app
EXPOSE 3000
ENTRYPOINT ["java", "-jar", "/app/KameleoonTestTask.jar"]

Сергей
  • 111
  • 4
0

I was getting the same error and found that one of the new maven dependency which I have added in the project have provided scope and It was not able to find specific classes of that dependency from JDK or a container.

This was fixed by changing the scope of dependency to default compile type from provided

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.0</version>
    <scope>provided</scope>
</dependency>

To

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.0</version>
</dependency>

https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

Hardik Patel
  • 1,033
  • 1
  • 12
  • 16
0

Class Not found is a Runtime exception when it tried to enter the dependency tree for class, and the class is not found in the child dependency of that jar module.

neuron
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 16 '23 at 02:23