Home > Back-end >  Failed to run .jar SpringBootApplication
Failed to run .jar SpringBootApplication

Time:04-19

I built a .jar artifact using IntelliJ. Creating the artifact

Here's how my pom.xml looks like:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.3</version>
        <relativePath/>
    </parent>
    <groupId>mk.klimasistemi</groupId>
    <artifactId>klimasistemi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>klimasistemi</name>
    <description>klimasistemi</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-csv</artifactId>
            <version>1.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>klimasistemiapi</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

When I build the artifact and try to run it I get the following error.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                        

07:49:53.150 [main] INFO mk.klimasistemi.KlimasistemiApplication - Starting KlimasistemiApplication using Java 17.0.2 on beast with PID 8446 (/home/mario/Desktop/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_backend_jar/klimasistemi-backend.jar started by mario in /home/mario/Desktop/klimasistemi/klimasistemi-backend)
07:49:53.152 [main] DEBUG mk.klimasistemi.KlimasistemiApplication - Running with Spring Boot, Spring
07:49:53.152 [main] INFO mk.klimasistemi.KlimasistemiApplication - No active profile set, falling back to default profiles: default
07:49:53.152 [main] DEBUG org.springframework.boot.SpringApplication - Loading source class mk.klimasistemi.KlimasistemiApplication
07:49:53.211 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@43d7741f
07:49:53.217 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
07:49:53.261 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Ignored because not a concrete top-level class: URL [jar:file:/home/mario/Desktop/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_backend_jar/klimasistemi-backend.jar!/mk/klimasistemi/repository/AirConditionerRepository.class]
07:49:53.261 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Ignored because not a concrete top-level class: URL [jar:file:/home/mario/Desktop/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_backend_jar/klimasistemi-backend.jar!/mk/klimasistemi/repository/AirConditionerBrandRepository.class]
07:49:53.263 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/home/mario/Desktop/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_backend_jar/klimasistemi-backend.jar!/mk/klimasistemi/service/implementation/AirConditionerService.class]
07:49:53.264 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/home/mario/Desktop/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_backend_jar/klimasistemi-backend.jar!/mk/klimasistemi/service/implementation/AirConditionerBrandService.class]
07:49:53.276 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/home/mario/Desktop/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_backend_jar/klimasistemi-backend.jar!/mk/klimasistemi/controller/AirConditionerBrandController.class]
07:49:53.279 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/home/mario/Desktop/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_backend_jar/klimasistemi-backend.jar!/mk/klimasistemi/controller/AirConditionerController.class]
07:49:53.281 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/home/mario/Desktop/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_backend_jar/klimasistemi-backend.jar!/mk/klimasistemi/config/ConfigurationSetup.class]
07:49:53.293 [main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [mk.klimasistemi.KlimasistemiApplication]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.servlet.support.SpringBootServletInitializer
07:49:53.298 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [mk.klimasistemi.KlimasistemiApplication]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.servlet.support.SpringBootServletInitializer
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:189)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:302)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
    at mk.klimasistemi.KlimasistemiApplication.main(KlimasistemiApplication.java:11)
Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.servlet.support.SpringBootServletInitializer
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:162)
    at org.springframework.context.annotation.ConfigurationClassParser.retrieveBeanMethodMetadata(ConfigurationClassParser.java:403)
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:326)
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207)
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175)
    ... 12 common frames omitted
Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.boot.web.servlet.support.SpringBootServletInitializer] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@5cb0d902]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481)
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:455)
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:151)
    ... 17 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
    at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3402)
    at java.base/java.lang.Class.getDeclaredMethods(Class.java:2504)
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463)
    ... 19 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
    ... 23 common frames omitted

Process finished with exit code 1

After some research I found that maybe I have a problem with my class annotations, but I checked them and they seem fine. Any idea how to fix this so I can run my jar file?

EDIT

Here is the class mk.klimasistemi.KlimasistemiApplication

package mk.klimasistemi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class KlimasistemiApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(KlimasistemiApplication.class, args);
    }
}

EDIT 18.04.2022 I tried to remove the extends SpringBootServletInitializer from the main method and deleted the tomcat dependency from pom.xml RESULT:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v0.0.1-SNAPSHOT)

11:47:12.310 [main] INFO mk.klimasistemi.KlimasistemiApplication - Starting KlimasistemiApplication v0.0.1-SNAPSHOT using Java 17.0.2 on mario-MS-7C96 with PID 15167 (/home/mario/Documents/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_jar/klimasistemi.jar started by mario in /home/mario/Documents/klimasistemi/klimasistemi-backend)
11:47:12.311 [main] DEBUG mk.klimasistemi.KlimasistemiApplication - Running with Spring Boot v0.0.1-SNAPSHOT, Spring v0.0.1-SNAPSHOT
11:47:12.311 [main] INFO mk.klimasistemi.KlimasistemiApplication - No active profile set, falling back to default profiles: default
11:47:12.312 [main] DEBUG org.springframework.boot.SpringApplication - Loading source class mk.klimasistemi.KlimasistemiApplication
11:47:12.362 [main] DEBUG org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@55040f2f
11:47:12.371 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
11:47:12.454 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/home/mario/Documents/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_jar/klimasistemi.jar!/mk/klimasistemi/controller/AirConditionerController.class]
11:47:12.455 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/home/mario/Documents/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_jar/klimasistemi.jar!/mk/klimasistemi/controller/AirConditionerBrandController.class]
11:47:12.458 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/home/mario/Documents/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_jar/klimasistemi.jar!/mk/klimasistemi/service/implementation/AirConditionerBrandService.class]
11:47:12.459 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/home/mario/Documents/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_jar/klimasistemi.jar!/mk/klimasistemi/service/implementation/AirConditionerService.class]
11:47:12.464 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/home/mario/Documents/klimasistemi/klimasistemi-backend/out/artifacts/klimasistemi_jar/klimasistemi.jar!/mk/klimasistemi/config/ConfigurationSetup.class]
11:47:12.615 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
11:47:12.617 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
11:47:12.619 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
11:47:12.621 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
11:47:12.623 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'
11:47:12.626 [main] DEBUG org.springframework.ui.context.support.UiApplicationContextUtils - Unable to locate ThemeSource with name 'themeSource': using default [org.springframework.ui.context.support.ResourceBundleThemeSource@573906eb]
11:47:12.627 [main] WARN org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
11:47:12.633 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:163)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:577)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:302)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
    at mk.klimasistemi.KlimasistemiApplication.main(KlimasistemiApplication.java:10)
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:210)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:180)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:160)
    ... 8 common frames omitted

Process finished with exit code 1

CodePudding user response:

Follow the below two steps and let us know.

Step 1: Just remove extends SpringBootServletInitializer from your main class.

Step 2: Remove the below dependency from your pom.xml file.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

Clean the maven, take the new build as a jar and run.

CodePudding user response:

This here solved my problem. I dont know what it does and have no idea how it works. But it works.

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <classifier>spring-boot</classifier>
                            <mainClass>mk.klimasistemi.KlimasistemiApplication</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  • Related