1

I'm trying to pass arguments to my Kotlin Multiplatform desktop app.

I have this in my build.gradle.kts:

tasks.withType<JavaExec> { args = listOf("test") } 

This is my Main.kt file:

fun main(args: Array<String>) { println(args.toList()) // ... } 

When I run my app using ./gradlew run, this is printed:

[] 

The run task is of type JavaExec, why are the arguments not being passed then?

I also tried jvmArgs:

tasks.withType<JavaExec> { jvmArgs = listOf("-Dtest=true") } 

and this in my Main.kt:

fun main(args: Array<String>) { println(System.getProperty("test")) // ... } 

But again, when running ./gradlew run, the output is:

null 

UPDATE

I tried using specific name as suggested in the comments:

tasks.named<JavaExec>("run") { args = listOf("test") jvmArgs = listOf("-Dtest=true") } 

but I know get an error:

Task with name 'run' not found in project ':composeApp'. 

I also tried this:

tasks.withType<JavaExec> { println("Exec TASK: $name, $args, $jvmArgs") args = listOf("test") jvmArgs = listOf("-Dtest=true") } 

and this is the output:

Exec TASK: run, [], [] Exec TASK: runRelease, [], [] Exec TASK: desktopRun, [], [] 

So using tasks.withType<JavaExec> does find some tasks, but for some reason still doesn't set their arguments.

1
  • Instead of using tasks.withType<JavaExec>, target the exact run task created by the application plugin: application { mainClass.set("your.package.MainKt") } tasks.named<JavaExec>("run") { args = listOf("test") jvmArgs = listOf("-Dtest=true") } Commented Apr 12 at 1:22

1 Answer 1

0

As said in the official docs, simply add your app arugments in your build.gradle.kts file like this:

compose.desktop { application { // ... args += listOf( "hello", "my second arg" ) } } 

You can also set VM arguments used by the JVM to execute your app using jvmArgs property:

jvmArgs += "--add-opens=java.base/java.nio=ALL-UNNAMED" 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.