others-how to solve the classnotfound issue when trying to run a kotlin application with springboot?

1. Purpose

In this post, I will demonstrate how to solve th following issue when trying to run a kotlin application with springboot.

> Task :api-messages:bootRun
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.5)

2022-11-03 10:58:38.292  INFO 79617 --- [  restartedMain] ApiMessagesAppKt                         : Starting ApiMessagesAppKt using Java 11.0.17 on MacBook-Pro-bswen.local with PID 79617 (/Users/bswen/KotlinProjects/blog/blog-backend-apis/blog-backend-apis/api-messages/build/classes/kotlin/main started by bswen in /Users/bswen/KotlinProjects/blog/blog-backend-apis/blog-backend-apis/api-messages)
2022-11-03 10:58:38.293  INFO 79617 --- [  restartedMain] ApiMessagesAppKt                         : No active profile set, falling back to 1 default profile: "default"
2022-11-03 10:58:38.394  INFO 79617 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-11-03 10:58:38.394  INFO 79617 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-11-03 10:58:38.438  WARN 79617 --- [  restartedMain] ionWarningsApplicationContextInitializer : 

** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.


2022-11-03 10:58:39.727  WARN 79617 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/Users/bswen/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-autoconfigure/2.7.5/96646e63a2296d0a3209383e81cdb8c87ab2f913/spring-boot-autoconfigure-2.7.5.jar!/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations$PoolConfiguration.class]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
2022-11-03 10:58:39.743  INFO 79617 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-11-03 10:58:39.834 ERROR 79617 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/Users/bswen/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-autoconfigure/2.7.5/96646e63a2296d0a3209383e81cdb8c87ab2f913/spring-boot-autoconfigure-2.7.5.jar!/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations$PoolConfiguration.class]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:457) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:316) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:276) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:128) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:295) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:249) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:206) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:174) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.5.jar:2.7.5]
    at ApiMessagesAppKt.main(ApiMessagesApp.kt:11) ~[main/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[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:566) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.7.5.jar:2.7.5]
Caused by: java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
    at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:54) ~[spring-boot-autoconfigure-2.7.5.jar:2.7.5]
    at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:108) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:88) ~[spring-context-5.3.23.jar:5.3.23]
Caused by: java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)

    at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:71) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isConditionMatch(ClassPathScanningCandidateComponentProvider.java:512) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.isCandidateComponent(ClassPathScanningCandidateComponentProvider.java:495) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.scanCandidateComponents(ClassPathScanningCandidateComponentProvider.java:430) ~[spring-context-5.3.23.jar:5.3.23]
    ... 25 common frames omitted
Caused by: java.lang.NoClassDefFoundError: io/r2dbc/spi/ValidationDepth
Caused by: java.lang.NoClassDefFoundError: io/r2dbc/spi/ValidationDepth

    at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
    at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3166) ~[na:na]
    at java.base/java.lang.Class.getDeclaredMethods(Class.java:2309) ~[na:na]
    at org.springframework.boot.context.properties.bind.JavaBeanBinder$Bean.getSorted(JavaBeanBinder.java:140) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.JavaBeanBinder$Bean.addProperties(JavaBeanBinder.java:132) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.JavaBeanBinder$Bean.<init>(JavaBeanBinder.java:127) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.JavaBeanBinder$Bean.get(JavaBeanBinder.java:226) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:54) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.Binder.lambda$bindDataObject$5(Binder.java:476) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.Binder$Context.withIncreasedDepth(Binder.java:590) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.Binder$Context.withDataObject(Binder.java:576) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.Binder$Context.access$300(Binder.java:537) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.Binder.bindDataObject(Binder.java:474) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.Binder.bindObject(Binder.java:414) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:343) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:332) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:262) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:223) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PooledConnectionFactoryCondition.getMatchOutcome(ConnectionFactoryConfigurations.java:138) ~[spring-boot-autoconfigure-2.7.5.jar:2.7.5]
    at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-2.7.5.jar:2.7.5]
    ... 31 common frames omitted
Caused by: java.lang.ClassNotFoundException: io.r2dbc.spi.ValidationDepth
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) ~[na:na]
Caused by: java.lang.ClassNotFoundException: io.r2dbc.spi.ValidationDepth

    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
    ... 51 common frames omitted


The core error is as follows:


Caused by: java.lang.ClassNotFoundException: io.r2dbc.spi.ValidationDepth
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) ~[na:na]
Caused by: java.lang.ClassNotFoundException: io.r2dbc.spi.ValidationDepth

    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
    ... 51 common frames omitted

The runtime classpath is as follows:

runtimeClasspath - Runtime classpath of compilation 'main' (target  (jvm)).
+--- org.springframework.boot:spring-boot-devtools -> 2.7.5
|    +--- org.springframework.boot:spring-boot:2.7.5
|    |    +--- org.springframework:spring-core:5.3.23
|    |    |    \--- org.springframework:spring-jcl:5.3.23
|    |    \--- org.springframework:spring-context:5.3.23
|    |         +--- org.springframework:spring-aop:5.3.23
|    |         |    +--- org.springframework:spring-beans:5.3.23
|    |         |    |    \--- org.springframework:spring-core:5.3.23 (*)
|    |         |    \--- org.springframework:spring-core:5.3.23 (*)
|    |         +--- org.springframework:spring-beans:5.3.23 (*)
|    |         +--- org.springframework:spring-core:5.3.23 (*)
|    |         \--- org.springframework:spring-expression:5.3.23
|    |              \--- org.springframework:spring-core:5.3.23 (*)
|    \--- org.springframework.boot:spring-boot-autoconfigure:2.7.5
|         \--- org.springframework.boot:spring-boot:2.7.5 (*)
+--- project :api-common
|    \--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.20
|         +--- org.jetbrains.kotlin:kotlin-stdlib:1.7.20
|         |    +--- org.jetbrains.kotlin:kotlin-stdlib-common:1.7.20
|         |    \--- org.jetbrains:annotations:13.0
|         \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.7.20
|              \--- org.jetbrains.kotlin:kotlin-stdlib:1.7.20 (*)
+--- org.springframework.boot:spring-boot-starter-data-jpa -> 2.7.5
|    +--- org.springframework.boot:spring-boot-starter-aop:2.7.5
|    |    +--- org.springframework.boot:spring-boot-starter:2.7.5
|    |    |    +--- org.springframework.boot:spring-boot:2.7.5 (*)
|    |    |    +--- org.springframework.boot:spring-boot-autoconfigure:2.7.5 (*)
|    |    |    +--- org.springframework.boot:spring-boot-starter-logging:2.7.5
|    |    |    |    +--- ch.qos.logback:logback-classic:1.2.11
|    |    |    |    |    +--- ch.qos.logback:logback-core:1.2.11
|    |    |    |    |    \--- org.slf4j:slf4j-api:1.7.32 -> 1.7.36
|    |    |    |    +--- org.apache.logging.log4j:log4j-to-slf4j:2.17.2
|    |    |    |    |    +--- org.slf4j:slf4j-api:1.7.35 -> 1.7.36
|    |    |    |    |    \--- org.apache.logging.log4j:log4j-api:2.17.2
|    |    |    |    \--- org.slf4j:jul-to-slf4j:1.7.36
|    |    |    |         \--- org.slf4j:slf4j-api:1.7.36
|    |    |    +--- jakarta.annotation:jakarta.annotation-api:1.3.5
|    |    |    +--- org.springframework:spring-core:5.3.23 (*)
|    |    |    \--- org.yaml:snakeyaml:1.30
|    |    +--- org.springframework:spring-aop:5.3.23 (*)
|    |    \--- org.aspectj:aspectjweaver:1.9.7
|    +--- org.springframework.boot:spring-boot-starter-jdbc:2.7.5
|    |    +--- org.springframework.boot:spring-boot-starter:2.7.5 (*)
|    |    +--- com.zaxxer:HikariCP:4.0.3
|    |    |    \--- org.slf4j:slf4j-api:1.7.30 -> 1.7.36
|    |    \--- org.springframework:spring-jdbc:5.3.23
|    |         +--- org.springframework:spring-beans:5.3.23 (*)
|    |         +--- org.springframework:spring-core:5.3.23 (*)
|    |         \--- org.springframework:spring-tx:5.3.23
|    |              +--- org.springframework:spring-beans:5.3.23 (*)
|    |              \--- org.springframework:spring-core:5.3.23 (*)
|    +--- jakarta.transaction:jakarta.transaction-api:1.3.3
|    +--- jakarta.persistence:jakarta.persistence-api:2.2.3
|    +--- org.hibernate:hibernate-core:5.6.12.Final
|    |    +--- org.jboss.logging:jboss-logging:3.4.3.Final
|    |    +--- net.bytebuddy:byte-buddy:1.12.9 -> 1.12.18
|    |    +--- antlr:antlr:2.7.7
|    |    +--- org.jboss:jandex:2.4.2.Final
|    |    +--- com.fasterxml:classmate:1.5.1
|    |    +--- org.hibernate.common:hibernate-commons-annotations:5.1.2.Final
|    |    |    \--- org.jboss.logging:jboss-logging:3.3.2.Final -> 3.4.3.Final
|    |    \--- org.glassfish.jaxb:jaxb-runtime:2.3.1 -> 2.3.7
|    |         +--- jakarta.xml.bind:jakarta.xml.bind-api:2.3.3
|    |         +--- org.glassfish.jaxb:txw2:2.3.7
|    |         +--- com.sun.istack:istack-commons-runtime:3.0.12
|    |         \--- com.sun.activation:jakarta.activation:1.2.2
|    +--- org.springframework.data:spring-data-jpa:2.7.5
|    |    +--- org.springframework.data:spring-data-commons:2.7.5
|    |    |    +--- org.springframework:spring-core:5.3.23 (*)
|    |    |    +--- org.springframework:spring-beans:5.3.23 (*)
|    |    |    \--- org.slf4j:slf4j-api:1.7.32 -> 1.7.36
|    |    +--- org.springframework:spring-orm:5.3.23
|    |    |    +--- org.springframework:spring-beans:5.3.23 (*)
|    |    |    +--- org.springframework:spring-core:5.3.23 (*)
|    |    |    +--- org.springframework:spring-jdbc:5.3.23 (*)
|    |    |    \--- org.springframework:spring-tx:5.3.23 (*)
|    |    +--- org.springframework:spring-context:5.3.23 (*)
|    |    +--- org.springframework:spring-aop:5.3.23 (*)
|    |    +--- org.springframework:spring-tx:5.3.23 (*)
|    |    +--- org.springframework:spring-beans:5.3.23 (*)
|    |    +--- org.springframework:spring-core:5.3.23 (*)
|    |    \--- org.slf4j:slf4j-api:1.7.32 -> 1.7.36
|    \--- org.springframework:spring-aspects:5.3.23
|         \--- org.aspectj:aspectjweaver:1.9.7
+--- org.springframework.boot:spring-boot-starter-mustache -> 2.7.5
|    +--- org.springframework.boot:spring-boot-starter:2.7.5 (*)
|    \--- com.samskivert:jmustache:1.15
+--- org.springframework.boot:spring-boot-starter-web -> 2.7.5
|    +--- org.springframework.boot:spring-boot-starter:2.7.5 (*)
|    +--- org.springframework.boot:spring-boot-starter-json:2.7.5
|    |    +--- org.springframework.boot:spring-boot-starter:2.7.5 (*)
|    |    +--- org.springframework:spring-web:5.3.23
|    |    |    +--- org.springframework:spring-beans:5.3.23 (*)
|    |    |    \--- org.springframework:spring-core:5.3.23 (*)
|    |    +--- com.fasterxml.jackson.core:jackson-databind:2.13.4.2
|    |    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.13.4
|    |    |    |    \--- com.fasterxml.jackson:jackson-bom:2.13.4
|    |    |    |         +--- com.fasterxml.jackson.core:jackson-annotations:2.13.4 (c)
|    |    |    |         +--- com.fasterxml.jackson.core:jackson-databind:2.13.4 -> 2.13.4.2 (c)
|    |    |    |         +--- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.4 (c)
|    |    |    |         +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.4 (c)
|    |    |    |         +--- com.fasterxml.jackson.module:jackson-module-kotlin:2.13.4 (c)
|    |    |    |         +--- com.fasterxml.jackson.module:jackson-module-parameter-names:2.13.4 (c)
|    |    |    |         \--- com.fasterxml.jackson.core:jackson-core:2.13.4 (c)
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.13.4
|    |    |    |    \--- com.fasterxml.jackson:jackson-bom:2.13.4 (*)
|    |    |    \--- com.fasterxml.jackson:jackson-bom:2.13.4 (*)
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.4
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.13.4 (*)
|    |    |    +--- com.fasterxml.jackson.core:jackson-databind:2.13.4 -> 2.13.4.2 (*)
|    |    |    \--- com.fasterxml.jackson:jackson-bom:2.13.4 (*)
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.4
|    |    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.13.4 (*)
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.13.4 (*)
|    |    |    +--- com.fasterxml.jackson.core:jackson-databind:2.13.4 -> 2.13.4.2 (*)
|    |    |    \--- com.fasterxml.jackson:jackson-bom:2.13.4 (*)
|    |    \--- com.fasterxml.jackson.module:jackson-module-parameter-names:2.13.4
|    |         +--- com.fasterxml.jackson.core:jackson-core:2.13.4 (*)
|    |         +--- com.fasterxml.jackson.core:jackson-databind:2.13.4 -> 2.13.4.2 (*)
|    |         \--- com.fasterxml.jackson:jackson-bom:2.13.4 (*)
|    +--- org.springframework.boot:spring-boot-starter-tomcat:2.7.5
|    |    +--- jakarta.annotation:jakarta.annotation-api:1.3.5
|    |    +--- org.apache.tomcat.embed:tomcat-embed-core:9.0.68
|    |    +--- org.apache.tomcat.embed:tomcat-embed-el:9.0.68
|    |    \--- org.apache.tomcat.embed:tomcat-embed-websocket:9.0.68
|    |         \--- org.apache.tomcat.embed:tomcat-embed-core:9.0.68
|    +--- org.springframework:spring-web:5.3.23 (*)
|    \--- org.springframework:spring-webmvc:5.3.23
|         +--- org.springframework:spring-aop:5.3.23 (*)
|         +--- org.springframework:spring-beans:5.3.23 (*)
|         +--- org.springframework:spring-context:5.3.23 (*)
|         +--- org.springframework:spring-core:5.3.23 (*)
|         +--- org.springframework:spring-expression:5.3.23 (*)
|         \--- org.springframework:spring-web:5.3.23 (*)
+--- com.fasterxml.jackson.module:jackson-module-kotlin -> 2.13.4
|    +--- com.fasterxml.jackson.core:jackson-databind:2.13.4 -> 2.13.4.2 (*)
|    +--- com.fasterxml.jackson.core:jackson-annotations:2.13.4 (*)
|    +--- org.jetbrains.kotlin:kotlin-reflect:1.5.30 -> 1.7.20
|    |    \--- org.jetbrains.kotlin:kotlin-stdlib:1.7.20 (*)
|    \--- com.fasterxml.jackson:jackson-bom:2.13.4 (*)
+--- org.jetbrains.kotlin:kotlin-reflect:1.7.20 (*)
+--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.7.20 (*)
\--- com.h2database:h2 -> 2.1.214

The app’s build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot")

    kotlin("jvm")
    kotlin("plugin.spring")
    id("application")
}


dependencies {
    implementation(project(":api-common"))
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-mustache")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    runtimeOnly("com.h2database:h2")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}


kotlin {
    jvmToolchain(11)
}


application {
    applicationDefaultJvmArgs = listOf("-javaagent:/Users/bswen/tech/skywalking/agent/skywalking-agent/skywalking-agent.jar=agent.service_name=api-messages",
        "-Dskywalking_config=/Users/bswen/KotlinProjects/blog/blog-backend-apis/blog-backend-apis/skywalking/agent.config")
}

The parent’s build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id("org.springframework.boot") version "2.7.5" apply false
    id("io.spring.dependency-management") version "1.0.15.RELEASE" apply false
    kotlin("jvm") version "1.7.20" apply false
    kotlin("plugin.spring") version "1.7.20" apply false
    kotlin("plugin.jpa") version "1.7.20" apply false
}

allprojects {
    group = "com.example"
    version = "1.0.0"

    tasks.withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget = "11"
        }
    }

    tasks.withType<Test> {
        useJUnitPlatform()
    }

}

subprojects {
    repositories {
        mavenCentral()
    }

    apply {
        plugin("io.spring.dependency-management")
    }
}

The directory structure:

.
├── api-common
│   └── src
│       ├── main
│       │   ├── kotlin

│       │   │               └── Message.kt
│       │   └── resources
│       └── test
│           ├── java
│           └── resources
├── api-messages
│   └── src
│       ├── main
│       │   ├── kotlin
│       │   │           ├── ApiMessagesApp.kt
│       │   │           └── controllers
│       │   │               └── MessageController.kt
│       │   └── resources
│       └── test
│           ├── java
│           ├── kotlin
│           └── resources
├── gradle
│   └── wrapper
└── skywalking

Attention that currently the file Message.kt is in src/main/kotlin directory.



2. Solution

Change springbootapplication to use the package defined in gradle:

.
├── api-common
│   └── src
│       ├── main
│       │   ├── kotlin
│       │   │   └── com
│       │   │       └── example
│       │   │           └── domain
│       │   │               └── Message.kt
│       │   └── resources
│       └── test
│           ├── java
│           └── resources
├── api-messages
│   └── src
│       ├── main
│       │   ├── kotlin
│       │   │   └── com
│       │   │       └── example
│       │   │           ├── ApiMessagesApp.kt
│       │   │           └── controllers
│       │   │               └── MessageController.kt
│       │   └── resources
│       └── test
│           ├── java
│           ├── kotlin
│           └── resources
├── gradle
│   └── wrapper
└── skywalking

We moved he Message.kt to com.example.domain to match the springboot definitions.

You can see that the key point to solve this problem is to use the correct package for your kotlin class.



3. Summary

In this post, I demonstrated how to solve the class not found exception when using springboot to run a kotlin application, the key point is to use the correct package and directory structure for your project. That’s it, thanks for your reading.