Is there a way to get the runtimeClasspath without...
# scripting
Is there a way to get the runtimeClasspath without the gradle API’s? I am adding it to the ScriptEngine - templateClasspath. Any help is welcomed. Right now I am collecting it here, but it doesn’t work consistently:
Copy code
val classpath: List<File>
  get() = ((Thread.currentThread().contextClassLoader as URLClassLoader).urLs ?: emptyArray<URL>()).toList()
    .filterNot { it.path.contains("Java/JavaVirtualMachines") }
    .map { Paths.get(it.toURI()).toFile() }
Any help is welcomed, how to add
to the classpath of an ScriptEngine. Usually if one creates a ScriptEngineManager with a classloader the classpath get’s added automatically to the ScriptEngines, but that is not the case here
I'm not sure I understand your problem correctly. But Kotlin JSR-223 engine tries to extract all paths to jars from the classloaders and add them to the script compilation classpath - in the linked example the parameter
wholeClasspath = true
instruct it to use whole extracted classpath unfiltered. So, if it doesn't work for you, could you please elaborate. Anyway, I'd suggest to use the new JSR-223 implementation - - in additional to other things, it has more reliable classpath extraction.
Yes I understand what it tries to do, but the problem is that it does not add the jars from my runtimeClasspath If I run a snippet. It is not aware of classes and members of a given project. Or at least I don’t know how to add it. Rn I am excluding them in this example: and I am looking for a way to run a Snippet, which is defined as a String with either JSR-233 or the scripting API’s. Any help is welcome. It appears that using the scripting API’s is recommended, but I don’t know how to add the runtimeClasspath with this function: I hope this is a bit more expressive.
@ilya.chernikov Do you know what might cause
ERROR USE_FIR: java.lang.NoSuchFieldError: USE_FIR
I defined this function:
Copy code
fun evaluate(snip: String, classpath: List<File> = empty()): ResultWithDiagnostics<EvaluationResult> =
    script = snip.toScriptSource(),
    compilationConfiguration = createJvmCompilationConfigurationFromTemplate<ScriptUtils> {
      jvm {
          scriptCompilationClasspathFromContext(wholeClasspath = true).plus(classpath).distinct()
    evaluationConfiguration = ScriptEvaluationConfiguration {
it works fine in a main function, but doesnt in my project. Am I missing any dependencies?
Your usage of the JSR-223 looks correct, provided that you want only jars with names containing
in your classpath. If so, I don't know why it doesn't work for you just looking at the code, some debugging is needed. If curios, you can step into the
constructor and check whether the passed classpath is the one you expect.
The JSR-223 is in fact a REPL, and therefore is preferable if you need a REPL or you want to use a standard API. Otherwise the Kotlin Scripting API will provide you more control.
If you decide to stay with JSR-223, I'd recommend to use the new implementation - as I wrote above -
, and you can have a look at this test - - to see how importing symbols from the classloader works.
If you want to switch to the scripting API, then adding static dependencies (the one known before script compilation) is as easy as adding
Copy code
right after
call in the host example you linked above. Or even instead of
, if you want to control your dependencies explicitly.