3

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?

3 Answers 3

5

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/

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

Also interesting, but for now it is enough to use the kotlin stdlib. Maybe I come back to this approach.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.