Unable to create a JAR file

I want to dockerize the application, so thats why i want to create a JAR.
The application works perfectly in IDE, but when i create a JAR and run it using

java -jar app.jar

It gives the following error.

22:17:32.840 [main] INFO com.omkarlubal.quiz.QuizApplication - Starting QuizApplication on DESKTOP-AP3I71I with PID 24372 (E:\projects\java_spring\omkarlubal-me_buildout_qa\out\artifacts\BUILDOUT_QA_buildout_main_jar\BUILDOUT_QA.buildout.main.jar started by OMKAR in E:\projects\java_spring\omkarlubal-me_buildout_qa\out\artifacts\BUILDOUT_QA_buildout_main_jar)
22:17:32.841 [main] DEBUG com.omkarlubal.quiz.QuizApplication - Running with Spring Boot, Spring
22:17:32.841 [main] INFO com.omkarlubal.quiz.QuizApplication - No active profile set, falling back to default profiles: default
22:17:32.842 [main] DEBUG org.springframework.boot.SpringApplication - Loading source class com.omkarlubal.quiz.QuizApplication
22:17:32.889 [main] DEBUG org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@86be70a
22:17:32.900 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘org.springframework.context.annotation.internalConfigurationAnnotationProcessor’
22:17:32.972 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/E:/projects/java_spring/omkarlubal-me_buildout_qa/out/artifacts/BUILDOUT_QA_buildout_main_jar/BUILDOUT_QA.buildout.main.jar!/com/omkarlubal/quiz/controller/QuestionController.class]
22:17:32.983 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Ignored because not a concrete top-level class: URL [jar:file:/E:/projects/java_spring/omkarlubal-me_buildout_qa/out/artifacts/BUILDOUT_QA_buildout_main_jar/BUILDOUT_QA.buildout.main.jar!/com/omkarlubal/quiz/repository/QuestionRepository.class]
22:17:32.986 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/E:/projects/java_spring/omkarlubal-me_buildout_qa/out/artifacts/BUILDOUT_QA_buildout_main_jar/BUILDOUT_QA.buildout.main.jar!/com/omkarlubal/quiz/repositoryservices/QuestionRepositoryServiceImpl.class]
22:17:32.987 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: URL [jar:file:/E:/projects/java_spring/omkarlubal-me_buildout_qa/out/artifacts/BUILDOUT_QA_buildout_main_jar/BUILDOUT_QA.buildout.main.jar!/com/omkarlubal/quiz/services/QuestionServiceImpl.class]
22:17:33.304 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘org.springframework.boot.context.properties.ConfigurationPropertiesBeanDefinitionValidator’
22:17:33.332 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘org.springframework.context.event.internalEventListenerProcessor’
22:17:33.333 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘org.springframework.context.event.internalEventListenerFactory’
22:17:33.335 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘org.springframework.context.annotation.internalAutowiredAnnotationProcessor’
22:17:33.335 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘org.springframework.context.annotation.internalCommonAnnotationProcessor’
22:17:33.337 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor’
22:17:33.351 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘org.springframework.boot.context.internalConfigurationPropertiesBinder’
22:17:33.355 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘org.springframework.boot.context.internalConfigurationPropertiesBinderFactory’
22:17:33.363 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ‘meterRegistryPostProcessor’
22:17:33.367 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Autowiring by type from bean name ‘meterRegistryPostProcessor’ via factory method to bean named ‘org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@86be70a’
22:17:33.375 [main] DEBUG org.springframework.ui.context.support.UiApplicationContextUtils - Unable to locate ThemeSource with name ‘themeSource’: using default [org.springframework.ui.context.support.ResourceBundleThemeSource@4982cc36]
22:17:33.377 [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.
22:17:33.386 [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:156)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at com.omkarlubal.quiz.QuizApplication.main(QuizApplication.java:11)
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:203)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:153)
… 8 common frames omitted

This is my build.gradle

