I'm reading <https://github.com/Kotlin/KEEP/blob/m...
# scripting
l
I'm reading https://github.com/Kotlin/KEEP/blob/master/proposals/scripting-support.md and I wonder if there is a complete example to load a kotlin script from java and/or kotlin project that allow to bind object from application to scripting code
i
I have some test projects, but they are not good enough to serve as a public sample yet. Maybe I’ll manage to do something out of it someday. For a moment you can probably only have a look at the tests, e.g. starting from here https://github.com/JetBrains/kotlin/blob/1.3.0/compiler/tests/org/jetbrains/kotlin/codegen/CustomScriptCodegenTestGenerated.java#L34 and also checking around this place - https://github.com/JetBrains/kotlin/blob/1.3.0/compiler/tests/org/jetbrains/kotlin/codegen/AbstractCustomScriptCodegenTest.kt#L135
l
will have a look, thank you
but then it fails because of:
Copy code
java.lang.NoSuchMethodError: org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(Lcom/intellij/openapi/Disposable;Lorg/jetbrains/kotlin/config/CompilerConfiguration;Lorg/jetbrains/kotlin/cli/jvm/compiler/EnvironmentConfigFiles;)Lorg/jetbrains/kotlin/cli/jvm/compiler/KotlinCoreEnvironment;
i
Please, do not use embeddable compiler for scripting. I will work on some solution for isolation, but for a moment the regular compiler jar should be used. See discussion here https://github.com/Kotlin/KEEP/issues/75#issuecomment-420475780 for details and also the issue https://youtrack.jetbrains.com/issue/KT-27382
l
ok thx, now that error gone away but having
ScriptDiagnostic(message=No class roots are found in the JDK path: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181.b15-5.fc29.x86_64/jre, severity=ERROR, location=null, exception=null)
what does this mean ? sorry for asking but Im quite a newbie on kotlin
i
could it be that your JAVA_HOME env var points to the JRE instead of JDK?(which might not be a problem generally, but maybe we’re processing this case incorrectly.)
l
no the JAVA_HOME points to the jdk
but it appears that the compiler select the jre in any case
this happens running it inside idea or from the comman line (linux) setting the JAVA_HOME to point to the JDK (roacle or openjdk does not make difference)
i
Could you, please, try it with a simple script definition, like this:
Copy code
@KotlinScript
abstract class SimpleScriptTemplate
and the use this class instead of imported one here https://gist.github.com/lburgazzoli/5818679786271f8ea86c5729c9f74ea2#file-scripting-kts-L31
l
how should I use it ?
i
as a type parameter in expression
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<SimpleScriptTemplate>()
l
Now I'm getting
Copy code
ScriptDiagnostic(message=No class roots are found in the JDK path: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181.b15-5.fc29.x86_64/jre, severity=ERROR, location=null, exception=null)
ScriptDiagnostic(message=Cannot access script base class 'org.apache.camel.k.kotlin.NewScriptTest.MyScript'. Check your module classpath for missing or conflicting dependencies, severity=ERROR, location=Location(start=Position(line=2, col=13, absolutePos=null), end=null), exception=null)
when running I get the error reported above, should I rise an issue ?
i
Yes, please, an issue is always helpful.
Btw, does it work for you if you simply use the main-kts sample from our rc blog post?
It seems to me that the problem is not with your sample - it is quite similar to the tests and samples in our code, but some config issue is unexpected for the compiler.
l
yes it does work
done