通常我使用 Intellij,但今天我尝试通过命令行运行我的代码。这段代码在 IDE 中运行得很好,但是当我在命令行上运行它时却不能。我正在 pom.xml 目录上运行“mvn spring-boot:run”命令。最让我恼火的是它曾经奏效过一次。一切都一样,但后来我玩了一些 Spring Boot 版本等,当我反转时,它不再工作了。 Intellij 如何在没有 spring-boot 的情况下运行这些代码?我还有其他项目的 pom.xml 上没有 spring-boot,它们在 intellij 上完美运行,但不能使用“mvn spring-boot:run”命令。 Intellij 使用的命令是什么?
我的文件夹结构是:
HelloWorld/src/main/java/Example.java HelloWorld/pom.xml
我的java版本: java 版本“11.0.12”2021-07-20 LTS
这是示例.java
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example{
@RequestMapping("/sayHello")
String sayHello(){
return "Hello World";
}
public static void main(String[] args){
SpringApplication.run(Example.class,args);
}
}
<?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.example</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
错误
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:myproject >------------------------
[INFO] Building myproject 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> spring-boot:1.5.7.RELEASE:run (default-cli) > test-compile @ myproject >>>
[INFO]
[INFO] --- resources:2.6:resources (default-resources) @ myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\tahir\Desktop\HelloWorld\src\main\resources
[INFO] skip non existing resourceDirectory C:\Users\tahir\Desktop\HelloWorld\src\main\resources
[INFO]
[INFO] --- compiler:3.1:compile (default-compile) @ myproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- resources:2.6:testResources (default-testResources) @ myproject ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\tahir\Desktop\HelloWorld\src\test\resources
[INFO]
[INFO] --- compiler:3.1:testCompile (default-testCompile) @ myproject ---
[INFO] No sources to compile
[INFO]
[INFO] <<< spring-boot:1.5.7.RELEASE:run (default-cli) < test-compile @ myproject <<<
[INFO]
[INFO]
[INFO] --- spring-boot:1.5.7.RELEASE:run (default-cli) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.093 s
[INFO] Finished at: 2024-04-13T20:03:57+03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.7.RELEASE:run (default-cli) on project myproject: Unable to find a suitable main class, please add a 'mainClass' property -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
当我编写错误代码时,我尝试了几乎可以在网上找到的所有内容。
请看一下答案我如何告诉Spring Boot哪个主类用于可执行jar?
您还可以尝试检查调试器/运行窗口日志上的第一行,以识别 IDE 调用来启动应用程序的命令。该行的主要部分是 IDE 添加到运行时的库列表(为了舒适的启动和 IDE 目的),但在该行的末尾,您会看到启动命令本身并与您的启动命令进行比较。可能会有帮助。