繁体   English   中英

无法使用 Spring+Maven 退出代码 1 执行 java

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM