Home > OS >  Deploy Spring Boot app to .jar with maven wrapper causes UnsatisfiedDependencyException
Deploy Spring Boot app to .jar with maven wrapper causes UnsatisfiedDependencyException

Time:02-12

Im trying to deploy my Spring Boot app to .jar file. I'm using maven wrapper to do it. Aplication works normally when I run it from intelij. Also maven package is created without any errors, but when I try to run the app I get UnsatisfiedDependencyException.

Here are command I used to deploy an app

.\mvnw package

Here is how I run it from target folder

java -jar .\Crypto_Tracker_App-0.0.1-SNAPSHOT.jar

Full logs after running an app

2022-02-11 18:12:42.086  INFO 18992 --- [           main] c.e.C.CryptoTrackerAppApplication        : Starting CryptoTrackerAppApplication v0.0.1-SNAPSHOT using Java 17 on LAPTOP-H644PS
E4 with PID 18992 (C:\dev_java\Crypto_Tracker_App\Backend\target\Crypto_Tracker_App-0.0.1-SNAPSHOT.jar started by 77jt7 in C:\dev_java\Crypto_Tracker_App\Backend\target)
2022-02-11 18:12:42.091  INFO 18992 --- [           main] c.e.C.CryptoTrackerAppApplication        : No active profile set, falling back to default profiles: default
2022-02-11 18:12:43.070  INFO 18992 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-02-11 18:12:43.161  INFO 18992 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 69 ms. Found 4 JPA repository interface
s.
2022-02-11 18:12:44.129  INFO 18992 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8084 (http)
2022-02-11 18:12:44.149  INFO 18992 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-02-11 18:12:44.150  INFO 18992 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.56]
2022-02-11 18:12:44.251  INFO 18992 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-02-11 18:12:44.252  INFO 18992 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2072 ms
2022-02-11 18:12:44.502  INFO 18992 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-02-11 18:12:44.611  INFO 18992 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.3.Final
2022-02-11 18:12:44.858  INFO 18992 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-02-11 18:12:44.987  INFO 18992 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-02-11 18:12:45.338  INFO 18992 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-02-11 18:12:45.366  INFO 18992 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2022-02-11 18:12:46.703  INFO 18992 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.
platform.internal.NoJtaPlatform]
2022-02-11 18:12:46.715  INFO 18992 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-02-11 18:12:47.205  WARN 18992 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be p
erformed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-02-11 18:12:47.245  WARN 18992 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: o
rg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cryptoCoinController' defined in URL [jar:file:/C:/dev_java/Crypto_Tracker_App/Backend/t
arget/Crypto_Tracker_App-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/example/Crypto_Tracker_App/app/controller/CryptoCoinController.class]: Unsatisfied dependency expressed through const
ructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cryptoCoinService': Lookup method resolution failed; nes
ted exception is java.lang.IllegalStateException: Failed to introspect Class [com.example.Crypto_Tracker_App.app.service.CryptoCoinService] from ClassLoader [org.springframework.boot.l
oader.LaunchedURLClassLoader@18769467]
2022-02-11 18:12:47.247  INFO 18992 --- [           main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2022-02-11 18:12:47.249  INFO 18992 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-02-11 18:12:47.270  INFO 18992 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2022-02-11 18:12:47.275  INFO 18992 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-02-11 18:12:47.296  INFO 18992 --- [           main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-02-11 18:12:47.331 ERROR 18992 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cryptoCoinController' defined in URL [jar:file:/C:/dev_java/Crypto_Tracker_App/Backend/
target/Crypto_Tracker_App-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/example/Crypto_Tracker_App/app/controller/CryptoCoinController.class]: Unsatisfied dependency expressed through cons
tructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cryptoCoinService': Lookup method resolution failed; ne
sted exception is java.lang.IllegalStateException: Failed to introspect Class [com.example.Crypto_Tracker_App.app.service.CryptoCoinService] from ClassLoader [org.springframework.boot.
loader.LaunchedURLClassLoader@18769467]
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) ~[spring-beans-5.3.14.jar!/:5.
3.14]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-5.3.14.jar!/:5.3
.14]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.14.jar!/:5.3.14]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.14.jar!/:5.3.14]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.2.jar!/:2.6.2]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) ~[spring-boot-2.6.2.jar!/:2.6.2]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) ~[spring-boot-2.6.2.jar!/:2.6.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot-2.6.2.jar!/:2.6.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.2.jar!/:2.6.2]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) ~[spring-boot-2.6.2.jar!/:2.6.2]
        at com.example.Crypto_Tracker_App.CryptoTrackerAppApplication.main(CryptoTrackerAppApplication.java:22) ~[classes!/:0.0.1-SNAPSHOT]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[Crypto_Tracker_App-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[Crypto_Tracker_App-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[Crypto_Tracker_App-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[Crypto_Tracker_App-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cryptoCoinService': Lookup method resolution failed; nested exception is java.lang.Il
legalStateException: Failed to introspect Class [com.example.Crypto_Tracker_App.app.service.CryptoCoinService] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@
18769467]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:289) ~[spring-bean
s-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1302) ~[spri
ng-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1219) ~[spring-beans-5.3.14.jar!/:5.3
.14]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1389) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) ~[spring-beans-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-5.3.14.jar!/:5.3.14]
        ... 27 common frames omitted
Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.example.Crypto_Tracker_App.app.service.CryptoCoinService] from ClassLoader [org.springframework.boot.loader.
LaunchedURLClassLoader@18769467]
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481) ~[spring-core-5.3.14.jar!/:5.3.14]
        at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:321) ~[spring-core-5.3.14.jar!/:5.3.14]
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:267) ~[spring-bean
s-5.3.14.jar!/:5.3.14]
        ... 40 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/configurationprocessor/json/JSONException
        at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
        at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3402) ~[na:na]
        at java.base/java.lang.Class.getDeclaredMethods(Class.java:2504) ~[na:na]
        at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463) ~[spring-core-5.3.14.jar!/:5.3.14]
        ... 42 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.configurationprocessor.json.JSONException
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:440) ~[na:na]
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587) ~[na:na]
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151) ~[Crypto_Tracker_App-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na]
        ... 46 common frames omitted

