Dependency on shared code from jvm module in KMM project

I have some problems setting up a dependency in a KMM project from a jvmmodule to the shared code. In short, the attached image shows what I want to do. The jvmmodule has a class called Test with a main method which uses code from the shared module.

I tried several solutions, none which are 100% working:

  1. (
    jvmmodule is configured as a regular kotlin jvm module which also has the application plugin enabled for testing purposes. The module has a project dependency on the shared module. When the Test class is ran using the ./gradlew :jvmmodule:run command everything works as expected. The output in the console should is ‘Hello from JVM’.

In IntelliJ or Android Studio the Greeting class in Test.kt cannot be found (not in the editor, compile error) and not at runtime. When running the classes from the gutter icon the following error occurs:

Caused by: java.lang.ClassNotFoundException: Greeting
   at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(
   at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(
   at java.base/java.lang.ClassLoader.loadClass(
   ... 1 more
  1. ( (inspired by kotlinconf app)
    jvmmodule is configured as a multiplatform module with a single jvm target and a project dependency on the shared module. In this scenario code completion works in IntelliJ but when the Test class is run from the gutter icon the following error occurs:

Error: Could not find or load main class TestKt
Caused by: java.lang.ClassNotFoundException: TestKt

Is this the correct way of using shared code from a plain kotlin (kvm) module?

1 Like

I know this has been some time, but have you found a solution?

I think it is a known issue now and covered by this: