无法使用 Spring+Maven 退出代码 1 执行 java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43157515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not exec java with Spring+Maven exit code 1
提问by NiHao92
I am new to Spring/Maven, and am following this tutorial: Serving Web Content with Spring MVC.
我是 Spring/Maven 的新手,正在学习本教程: 使用 Spring MVC 服务 Web 内容。
Everytime I run mvn spring-boot:run
, I get this error:
每次运行时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 ->
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 ->
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.
我尝试添加类路径,尝试运行mvn install clean spring-boot:run
,做了很多其他人在类似情况下在 stackoverflow 上建议的事情,在这上面花费了 8 个多小时 - 没有用。
Here is my main class Application.java
:
这是我的主要课程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);
}
}
Here is my GreeetingController.java
class:
这是我的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";
}
}
Here is my pom.xml
file:
这是我的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>
Here is my HTML template:
这是我的 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>
The structure of a project is
一个项目的结构是
src/main/java/hello/pom.xml
src/main/java/hello/Application.java
src/main/java/hello/GreetingController.java
src/main/resources/templates/greeting.html
采纳答案by shizhz
I made the following changes to make mvn clean spring-boot:run
work:
我进行了以下更改以使其mvn clean spring-boot:run
工作:
- Move
pom.xml
to the root directory, which makes the directory hierarchy to be:
- 移动
pom.xml
到根目录,这使得目录层次结构为:
Directory hierarchy:
目录层次结构:
.
├── pom.xml
└── src
└── main
├── java
│?? └── hello
│?? ├── Application.java
│?? └── GreetingController.java
└── resources
└── templates
└── greeting.html
- Commented out the
extensions
in the following part:
- 注释掉
extensions
以下部分:
Commented out 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>
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.
您似乎打算排除这些依赖项。mvn clean spring-boot:run
如果嵌入的 tomcat 被排除,它将成功退出,但我认为这是正确的行为,因为没有容器来部署应用程序。无论如何,您可以尝试一下并根据您的要求进行更改。
回答by Komal
Sometimes the port might just be already in use, make sure you kill all java processes before running an application.
有时端口可能已经在使用中,请确保在运行应用程序之前终止所有 Java 进程。
回答by Zhong Linzhou
I've same error as yours and finally I found it was cased by the wrong cooperation version between "spring-boot-starter-parent" and "spring-boot-autoconfigure", thereof making sure with same version. And also check their versions are the newest nor not.
我和你的错误一样,最后我发现它是由“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>
回答by poomcyber
In my case I just delete "devtools" dependency.
就我而言,我只是删除了“devtools”依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
And it works!
它有效!
回答by weilin wang
In my case I just delete "tomcat-embed-el" dependency.
就我而言,我只是删除了“tomcat-embed-el”依赖项。
<exclusion>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-el</artifactId>
</exclusion>
And it works.....
它有效......