Here is cryptoCoinController where the error seems to appear

package com.example.Crypto_Tracker_App.app.controller;

import com.example.Crypto_Tracker_App.app.dto.CoinsRequest;
import com.example.Crypto_Tracker_App.app.service.CryptoCoinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/api/coins")
public class CryptoCoinController {

    private final CryptoCoinService cryptoCoinService;

    @Autowired
    public CryptoCoinController(CryptoCoinService cryptoCoinService) {
        this.cryptoCoinService = cryptoCoinService;
    }

    @GetMapping("/all")
    @PreAuthorize("hasAuthority('user:read')")
    public ResponseEntity<String> getAllCoins() throws IOException {
        String coins = cryptoCoinService.getAllCoins();
        return ResponseEntity.ok(coins);
    }

    @GetMapping("")
    @PreAuthorize("hasAuthority('user:read')")
    public ResponseEntity<String> getSpecificCoins(@RequestBody CoinsRequest request) throws IOException {
        String coins = cryptoCoinService.getSpecificCoins(request.getNames());
        return ResponseEntity.ok(coins);
    }
}

CodePudding user response:

When analyzing a stack trace, always have a look at the lowermost cause. Here

Caused by: java.lang.ClassNotFoundException: org.springframework.boot.configurationprocessor.json.JSONException

So the exception class is not in the class path. This is a very common problem for new Java programmers, as the default way to create a JAR file does not include its dependencies ("other jars").

See https://www.baeldung.com/deployable-fat-jar-spring-boot for a tutorial on how to have Spring Boot create a complete deployment for you (a so-called "fat jar").

  • Related