Reputation: 984
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?
Upvotes: 3
Views: 1292
Reputation: 6025
[For compiling any Java bytecode to JavaScript,] I HIGHLY recommend you take a look at the JTransc JVM-to-just-about-anything* project (JTransc@Github).
The JTransc Project appears to have a heavy lean towards Kotlin (and also Java and Scala):
Convert your Java, Kotlin and Scala code into JavaScript, C++, D, C#, PHP, AS3, Dart and Haxe and run it everywhere. Also use JVM code in your favourite language as a library.
JTransc Project targets many languages and frameworks, including a LibGDX backend by leveraging Haxe (an old favourite of mine).
JTransc's author, Soywiz (Carlos Ballesteros Velasco) is a genius IMHO (¡Eres un crack, señor!).
(*in my own words)
Mild disclaimer: I am in no way associated with the author of the JTransc project.
Upvotes: 0
Reputation: 2085
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.
Upvotes: 2
Reputation: 3965
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/
Upvotes: 5