Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Java Standard Lib for Kotlin when compiling to Javascript

I have implemented a library in Kotlin for use in the browser. When I compile it to Javascript, the dependencies from the Java standard lib, e.g. collection classes like PriorityQueue, are not found.

Is it possible to convince the compiler to find and compile these classes as well, or is there a precompiled Javascript-Java standard lib, or do I have to use the Kotlin standard lib only?

like image 689
Mark Schäfer Avatar asked May 29 '26 19:05

Mark Schäfer


2 Answers

You have to use the Kotlin standard library only. The Kotlin -> js compiler has no way to turn java libraries (or any jar) into javascript.

I'm not as familiar with the efforts for Kotlin, but the scala.js community has ported many standard java library features to scala.js to ease the transition between the jvm and the web browser. Something similar would need to happen for Kotlin for the specific features you want to use.

You can however reference javascript from kotlin: https://blog.jetbrains.com/kotlin/2014/12/javascript-interop/

like image 129
gregghz Avatar answered Jun 01 '26 09:06

gregghz


You can try TeaVM that compiles bytecode to JavaScript. Simply use kotlinc to get bytecode from Kotlin and run it through TeaVM. You won't be able to use JS interop available from Kotlin2JS (instead, you can use TeaVM's own way to communicate with JavaScript), but you can easily interoperate with Java.

like image 34
Alexey Andreev Avatar answered Jun 01 '26 09:06

Alexey Andreev