Java 如何用spring boot解决找不到或加载主类错误?
我为Spring Boot创建了一个Maven项目。我有很多Spring依赖项和一个主类:Java 如何用spring boot解决找不到或加载主类错误?,java,maven,spring-boot,Java,Maven,Spring Boot,我为Spring Boot创建了一个Maven项目。我有很多Spring依赖项和一个主类: package com.vastserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyArtifactApplication
package com.vastserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyArtifactApplication {
public static void main(String[] args) {
// SpringApplication.run(MyArtifactApplication.class, args);
System.out.println("hello!");
}
}
src
目录的文件夹结构为:
.
└── main
├── java
│ └── com
│ └── vastserver
│ └── MyArtifactApplication.java
└── resources
└── application.properties
在我的pom.xml中,我使用maven汇编插件
在一个独立的.jar文件中构建我的项目。即使我三次检查目录结构和主类文件是否正确显示在pom.xml中,我还是不断得到错误:error:当我运行mvn package
然后运行java-cp时,无法找到或加载主类com.vastserver.MyArtifactApplication
target/vast-ad-server-artifactId-1.0-SNAPSHOT-jar-with-dependencies.jar com.vastserver.MyArtifactApplication
或mvn exec:exec
。如果我从Intellij运行主类,它确实可以工作,因此我知道代码不是问题所在,而是Maven构建设置。我迷失了我的问题所在
我的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<maven.compiler.release>11</maven.compiler.release>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<mainClass>com.vastserver.MyArtifactApplication</mainClass>
<descriptorRef>jar-with-dependencies</descriptorRef>
<targetSnapshot>target/vast-ad-server-artifactId-1.0-SNAPSHOT</targetSnapshot>
<targetWithDependencies>${targetSnapshot}-${descriptorRef}.jar</targetWithDependencies>
</properties>
<groupId>com.vastserver</groupId>
<artifactId>vast-ad-server-artifactId</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>${descriptorRef}</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-cp</argument>
<argument>${targetWithDependencies}</argument>
<argument>${mainClass}</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
4.0.0
org.springframework.boot
spring启动程序父级
2.1.6.1发布
11
11
11
com.vastserver.MyArtifactApplication
带有依赖项的jar
目标/vast-ad-server-artifactId-1.0-SNAPSHOT
${targetSnapshot}-${descriptorRef}.jar
com.vastserver
广域广告服务器工件
罐子
1.0-快照
org.springframework.boot
springbootstarteractivemq
org.springframework.boot
弹簧靴启动器jdbc
org.springframework.boot
弹簧启动器webflux
org.springframework.boot
弹簧靴开发工具
运行时
真的
org.postgresql
postgresql
运行时
org.springframework.boot
springbootmaven插件
org.apache.maven.plugins
maven汇编插件
包裹
单一的
${mainClass}
${descriptorRef}
org.codehaus.mojo
execmaven插件
1.3.2
执行官
JAVA
-cp
${targetWithDependencies}
${mainClass}
我意识到spring boot maven插件实际上是构建的,因此不需要其他插件。如果maven中的插件部分被编辑为:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
org.springframework.boot
springbootmaven插件
然后运行mvn包
和java-jar-target/vast-ad-server-artifactId-1.0-SNAPSHOT.jar
就可以了 我认为您应该检查maven构建的工件的目录结构。
通常,spring引导工件是由一个特殊的spring引导插件准备的,而不是由maven组装插件准备的
虽然它共享“jar”后缀,但实际上它不是一个jar,它有特殊的类加载器从BOOT-INF/lib
文件夹加载类
我已经提供了一个详细的答案,说明了当spring启动应用程序启动时会发生什么,但如果您使用汇编插件,那么您必须准备清单文件和相当复杂的文件夹结构。坦白地说,我认为您应该首先使用spring boot插件来构建spring boot应用程序。对我来说:Maven-Update项目成功了。Maven-->Update项目
它适用于我添加到上述所有答案中
右键单击project->Maven->updateproject->force update
这将下载所有缺少的依赖项
如果它不起作用呢??右键单击pom.xml
并以配置方式运行。检查您是否使用了正确的JRE。然后将目标设置为安装并运行它
重要信息检查pom.xml中的java版本,如果它与您安装的JRE版本不匹配,那么您也可能会遇到此错误。所以要确保它是一样的。
还是不行从工作区删除项目并将其导入回右键单击项目->Maven->更新项目->勾选复选框强制更新快照/发布确定
给时间更新项目。然后简单地运行应用程序Rightclickonproject->Runas->Spring Boot App尝试在maven中更新项目。有时,在添加新的依赖项时,它希望更新maven,而开发工具将无法工作
---右键单击项目
---去maven
---更新项目