[英]Could not exec java with Spring+Maven exit code 1
我是 Spring/Maven 的新手,正在學習本教程: Serving Web Content with Spring MVC 。
每次運行mvn spring-boot:run
時,我都會收到此錯誤:
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.2.RELEASE:run (default-cli) on project gs-serving-web-content: Could not exec java: Application finished with exit code: 1 ->
我嘗試添加類路徑,嘗試運行mvn install clean spring-boot:run
,在類似情況下做了很多人們在 stackoverflow 上建議的其他事情,花了 8 個多小時 - 沒有用。
這是我的主要 class Application.java
:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception{
SpringApplication.run(Application.class, args);
}
}
這是我的GreeetingController.java
class:
package hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GreetingController {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";
}
}
這是我的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>org.springframework</groupId>
<artifactId>gs-serving-web-content</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Exclusions to allow SpringBoot execute on HCP -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</exclusion>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
<!-- The main class to start by executing java -jar -->
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>hello.Application</mainClass>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
這是我的 HTML 模板:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
一個項目的結構是
src/main/java/hello/pom.xml
src/main/java/hello/Application.java
src/main/java/hello/GreetingController.java
src/main/resources/templates/greeting.html
有時端口可能已經在使用中,請確保在運行應用程序之前終止所有 Java 進程。
我進行了以下更改以使mvn clean spring-boot:run
工作:
pom.xml
移動到根目錄,這樣目錄層次結構為:目錄層次結構:
.
├── pom.xml
└── src
└── main
├── java
│ └── hello
│ ├── Application.java
│ └── GreetingController.java
└── resources
└── templates
└── greeting.html
exclusions
:注釋掉的部分:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Exclusions to allow SpringBoot execute on HCP -->
<!--<exclusions>-->
<!--<exclusion>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
<!--</exclusion>-->
<!--<exclusion>-->
<!--<groupId>org.apache.tomcat.embed</groupId>-->
<!--<artifactId>tomcat-embed-el</artifactId>-->
<!--</exclusion>-->
<!--<exclusion>-->
<!--<artifactId>logback-classic</artifactId>-->
<!--<groupId>ch.qos.logback</groupId>-->
<!--</exclusion>-->
<!--</exclusions>-->
</dependency>
您似乎打算排除這些依賴項。 如果嵌入的 tomcat 被排除, mvn clean spring-boot:run
將成功退出,但我認為這是正確的行為,因為沒有容器來部署應用程序。 無論如何,您可以嘗試一下並根據您的要求進行更改。
我和你的錯誤一樣,最后我發現它是由“spring-boot-starter-parent”和“spring-boot-autoconfigure”之間的錯誤合作版本造成的,確保使用相同的版本。 還要檢查他們的版本是否是最新的。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
就我而言,我只是刪除了“devtools”依賴項。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
它有效!
就我而言,我只是刪除了“tomcat-embed-el”依賴項。
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</exclusion>
它有效......
確保 Maven 運行正確的 Java JDK。 對於 MacOS,運行/usr/libexec/java_home
。
這將在/Library/Java/JavaVirtualMachines/
中搜索您已安裝的 JDK。 很可能即使$JAVA_HOME
設置為預期的 JDK,子進程也可能在其他 JDK 上運行。 如果您沒有運行預期的 JDK,則可能必須將/Library/Java/JavaVirtualMachines/
之外的其他 JDK 放在一邊。 不幸的是,我還沒有找到更好的方法來做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.