Embedded scripting warning

Hi, I am running into some problems while trying to evaluate my embedded scripts.

I am receiving the following error message while evaluating the script:

WARN: Failed to initialize native filesystem for Windows
java.lang.RuntimeException: Could not find installation home path. Please make sure bin/idea.properties is present in the installation directory.
at com.intellij.openapi.application.PathManager.getHomePath(PathManager.java:82)
at com.intellij.openapi.application.PathManager.findBinFile(PathManager.java:161)
at com.intellij.util.lang.UrlClassLoader.loadPlatformLibrary(UrlClassLoader.java:321)
at com.intellij.openapi.util.io.win32.IdeaWin32.<clinit>(IdeaWin32.java:48)
at com.intellij.openapi.util.io.FileSystemUtil.getMediator(FileSystemUtil.java:62)
at com.intellij.openapi.util.io.FileSystemUtil.<clinit>(FileSystemUtil.java:54)
at com.intellij.openapi.vfs.impl.ZipHandler.setFileAttributes(ZipHandler.java:61)
at com.intellij.openapi.vfs.impl.ZipHandler$1.createAccessor(ZipHandler.java:43)
at com.intellij.openapi.vfs.impl.ZipHandler$1.createAccessor(ZipHandler.java:39)
at com.intellij.util.io.FileAccessorCache.createHandle(FileAccessorCache.java:60)
at com.intellij.util.io.FileAccessorCache.get(FileAccessorCache.java:52)
at com.intellij.openapi.vfs.impl.ZipHandler.getCachedZipFileHandle(ZipHandler.java:83)
at com.intellij.openapi.vfs.impl.ZipHandler.acquireZipHandle(ZipHandler.java:129)
at com.intellij.openapi.vfs.impl.ZipHandlerBase.createEntriesMap(ZipHandlerBase.java:44)
at com.intellij.openapi.vfs.impl.ArchiveHandler.getEntriesMap(ArchiveHandler.java:197)
at com.intellij.openapi.vfs.impl.jar.CoreJarHandler.<init>(CoreJarHandler.java:42)
at com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem.lambda$new$0(CoreJarFileSystem.java:33)
at com.intellij.util.containers.ConcurrentFactoryMap$4.create(ConcurrentFactoryMap.java:224)
at com.intellij.util.containers.ConcurrentFactoryMap.get(ConcurrentFactoryMap.java:63)
at com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem.findFileByPath(CoreJarFileSystem.java:44)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.findJarRoot(KotlinCoreEnvironment.kt:398)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.contentRootToVirtualFile(KotlinCoreEnvironment.kt:377)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.access$contentRootToVirtualFile(KotlinCoreEnvironment.kt:117)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$5.invoke(KotlinCoreEnvironment.kt:255)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$5.invoke(KotlinCoreEnvironment.kt:117)
at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.convertClasspathRoots(ClasspathRootsResolver.kt:70)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:262)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:117)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(KotlinCoreEnvironment.kt:446)
at kotlin.script.experimental.jvmhost.impl.KJVMCompilerImpl.compile(KJVMCompilerImpl.kt:138)
at kotlin.script.experimental.jvm.JvmScriptCompiler.compile$suspendImpl(jvmScriptCompilation.kt:43)
at kotlin.script.experimental.jvm.JvmScriptCompiler.compile(jvmScriptCompilation.kt)
at kotlin.script.experimental.host.BasicScriptingHost$eval$1.doResume(BasicScriptingHost.kt:38)
at kotlin.script.experimental.host.BasicScriptingHost$eval$1.invoke(BasicScriptingHost.kt)
at kotlin.script.experimental.host.BasicScriptingHost$eval$1.invoke(BasicScriptingHost.kt:25)
at kotlin.script.experimental.host.BasicScriptingHost$runInCoroutineContext$1.doResume(BasicScriptingHost.kt:30)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:42)
at kotlinx.coroutines.experimental.DispatchTask.run(CoroutineDispatcher.kt:129)
at kotlinx.coroutines.experimental.EventLoopBase.processNextEvent(EventLoop.kt:147)
at kotlinx.coroutines.experimental.BlockingCoroutine.joinBlocking(Builders.kt:236)
at kotlinx.coroutines.experimental.BuildersKt.runBlocking(Builders.kt:174)
at kotlinx.coroutines.experimental.BuildersKt.runBlocking$default(Builders.kt:167)
at kotlin.script.experimental.host.BasicScriptingHost.runInCoroutineContext(BasicScriptingHost.kt:30)
at kotlin.script.experimental.host.BasicScriptingHost.eval(BasicScriptingHost.kt:37)
at domain.ScriptHostKt.evalFile(ScriptHost.kt:34)

My script definition looks like this:

@KotlinScript
@KotlinScriptEvaluator(BasicJvmScriptEvaluator::class)
abstract class ContentScript {
    abstract fun body(vararg args: String): Int
}

Script:

println("Hello world!")

Script host (from the example in the kotlin respository):

val myJvmConfigParams = jvmJavaHomeParams + with(ScriptCompileConfigurationProperties) {
    listOf(
        baseClass<ContentScript>(),
        dependencies(JvmDependency(scriptCompilationClasspathFromContext("server")))
    )
}

fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
    val scriptCompiler = JvmScriptCompiler(KJVMCompilerImpl(), DummyCompiledJvmScriptCache())
    val scriptDefinition = ScriptDefinitionFromAnnotatedBaseClass(
        ScriptingEnvironment(
            ScriptingEnvironmentProperties.baseClass<ContentScript>(),
            ScriptingEnvironmentProperties.getScriptingClass(JvmGetScriptingClass())
        )
    )

    val host = JvmBasicScriptingHost(
        scriptDefinition.compilationConfigurator,
        scriptCompiler,
        scriptDefinition.evaluator
    )

    return host.eval(scriptFile.toScriptSource(), ScriptCompileConfiguration(myJvmConfigParams), ScriptEvaluationEnvironment())
}

