Hey all, making a multi-module kotlin gradle appli...
# gradle
s
Hey all, making a multi-module kotlin gradle application project for the first time. Just made the project structure for now and tried to run it with
gradle run
but it gave me this error:
Copy code
Error: Could not find or load main class com.demo.AppKt
Caused by: java.lang.ClassNotFoundException: com.demo.AppKt
Here’s the declaration of main class in my gradle file configuration (will be happy to share the whole file as well):
Copy code
application {
    mainClassName = "com.demo.AppKt"
}
And here’s my project structure:
Copy code
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)
Can anyone tell what error am I doing?
v
Best would be to share your whole play project. There are some issues with stuff you mentioned for example but some could also be typos. For example you shouldn't use
gradle
but the Gradle wrapper, if you are using
mainClassName
instead of
mainClass
you are probably using an old Gradle version, the build scripts must end in
.gradle.kts
, not
.gradle.kt
s
Sure, just a second. https://github.com/sangeetds/isdb Here it is. Was in a process of testing something but it didn’t work, so the
build.gradle.kts
file not be as specified above.
And you were right, I was in fact using an older gradle version.
v
Yeah, there is no
AppKt
class anywhere, just as the error says. Btw. setting the main class attribute on the jar is not going to help.
s
As I said I was working on few things so I changed the names
The right name should be
com.demo.DemoApplicationKt
right? Doesn’t work with that too.
Can’t seem to set any main class inside another module. If I bring all the source file outside the module, or if I run
./gradlew run
inside the application module, it works
Also, I thought to use something else so I thought to create a jar file and run it, same problem, main class not found
v
Your problem is a bit differently. I had only looked at the root projects build script as you already applied the
application
plugin there an had there main class set to
AppKt
. You have the
application
plugin applied to your root project and your
application
project. So what happens if you do
gradlew run
is, that all
run
task in all project are executed. It starts with
:run
(the on in the root project) and that fails due to class not found and then the build fails. If you would have done
gradlew --continue run
, then after
:run
failed it would have started
:application:run
as it does not depend on the failed task. So if you want to start the
run
task in the
application
project specifically, use
gradlew application:run
, or actually
gradlew a:r
would be enough in your case. If you remove the
application
plugin from the root project, just doing
gradlew run
is also fine again as then only the
application
project has a
run
task. Or you can of course also apply the
application
plugin to the root project if you there depend on your
application
project and use the correct class name.
s
Sure thanks! Works like a charm. Any general advice so regarding setting up multi modules gradle project from scratch?
v
Do it right 😄
Other than that, the userguide and samples are pretty good generally I'd say
There are many things to keep in mind so hard to recommend some few points
A good gradle build of a bigger project is a project of its own actually
s
Truer words were never spoken
Thanks a lot! Will learn more gradually!