4

This is my pom.xml file:

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>SpringBootGame</groupId>
    <artifactId>SpringBootGame</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>

    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.3.RELEASE</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.game.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>

    </build>

</project>

I build my jar file by "mvn clean install" and then running the jar file extracted by: "java -jar target\SpringBootGame-1.0-SNAPSHOT.jar".

Then, I get an error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication at com.game.Main.main(Main.java:15) Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more

Do you know what is the problem?

3 Answers 3

10
  1. you should remove the maven-jar-plugin

  2. modify spring-boot-maven-plugin to:

     <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <version>2.0.3.RELEASE</version>
         <configuration>
             <mainClass>com.game.Main</mainClass> 
         </configuration>
     </plugin>
    
  3. you can execute mvn package spring-boot:repackage to generate the executable jar

Sign up to request clarification or add additional context in comments.

6 Comments

In this case I get the error: "no main manifest attribute, in target\SpringBootGame-1.0-SNAPSHOT.jar"
try mvn package spring-boot:repackage
because you didn't configure the pom.xml with spring-boot-starter-parent pom, so the configuration is a little bit complicated
OK. How can I configure pom.xml with spring-boot-starter-parent ?
|
0

I also had this problem ... There are a bug on Intellij Idea 2015 and using Gradle v4+... in shortly:

Update youre Intellij Idea to 2017 or later

Comments

0

My solution was inspired by @clevertension. However, due to small differences in my plugin configuration, I decided to share if somebody would make use of it.

  1. you should remove the maven-jar-plugin
  2. modify spring-boot-maven-plugin to:

         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <archive>
                  <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathPrefix>lib/</classpathPrefix>
                    <mainClass>com.game.Main</mainClass>
                  </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    
  3. you can execute mvn package spring-boot:repackage

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.