I'm having trouble with the jvmRun gradle task. I ...
# multiplatform
z
I'm having trouble with the jvmRun gradle task. I feel like I'm missing something simple, so I'm probably missing something in the lingo. My goal is to simply run the
main
method in the JVM Target, and pass args to it. I've found stackoverflow answers talk about just running
main
, or passing args from the gradle command line to the gradle task, or using the
java
or
application
plugins, or making an executable fatjar, or setting system properties. None of which I feel like should be necessary to just do what's already working (jvmRun) with some args.
This is what I get when I try to run it "normally" in my mind.
Copy code
zymus@abyss:~/github/cultoftheancestormoth$ ./gradlew :cotam:jvmRun -DmainClass=games.studiohummingbird.cultoftheancestormoth.CotamKt

> Task :cotam:jvmRun
Usage: cotam [<options>] <command> [<args>]...

Options:
  -h, --help  Show this message and exit

Commands:
  drudge
  librarian
  adept
  example
  load

BUILD SUCCESSFUL in 1s
6 actionable tasks: 1 executed, 5 up-to-date
And when passing an arg (load)
Copy code
zymus@abyss:~/github/cultoftheancestormoth$ ./gradlew :cotam:jvmRun -DmainClass=games.studiohummingbird.cultoftheancestormoth.CotamKt load

FAILURE: Build failed with an exception.

* What went wrong:
Task 'load' not found in root project 'cultoftheancestormoth' and its subprojects.

* Try:
> Run gradlew tasks to get a list of available tasks.
> For more on name expansion, please refer to <https://docs.gradle.org/8.6/userguide/command_line_interface.html#sec:name_abbreviation> in the Gradle documentation.
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at <https://help.gradle.org>.

BUILD FAILED in 931ms
z
Hmm, that seems to bring me to the same position. It allows be to create an entrypoint to
main
correctly, but I still can't see how to pass any args to that main.
s
I was assuming (but haven't tried) that it works like for Gradle's standard application plugin, so you pass args to the task via
--args="options for main"
task option.
t
In the new dsl you could use
applicationDefaultJvmArgs
to pass arguments for the
main
function
jvmRun
task is mostly needed for the IDE to run random code from the given project
s
So there is no task option provided by
jvmRun
for that purpose?
t
in form of Gradle property - no. Though the task has
args
input to configure run arguments.
s
Yeah, that's what I mean. Not Gradle properties, but Gradle task specific options, see https://docs.gradle.org/current/userguide/custom_tasks.html#sec:declaring_and_using_command_line_options
z
It looks like
--args="load"
worked for me. Using
applicationDefaultJvmArgs
doesn't seem to pass to
main
, as I get the same result as the failure case above. Not working
Copy code
kotlin {
    jvm {
        @OptIn(ExperimentalKotlinGradlePluginApi::class)
        binaries {
            executable(KotlinCompilation.MAIN_COMPILATION_NAME) {
                mainClass.set("games.studiohummingbird.cultoftheancestormoth.CotamKt")
                applicationDefaultJvmArgs.set(listOf("load"))
            }
        }
    }
}
Working
Copy code
zymus@abyss:~/github/cultoftheancestormoth$ ./gradlew :cotam:runJvm -DmainClass=games.studiohummingbird.cultoftheancestormoth.CotamKt --args="load"
t
the error was because JDK could not find "load" class?
yeah, I was wrong -
applicationDefaultJvmArgs
passes arguments to the
java
rather then to the application itself. Though
--args="load"
should also work for
runJvm
task.
z
I believe it's a doing-the-wrong-thing-in-the-wrong-scope issue on my end. Specifically, if my build.gradle.kts contains
Copy code
kotlin {
    jvm {
        @OptIn(ExperimentalKotlinGradlePluginApi::class)
        binaries {
            executable(KotlinCompilation.MAIN_COMPILATION_NAME) {
                mainClass.set("games.studiohummingbird.cultoftheancestormoth.CotamKt")
            }
        }
    }
}
And I run
./gradlew :cotam:runJvm load
It tries to interpret "load" as another gradle command, but there isn't one. It's intent was to be an arg to the
main
pointed to by
:cotam:runJvm
. But running
./gradlew :cotam:runJvm --args="load"
, it does what I need.
t
Generally you should be able to configure returned by
executable()
DSL
JavaExec
task with your arguments. But if you would like to see such an option in the
executable
DSL itself - please open a new Kotlin issue
z
I did try going down a configuring the existing task option, but then gradle started telling me conflicting information. Running
./gradlew tasks
would show
jvmRun
, but trying to use a
tasks.named<JavaExec>("jvmRun")
kept throwing an error that the task didn't exist, so I presumed I was also doing something wrong there.