Hi,
I am new to Kotlin. We are planning on using kotlin to run pluggable scripts, so I am trying to compile kotlin scripts in java and add to the available classes.
My java code puts the script text to a file and runs K2JVMCompiler.
For testing I am trying to compile this script:
class HelloWorld {
fun sayHello(inp : String) : String { return "Hello from dynamic Kotlin!" + inp; }
}
in java I run this:
private static final K2JVMCompiler KOTLIN_COMPILER = new K2JVMCompiler();
File kotlinSourceFile = new File(className + “.kt”);kotlinSourceFile.delete();Files.write(kotlinSourceFile.toPath(), code.getBytes());
ExitCode exitCode = KOTLIN_COMPILER.exec(System.out, kotlinSourceFile.getAbsolutePath(), “-d”, className + “.jar”);
which gives me this warning/error:
warning: unable to find kotlin-stdlib.jar in the Kotlin home directory. Pass either ‘-no-stdlib’ to prevent adding it to the classpath, or the correct ‘-kotlin-home’
warning: unable to find kotlin-script-runtime.jar in the Kotlin home directory. Pass either ‘-no-stdlib’ to prevent adding it to the classpath, or the correct ‘-kotlin-home’
warning: unable to find kotlin-reflect.jar in the Kotlin home directory. Pass either ‘-no-reflect’ or ‘-no-stdlib’ to prevent adding it to the classpath, or the correct ‘-kotlin-home’
HelloWorld.kt:1:1: error: cannot access built-in declaration ‘kotlin.Any’. Ensure that you have a dependency on the Kotlin standard library.
class HelloWorld
^^^^^^^^^^^^^^^^
HelloWorld.kt:3:79: error: cannot access built-in declaration ‘kotlin.String’. Ensure that you have a dependency on the Kotlin standard library.
fun sayhello(inp : String) : String { return “Hello from dynamic Kotlin!” + inp; }
^
HelloWorld.kt:3:81: error: cannot access built-in declaration ‘kotlin.String’. Ensure that you have a dependency on the Kotlin standard library.
fun sayhello(inp : String) : String { return “Hello from dynamic Kotlin!” + inp; }
^^^
My pom.xml has
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-stdlib -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>2.2.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-script-runtime -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-script-runtime</artifactId>
<version>2.2.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-scripting-common -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-common</artifactId>
<version>2.2.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-scripting-jvm -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-jvm</artifactId>
<version>2.2.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-scripting-dependencies -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-dependencies</artifactId>
<version>2.2.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-compiler-embeddable -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-compiler-embeddable</artifactId>
<version>2.2.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-scripting-compiler-embeddable -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-compiler-embeddable</artifactId>
<version>2.2.10</version>
<!-->runtime</scope-->
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-scripting-jsr223 -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-scripting-jsr223</artifactId>
<version>2.2.10</version>
<!-->runtime</scope-->
</dependency>
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin.jvm/org.jetbrains.kotlin.jvm.gradle.plugin -->
<dependency>
<groupId>org.jetbrains.kotlin.jvm</groupId>
<artifactId>org.jetbrains.kotlin.jvm.gradle.plugin</artifactId>
<version>2.2.10</version>
<type>pom</type>
<!--scope>runtime</scope-->
</dependency>
Any idea on what I need to do to get the compiler to see the appropriate kotlin library/libraries?