https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
h

Henri Gourvest

03/15/2024, 7:18 PM
Hello, I can't find a way to create a multiplatform console application with the multiplatform plugin that targets jvm. I can do it with the native platforms, but I don't know how to configure the main class for the jvm. Any ideas?
c

Casey Brooks

03/15/2024, 8:01 PM
To run a JVM console app, you’ll need to create a “fat jar” (or “Shadow Jar”) which bundles all your code with its dependencies into a single file. You then need to execute that fat jar through Java; it can’t be able to be run as a native binary like you would with Native. This Gradle plugin is the standard for creating Fat Jars. When combined with the `application` plugin, you can then run
java -jar ./path/to/consoleApp-all.jar
to execute it. The Ktor gradle plugin configures this plugin for you, and you might find it a bit more convenient to use that instead of directly configuring the Shadow plugin
There are alternatives to running a Jar with Java, such as jpackage or GraalVM, which compile your jar with a distribution of the JRE into a single (usually pretty large) file which can be executed like a native binary. But these can be a bit of a pain to set up. Conveyor is a service which can help with building and packaging JVM console apps, which is free for Open Source apps
e

edrd

03/15/2024, 8:17 PM
Quick note: with GraalVM native-image, it actually compiles the Java code to native, so you don’t have the JRE embedded. It also starts the application orders of magnitude faster
c

Casey Brooks

03/15/2024, 8:21 PM
That’s right, thanks for catching that omission. That AOT compilation also has implications for what you can do with your code, such as the inability to use bytecode manipulation (libraries like Spring AOP for example), and limited support for reflection
👍 1
1
h

Henri Gourvest

03/15/2024, 8:39 PM
thank you for your reply, after testing the ktor plugin, I find that it's not compatible with the organization of a multiplatform project and that no classes are packaged by the ktor plugin.
2 Views