buildscript {
ext {
buildId = System.currentTimeMillis()
springBootVersion = “2.1.4.RELEASE”
junitVersion = “5.3.1”
junitPlatformLauncherVersion = “1.3.1”
mockitoVersion = “2.22.0”
swaggerVersion = “2.9.2”
lombokVersion = “1.18.2”
modelMapperVersion = “2.3.2”
jacksonVersion = “2.9.8”
}
repositories {
mavenCentral()
}
dependencies {
classpath(“org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion”)
}
}
plugins {
id ‘org.springframework.boot’ version ‘2.2.4.RELEASE’
id ‘io.spring.dependency-management’
id ‘java’
id ‘jacoco’
id “name.remal.sonarlint” version “1.0.189”
}
jar {
manifest {
attributes(
‘Main-Class’: ‘com.omkarlubal.quiz.QuizApplication’
)
}
}
group ‘com.crio’
version ‘1.0-SNAPSHOT’
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile “org.springframework.boot:spring-boot-starter-web”
compile “org.springframework.boot:spring-boot-starter-data-mongodb”
compile “org.springframework.boot:spring-boot-starter-actuator:$springBootVersion”
compile “org.slf4j:jul-to-slf4j:1.7.28”
compile group: ‘org.springframework.boot’, name: ‘spring-boot-starter-tomcat’, version: ‘2.1.4.RELEASE’
compile “org.springframework.boot:spring-boot-starter-webflux:$springBootVersion”
compile ‘org.projectreactor:reactor-spring:1.0.1.RELEASE’
implementation “org.modelmapper:modelmapper:$modelMapperVersion”
implementation “javax.inject:javax.inject:1”
implementation “io.springfox:springfox-swagger2:$swaggerVersion”
implementation “io.springfox:springfox-swagger-ui:$swaggerVersion”
implementation “org.projectlombok:lombok:$lombokVersion”
compile “com.fasterxml.jackson.core:jackson-core:$jacksonVersion”
compile “com.fasterxml.jackson.core:jackson-annotations:$jacksonVersion”
compile “com.fasterxml.jackson.core:jackson-databind:$jacksonVersion”
implementation “redis.clients:jedis:3.2.0”
annotationProcessor “org.projectlombok:lombok”
testAnnotationProcessor “org.projectlombok:lombok”
testCompile group: ‘de.flapdoodle.embed’, name: ‘de.flapdoodle.embed.mongo’, version: ‘2.2.0’
testImplementation “org.mockito:mockito-junit-jupiter:$mockitoVersion”
testImplementation (“org.springframework.boot:spring-boot-starter-test:$springBootVersion”) {
exclude group: “junit”, module: “junit”
}
testImplementation “org.junit.jupiter:junit-jupiter-api:$junitVersion”
testRuntimeOnly “org.junit.jupiter:junit-jupiter-engine:$junitVersion”
testImplementation “org.junit.platform:junit-platform-launcher:$junitPlatformLauncherVersion”
testImplementation “org.junit.jupiter:junit-jupiter-params:junitVersion" } test { useJUnitPlatform { } reports { junitXml.enabled = true def destinationDirectory = System.getProperty("user.home") + "/.gradle/daemon/{buildId}/test-results”
junitXml.destination = file(destinationDirectory)
html.enabled = true
}
jacoco {
enabled = true
destinationFile = file("buildDir/jacoco/{name}.exec")
includes = []
excludes = []
excludeClassLoaders = []
includeNoLocationClasses = false
sessionId = “”
dumpOnExit = true
classDumpDir = null
output = JacocoTaskExtension.Output.FILE
address = “localhost”
port = 6300
jmx = false
}
}
jacocoTestCoverageVerification {
dependsOn test
violationRules {
rule {
limit {
counter = ‘LINE’
value = ‘COVEREDRATIO’
minimum = 0.40
}
limit {
counter = ‘CLASS’
value = ‘COVEREDRATIO’
minimum = 0.40
}
limit {
counter = ‘METHOD’
value = ‘COVEREDRATIO’
minimum = 0.40
}
}
}
}
configurations {
cucumberRuntime {
extendsFrom testRuntime
}
}
task cucumber() {
dependsOn assemble, compileTestJava
doLast {
javaexec {
main = “io.cucumber.core.cli.Main”
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = [’–plugin’, ‘pretty’, ‘–glue’, ‘hellocucumber’, ‘src/test/resources’]
}
}
}
sonarlint {
ignoreFailures = true
}
//check.dependsOn jacocoTestCoverageVerification
// sonarlintMain

My main application file.

package com.omkarlubal.quiz;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class QuizApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(QuizApplication.class, args);
}
}

Tried solutions still no use.

  • Extend the SpringBootServletInitializer class.
  • Update spring-boot-starter-parent.
  • Setting spring.profiles.active=default in application.properties.

Would really use some help.

We will need the full stack trace to look into it.

@anand-crio I have provided the stack trace at the top.
I just dont understand how it is working in Intellij IDEA but giving this error when running as JAR.

Did you run gradle bootjar target?

Yeah this solved the problem!
I was building JAR file using the Build > Build Artifacts option in IDEA. But doing a gradle bootjar provided a perfect executable jar.
Can you explain what is the difference between these two methods?

Closing this topic as your issue is resolved by the mentor. If it is still not resolved, Kindly un-mark the accepted solution or create a new topic and post this question as a reference link in the description of the new topic.

Can you search on google? Its simple…

A recap - One creates a plain Jar file, without server dependencies, in our case tomcat.
The other creates a fully bloated spring powered Jar file.

1 Like