Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import Java packages in Kotlin Native

Tags:

java

kotlin

I'm trying to import Java package for use in Kotlin native as below;

/* main.kt */
import java.util.*

fun main(args: Array<String>) {
    print("Simple program")
}

and compile it with the below command

kotlinc main.kt -o main

I got the error message

main.kt:2:8: error: unresolved reference: java
import java.util.*

obviously I need to show kotlinc where to find java.util, how can I achieve this without using the command line? I'm on Windows 10 64 bit using Kotlin native 0.3.

like image 329
Amani Avatar asked Jun 30 '17 17:06

Amani


2 Answers

Since we can't actually add JVM-Libs to Kotlin/Native, as zsmb13 already explained, i'd like to point at GraalVM (for those who get here and do not already know about it).

https://www.graalvm.org/examples/java-kotlin-aot/

Their approach allows to use java-libs with Kotlin, because it compiles the required lib- and the JVM-classes into the native executable.

like image 187
snv Avatar answered Nov 12 '22 07:11

snv


You won't have access to Java packages in Kotlin Native. The whole point of this project is to run Kotlin code without a VM, so you don't have the Java Virtual Machine (or libraries) to use. You can, however, use native C libraries instead. You can find information about how this works here.

From the announcement post of the first preview of Kotlin/Native:

Note that we do not intend to make arbitrary Kotlin/JVM programs runnable on Kotlin/Native or Kotlin/JS. It would be equivalent to implementing another JVM, which is both a lot of work and a lot of limitations for the runtime. We are going another way: providing a common language for all platforms while enabling creation of common libraries through seamless interoperability with platform code.

like image 28
zsmb13 Avatar answered Nov 12 '22 07:11

zsmb13