John R. Martinez
John R. Martinez

Reputation: 67

AWS EC2 Java Spring Boot - Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication

Hello I am trying to run Spring Boot in a AWS EC2 instance and i am getting the following error:

Command in EC2: java -jar app-dal-1.0-SNAPSHOT.jar

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
        at com.smartcommunity.smartparking.appdal.BootApp.main(BootApp.java:12)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

Everything works fine in my local machine.

Local Java version "9.0.4" - EC2 Java Version 1.8.0

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

    <groupId>com.smartcommunity.smartparking</groupId>
    <artifactId>app-dal</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>



    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.8</version>
        </dependency>

            <dependency>
                <groupId>org.mongodb</groupId>
                <artifactId>mongodb-driver</artifactId>
                <version>3.5.0</version>
            </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.1</version>
        </dependency>


        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>8.5.32</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>

                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>com.smartcommunity.smartparking.appdal.BootApp</mainClass>
                        </manifest>
                    </archive>


                </configuration>

            </plugin>
        </plugins>
    </build>

</project>

Steps I am following, using IntelliJ Maven Lifecycle:

Upvotes: 0

Views: 1104

Answers (3)

M. Deinum
M. Deinum

Reputation: 124516

When using Spring Boot it expects a special structure in your jar file. The spring-boot-maven-plugin makes sure that this structure is created in the jar.

The spring-boot-maven-plugin is specially designed and build to create executable jar files for Spring Boot based applications. So instead of your explicitly added >maven-dependency-plugin and maven-jar-plugin, replace those with a single spring-boot-maven-plugin.

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

Basically this is al you need.

Upvotes: 1

John R. Martinez
John R. Martinez

Reputation: 67

Adding spring-boot-maven-plugin in the pom.xml file as one of the configs plugin did the trick. Thanks to everyone who commented, the Solution to this problem and who came with the solution is in the comments. Thanks @M. Deinum

Upvotes: 0

Saranraj Gunabalan
Saranraj Gunabalan

Reputation: 73

Copy your output jar file from target repo to different location and execute it in your local machine. Let me know if you are facing any issues. In that case, we should modify the pom file to generated resources.

Sorry, since I am new contributor I can't comment you this in your question.

Upvotes: 0

Related Questions