无法使用Spring + Maven退出代码1执行Java [英] Could not exec java with Spring+Maven exit code 1
问题描述
我是Spring/Maven的新手,并且正在学习本教程: 使用Spring MVC服务Web内容.
I am new to Spring/Maven, and am following this tutorial: Serving Web Content with Spring MVC.
每次我运行mvn spring-boot:run
时,都会出现此错误:
Everytime I run mvn spring-boot:run
, I get this error:
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个小时以上-没有用.
I tried to add classpath, tried to run mvn install clean spring-boot:run
, did a lot of other things that people suggested on stackoverflow in similar situations, spent on this more than 8 hours - no use.
这是我的主要课程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
课:
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模板:
Here is my HTML template:
<!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
推荐答案
我进行了以下更改以使mvn clean spring-boot:run
工作:
I made the following changes to make mvn clean spring-boot:run
work:
- 将
pom.xml
移至根目录,这将使目录层次结构为:
- Move
pom.xml
to the root directory, which makes the directory hierarchy to be:
目录层次结构:
.
├── pom.xml
└── src
└── main
├── java
│ └── hello
│ ├── Application.java
│ └── GreetingController.java
└── resources
└── templates
└── greeting.html
- 在以下部分中注释了
extensions
: - Commented out the
extensions
in the following part:
注释部分:
<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
只会成功退出,但是我认为这是正确的行为,因为没有容器可以部署应用程序.无论如何,您可以尝试一下并根据需要进行更改.
It seems you meant to exclude those dependencies. mvn clean spring-boot:run
will just exit successfully if the embed tomcat is excluded, but I think this is the correct behave because there's no container to deploy the application. Anyway, you can try it out and make changes according your requirements.
这篇关于无法使用Spring + Maven退出代码1执行Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!