Could not exec java with Spring+Maven exit code 1
Asked Answered
L

6

15

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
Luzern answered 1/4, 2017 at 13:30 Comment(2)
Have you simply tried to call the application on plain command line via java -jar target/filename.jar ? The message which is given Application finished with exit code: 1 at ...looks like there is a problem in the app...Cilice
I'm not sure it's related to your problem, but ordinarily, the pom.xml file goes in the project root folder, not under src/javaKarlakarlan
S
3

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.

Sewell answered 1/4, 2017 at 16:0 Comment(2)
thanx a lot, moving pom.xml to the right folder helped build succeed, and commenting out those dependencies helped it run, with the location everything is easy to grasp, but can U explain me, please, how those dependencies hurted the application?Luzern
Glad to help. Traditionally, we follow J2EE specification and build our web application to a war, then deploy it to servlet container like Tomcat, Spring Boot tries to build/deploy/run spring based applications easier by package all necessary stuff into one bundle, it has different starters for different kinds of applications, for web applications, the specific starter also packages servlet container inside, like Tomcat or Jetty. spring-boot-starter-web uses embed tomcat by default, so if you excludes it when build the package, there's no place to deploy your application, so it just exits.Sewell
A
6

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!

Anu answered 27/7, 2019 at 9:18 Comment(1)
my app started doing the same thing. Also removed devtools and it started working. Strange, why all of a sudden?Ensample
S
5

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

Sabba answered 30/7, 2018 at 19:55 Comment(0)
S
3

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.

Sewell answered 1/4, 2017 at 16:0 Comment(2)
thanx a lot, moving pom.xml to the right folder helped build succeed, and commenting out those dependencies helped it run, with the location everything is easy to grasp, but can U explain me, please, how those dependencies hurted the application?Luzern
Glad to help. Traditionally, we follow J2EE specification and build our web application to a war, then deploy it to servlet container like Tomcat, Spring Boot tries to build/deploy/run spring based applications easier by package all necessary stuff into one bundle, it has different starters for different kinds of applications, for web applications, the specific starter also packages servlet container inside, like Tomcat or Jetty. spring-boot-starter-web uses embed tomcat by default, so if you excludes it when build the package, there's no place to deploy your application, so it just exits.Sewell
G
0

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>
Gladdie answered 20/5, 2019 at 23:26 Comment(0)
M
0

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

Margalit answered 19/3, 2020 at 15:11 Comment(0)
P
0

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.

Procter answered 4/5, 2022 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.