sangeetds
sangeetds

Reputation: 450

Gradle multi-module Kotlin application giving main class not found error

I have created a small project which has multiple modules and very little code inside it. I have just set it up so there are just folders with almost no code. The structure is like this:-

demo(root folder)
  -- application(module 1)
    -- src
      -- main
        -- kotlin
          --com.demo
            -- App.kt
    -- build.gradle.kt
  -- login (module 2)
  -- build.gradle.kt
  -- (other build, IDE and gradle folders)

I want to run App.kt file from gradle, by the command gradle run (which I guess is the default way of running a project with gradle application plugin) when I'm in the demo directory (I think that's possible?). I can run App.kt from the IDE easily and also by gradle run when I'm inside the application module. Here's the content of the build.gradle file in the root directory:

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

plugins {
    id("org.springframework.boot") version "2.2.0.RELEASE" apply false
    id("io.spring.dependency-management") version "1.0.8.RELEASE" apply false

    kotlin("jvm") version "1.4.30"
    kotlin("plugin.spring") version "1.3.50" apply false
    kotlin("kapt") version "1.4.10"
    
    application
}

repositories {
    jcenter()
}

subprojects {
    repositories {
        mavenCentral()
        jcenter()
    }

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

dependencies {
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    testImplementation("org.jetbrains.kotlin:kotlin-test")
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}

application {
    // Define the main class for the application.
    mainClassName = "com.demo.AppKt"
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

tasks.test {
    useJUnit()
    useJUnitPlatform()
}

But running gradle run from the root directory gives me this error:

Error: Could not find or load main class com.demo.AppKt
Caused by: java.lang.ClassNotFoundException: com.demo.AppKt

As far I have seen various other answers related to questions of mine, I have set up the main class in build.gradle with no error. Is there anything I'm missing or I am doing wrong? Any help will be appreciated. Thanks in advance!

Edit: There was a typo in package name

Upvotes: 3

Views: 1396

Answers (1)

Dmitrii B
Dmitrii B

Reputation: 2860

you have the wrong path to the main class. In your directory description, I see path com.demo.App.kt

Try to change from mainClassName = "com.isdb.AppKt" to mainClassName = "com.demo.AppKt"

Upvotes: 1

Related Questions