How to create executable file?

I’ve tried three ways:


  1. The kotlinc hello.kt -include-runtime -d hello.jar way didn’t work.
Output

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.intellij.util.ReflectionUtil to method java.util.ResourceBundle.setParent(java.util.ResourceBundle)
WARNING: Please consider reporting this to the maintainers of com.intellij.util.ReflectionUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
main.kt:1:12: error: unresolved reference: jsoup


  1. After tried this I’ve tried to add this code into build.gradle.kts, it didn’t work.
Code and error
jar {
   manifest {
       attributes 'Main-Class': 'HelloWorldKt'
   }
   from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

Gives error:

build.gradle.kts:35:20: Unexpected tokens (use ‘;’ to separate expressions on the same line)


  1. Finally, I’ve tried to build with Gradle → Build → jar it didn’t work either. Actually last way builds jar file but after double click, it doesn’t write text into txt file.

How can I create executable file?

I solved like this:

  1. If you only need a working jar file, you can build a fatJar containing all the libs needed. Add this to your gradle build:
    task fatJar(type: Jar) {
        baseName 'your_preferred_package_name'

        manifest {
            attributes "Main-Class": "EngineKt"
        }
        from {
            configurations.runtimeClasspath.collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }

        with jar
    }

It’s groovy though, not kotlin script. I’ve also been suggested on this so I would not really know how to port it to kts. If you want the full conversation I had on reddit, here it is: https://www.reddit.com/r/Kotlin/comments/lkzwvn/a_suggestion_about_tornadofx/

  1. If you want an executable, say .exe or .dmg file, build the fatJar in 1. and then use launch4j to build the exe. You need to be on the machine you want to build it for though, so to build a .exe you need to work with launch4j on a windows machine. Here is Launch4j project: http://launch4j.sourceforge.net/

I’m still learning so this won’t definitely be the best solution, still it’s a viable one.

PS. Credit to poralexc for the best help ever on this :smiley:

1 Like

Thanks for advices, I forgot to edit. I’ve solved.

Good job!

1 Like

Thanks. Although I’ve done still don’t understand why I got error when I try this code via cmd:
kotlinc hello.kt -include-runtime -d hello.jar

I think there is a bug in the current version of kotlin combined with jvm 16. Not sure and I can’t find the youtrack issue right now. In general this should work.

I’m using 1.8.

Snippet 1 output doesn’t look like Java 8, it’s a warning which exists only on Java 9+, it’s still a problem of Kotlin compiler, but it’s a warning, not an error, see more details and workaround in this issue KT-43704

Though, it doesn’t work not because of this warning, see last line:

main.kt:1:12: error: unresolved reference: jsoup

So it looks that you references jsoup, in your code, which is not a part of Kotlin or Java standard classpath, you can add it manually to classpath when run your jar.

About snippet 2, better to use standard Gradle application plugin (not a big deal, but at least it’s official support) and Gradle Shadow plugin which can create fat jar for you and it also has integration with application plugin, both plugins have good documentation with snippets

1 Like

As an alternative to launch4j, you might consider Java’s jpackage tool. See

Or jlink if you just want a minimal jvm for your project. But it is really painful when you use libs that don’t have a module-info (and not many have that)… But once you got it to work it is really excellent.