'Could not exec java with Spring+Maven exit code 1

I am new to Spring/Maven, and am following this tutorial: Serving Web Content with Spring MVC.

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 ->

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.

Here is my main 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);
    }
}

Here is my 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";
    }

}

Here is my pom.xml file:

<?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:

<!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


Solution 1:[1]

I made the following changes to make mvn clean spring-boot:run work:

  • Move pom.xml to the root directory, which makes the directory hierarchy to be:

Directory hierarchy:

.
??? pom.xml
??? src
    ??? main
        ??? java
        ?   ??? hello
        ?       ??? Application.java
        ?       ??? GreetingController.java
        ??? resources
            ??? templates
                ??? greeting.html
  • Commented out the exclusions in the following part:

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.

Solution 2:[2]

Sometimes the port might just be already in use, make sure you kill all java processes before running an application.

Solution 3:[3]

In my case I just delete "devtools" dependency.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>

And it works!

Solution 4:[4]

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.

<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>

Solution 5:[5]

In my case I just delete "tomcat-embed-el" dependency.

                <exclusion>
                    <groupId>org.apache.tomcat.embed</groupId>
                    <artifactId>tomcat-embed-el</artifactId>
                </exclusion>

And it works.....

Solution 6:[6]

Make sure Maven runs the correct Java JDK. For MacOS, run /usr/libexec/java_home.

This will search /Library/Java/JavaVirtualMachines/ for JDKs you've installed. It's likely that even though $JAVA_HOME is set to the intended JDK, the child process could be running on some other JDK. If you're not running the intended JDK, you may have to set aside the other JDK outside of /Library/Java/JavaVirtualMachines/. I haven't found a better way to do this, unfortunately.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mahbuhs_redoc
Solution 2 Komal
Solution 3 poomcyber
Solution 4 Zhong Linzhou
Solution 5 weilin wang
Solution 6 Andrew Feikema