Kotlin error running class not found in module

3 votes and 10 comments so far on Reddit

Edit: I’ve found a couple of workarounds/solutions, though I think something is still screwy in that I’m required to do these things (see bottom of post.)


For several hours I’ve been banging my head against this. I can’t compile anything without this error cropping up. I’m a complete newbie, so thanks in advance and bear with me please.

I’ve just started using IntelliJ in the course of attempting to learn Kotlin. I followed this official tutorial exactly, or rather, I attempted to. The first sign of trouble is that there’s no option to compile and run as there is in the pictured example, at least, not at first. After going to «edit configuration» and creating a Kotlin run/debug configuration from defaults, that option does appear, but I still get the error message:

Error running 'HelloWorld'
Class 'Main' not found in module Untitled 

Things I’ve tried:

  • Changing the name of the Main class in the run/debug configuration to HelloWorld.kt (this simply changes ‘Main’ to ‘HelloWorld.kt’ in the error message

  • Renaming the HelloWorld.kt file to «Main»

  • Creating a new Main class and putting it in SRC with my code file

  • Creating a package file, putting the source file inside, and adding a line referring to the package

  • Updating the Kotlin plugin

  • Uninstalling and reinstalling the Kotlin plugin

  • Uninstalling and reinstalling IntelliJ IDEA

The only thing that even kinda worked was Wrapping the code in a Main class (Public Class Main {}) — however, that just changes the error to

Function 'main' not found in class 'Main'

instead. And I’m not sure why this stuff should be necessary — I’ve also watched two video tutorials using IntelliJ, and none of them mention naming classes or any such thing. They just jump right in with a function alone and it works.

I’m at the end of my rope. Any suggestions would be extremely welcome! Further info — Windows 10, Intel i5. IntelliJ Idea 2017.2.3. My source file is located in /src, called HelloWorld.kt, in a project called Untitled. The Kotlin plugin is enabled, I created the project using the Kotlin JVM option, and I’m using Java JDK 1.8.0_144.


Workarounds:

  • The first thing that worked was creating a package called com.me and then invoking a class in the file called com.me.HelloWorld. I have no idea why this worked.

  • The second thing that seems to work is running initially with alt-shift-f10, instead of shift-f10. This brings up a dialog which shows a run/debug configuration which IntelliJ apparently created specifically for this file. I think it’s supposed to do this automatically, and for some reason, it isn’t, for me.

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.

@simplebooklib

Description of the issue:
Main method not found with kotlin.
It doesn’t work a Kotlin mainClass. if I add Java mainClass, It works.

Expected behavior:
I only want to run an application by Kotlin. without Java file

Steps to reproduce:
package structure

src
 ┗ main
     ┗java
        ┗ com.sample.test
               ┗ JavaApplication.java
     ┗kotlin
        ┗ com.sample.test
               ┗ Application.kt

build.gradle

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.11'
    id 'com.google.cloud.tools.jib' version '0.10.1'
}

group 'com.sample.test'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.1.RELEASE'
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

jib {
    from {
        image = 'openjdk:12-jdk-alpine'
    }
    to {
        image = ".../kotlin-sample"
        tags = ['0.0.1', 'latest']
        auth {
            username = '...'
            password = '...'
        }
    }
    container {
        jvmFlags = ['-Xms256m', '-Xdebug']
        mainClass = 'com.sample.test.JavaApplication'
        ports = ['8040', '8080']
        useCurrentTimestamp = true
    }
}

tasks.build.dependsOn tasks.jib

Log output:

Error: Main method not found in class com.sample.test.Application, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

@chanseokoh

Hi @simplebooklib,

In #396, a Kotlin project without any Java code works, where src/main/kotlin/HelloWorld.kt is

fun main(args: Array<String>) {
    println("Hello, world!")
}

and jib.container.mainClass is configured to HelloWorldKt.

jib {
    container.mainClass = "HelloWorldKt"
}

I don’t know anything about Kotlin, but I think you can specify the main class name as com.sample.test.ApplicationKt (i.e., with the Kt suffix). Check the project setup in #396.

@simplebooklib

@chanseokoh
I really didn’t know about Kotlin. Thank you for advice with the Kt suffix.

Понравилась статья? Поделить с друзьями:

Читайте также:

  • Korting kdi 60175 ошибка e4 как устранить
  • Korting kdi 4540 коды ошибок
  • Korting kdi 45165 ошибка e4 как устранить
  • Korg pandora mini memory error
  • Kontakt runtime error in setup script

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии