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
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)
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.
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
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.
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
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.