gpt4 book ai didi

java - AWS EC2 Java Spring Boot - 线程 "main"java.lang.NoClassDefFoundError : org/springframework/boot/SpringApplication 中出现异常

转载 作者:行者123 更新时间:2023-12-02 10:09:36 25 4
gpt4 key购买 nike

您好,我正在尝试在 AWS EC2 实例中运行 Spring Boot,但收到以下错误:

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

在我的本地计算机上一切正常。

本地 Java 版本“9.0.4”- EC2 Java 版本 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>

我使用 IntelliJ Maven Lifecycle 遵循的步骤:

  • Maven 清洁
  • Maven 编译
  • Maven 包
  • Maven 安装

最佳答案

当使用 Spring Boot 时,它需要您的 jar 文件中有一个特殊的结构。 spring-boot-maven-plugin 确保在 jar 中创建此结构。

spring-boot-maven-plugin专门设计和构建为基于 Spring Boot 的应用程序创建可执行 jar 文件。因此,而不是您明确添加的 >maven-dependency-pluginmaven-jar-plugin ,将其替换为单个 spring-boot-maven-plugin

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

基本上这就是您所需要的。

关于java - AWS EC2 Java Spring Boot - 线程 "main"java.lang.NoClassDefFoundError : org/springframework/boot/SpringApplication 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55095576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com