The evaluation call which causes the error:

evalFile(File("domain/ExampleScript.kts"))

Dependencies:

compile group: 'org.jetbrains.kotlin', name: 'kotlin-scripting-common', version: '1.2.60'
compile group: 'org.jetbrains.kotlin', name: 'kotlin-scripting-jvm', version: '1.2.60'
compile group: 'org.jetbrains.kotlin', name: 'kotlin-scripting-jvm-host', version: '1.2.60'
compile group: 'org.jetbrains.kotlin', name: 'kotlin-scripting-misc', version: '1.2.60'
compile "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.21"
runtime group: 'org.jetbrains.kotlin', name: 'kotlin-compiler', version: '1.2.60'

When I remove the kotlin-compiler dependency I receive a different error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/jetbrains/kotlin/cli/common/messages/MessageCollector
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at kotlin.script.experimental.jvmhost.impl.KJVMCompilerImpl.compile(KJVMCompilerImpl.kt:78)
at kotlin.script.experimental.jvm.JvmScriptCompiler.compile$suspendImpl(jvmScriptCompilation.kt:43)
at kotlin.script.experimental.jvm.JvmScriptCompiler.compile(jvmScriptCompilation.kt)
at kotlin.script.experimental.host.BasicScriptingHost$eval$1.doResume(BasicScriptingHost.kt:38)
at kotlin.script.experimental.host.BasicScriptingHost$eval$1.invoke(BasicScriptingHost.kt)
at kotlin.script.experimental.host.BasicScriptingHost$eval$1.invoke(BasicScriptingHost.kt:25)
at kotlin.script.experimental.host.BasicScriptingHost$runInCoroutineContext$1.doResume(BasicScriptingHost.kt:30)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:42)
at kotlinx.coroutines.experimental.DispatchTask.run(CoroutineDispatcher.kt:129)
at kotlinx.coroutines.experimental.EventLoopBase.processNextEvent(EventLoop.kt:147)
at kotlinx.coroutines.experimental.BlockingCoroutine.joinBlocking(Builders.kt:236)
at kotlinx.coroutines.experimental.BuildersKt.runBlocking(Builders.kt:174)
at kotlinx.coroutines.experimental.BuildersKt.runBlocking$default(Builders.kt:167)
at kotlin.script.experimental.host.BasicScriptingHost.runInCoroutineContext(BasicScriptingHost.kt:30)
at kotlin.script.experimental.host.BasicScriptingHost.eval(BasicScriptingHost.kt:37)
at domain.ScriptHostKt.evalFile(ScriptHost.kt:34)
at domain.main(Main.kt:10)
Caused by: java.lang.ClassNotFoundException: org.jetbrains.kotlin.cli.common.messages.MessageCollector
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 29 more

I’ve added the dicovery file. I don’t really understand these errors, I’ve been trying to implement embedded scripting by replicating the examples on the kotlin repository with no success. If anybody can provide some insight that would be appreciated.

JDK version: 1.8.172

Kind regards

Is it really an error for you? This is, in fact, an overly talkative warning. Should be disabled on our side, of course, but for a moment, you can try to pass something like “-Didea.io.use.fallback=true” to your JVM.
But I guess that something else is wrong as well…

I have solved the problem by using kotlin-compiler-embeddable instead of kotlin-compiler. The error is gone but the scripts still don’t execute. In the script diagnostic I have found the following exception:

java.lang.NoClassDefFoundError: com/intellij/openapi/util/Disposer

After adding:

    compile group: 'com.intellij', name: 'openapi', version: '7.0.3'

To my dependencies it produces the following exception:

java.lang.NoSuchMethodError: com.intellij.openapi.util.Disposer.newDisposable()Lcom/intellij/openapi/Disposable

This version of openapi seems a little bit outdated (2008) but I can’t seem to find a more recent version.

This is not a solution, I guess it simply fails earlier. You shouldn’t use the embeddable compiler here.

(The unimportant details: the exceptions you’re getting pointing to the classes that are relocated in the embeddable compiler. To make it run, you’ll need to build the scripting infrastructure with the same relocation. We’re not (yet) providing embeddable versions of these.)

So I suggest you revert to the regular kotlin-compiler jar, and first of all check that it is really an error/exception you’re getting, and then try to disable it using idea.io.use.fallback property.

Thanks ilya, this seem to have worked. I am getting a more sensible output now:

Message: No class roots are found in the JDK path: C:\Program Files\Java\jdk1.8.0_172\jre
Message: Cannot access script base class 'domain.ContentScript'. Check your module classpath for missing or conflicting dependencies

I don’t really know what goes wrong though. Does it not recognise the discovery file?

Another thing to try - comment out the body function in the ContentScript. (The reason is that generating script into body is not implemented yet, so it was probably too early to put it into examples, albeit in a comment.)

And make sure that the ContentScript itself, and all its dependencies are part of the compilation classpath (dependencies(JvmDependency(... part of the configuration). There is no easy way to do it now, but you can try to debug it: set a breakpoint in the KJVMCompilerImpl.compile and check that scriptCompileConfiguration contains all the dependencies the script needs.

Confirm your JDK path is really " C:\Program Files\Java\jdk1.8.0_172\jre"? If not , you can set java home in vm options, like this: “-Djava.home=C:\DevTools\Java\jdk1.8.0_73”