CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-kotlin--kotlin-script-util

Kotlin scripting support utilities library that provides infrastructure for implementing Kotlin script execution, including JSR-223 script engine implementations, dependency resolution, and utilities for classpath management in scripting contexts

Pending
Overview
Eval results
Files

classpath-utilities.mddocs/

Classpath Utilities (Deprecated)

The kotlin-script-util library provides deprecated classpath management utilities that redirect to newer implementations in the kotlin-scripting-* libraries. These functions help with dynamic classpath resolution during script compilation and execution.

Note: All functions in this module are deprecated and redirect to kotlin.script.experimental.jvm.util. Use the new implementations directly for new code.

Classpath Discovery Functions

classpathFromClassloader

Extracts classpath entries from a ClassLoader.

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")
fun classpathFromClassloader(classLoader: ClassLoader): List<File>?

Parameters:

  • classLoader: ClassLoader - The ClassLoader to extract classpath from

Returns: List of classpath File entries, or null if extraction fails

Usage Example:

import org.jetbrains.kotlin.script.util.classpathFromClassloader

val classLoader = Thread.currentThread().contextClassLoader
val classpath = classpathFromClassloader(classLoader)

classpath?.forEach { file ->
    println("Classpath entry: ${file.absolutePath}")
}

classpathFromClasspathProperty

Extracts classpath entries from the java.class.path system property.

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")
fun classpathFromClasspathProperty(): List<File>?

Returns: List of classpath File entries from system property, or null if not available

Usage Example:

import org.jetbrains.kotlin.script.util.classpathFromClasspathProperty

val systemClasspath = classpathFromClasspathProperty()
println("System classpath has ${systemClasspath?.size ?: 0} entries")

classpathFromClass

Finds classpath entries containing a specific class.

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")
fun classpathFromClass(
    classLoader: ClassLoader, 
    klass: KClass<out Any>
): List<File>?

Parameters:

  • classLoader: ClassLoader - The ClassLoader to search in
  • klass: KClass<out Any> - The class to locate in the classpath

Returns: List of classpath File entries containing the class, or null if not found

Usage Example:

import org.jetbrains.kotlin.script.util.classpathFromClass
import kotlin.script.templates.ScriptTemplateDefinition

val classLoader = Thread.currentThread().contextClassLoader
val classpath = classpathFromClass(classLoader, ScriptTemplateDefinition::class)

classpath?.forEach { file ->
    println("Found ScriptTemplateDefinition in: ${file.absolutePath}")
}

classpathFromFQN

Finds classpath entries containing a class with the specified fully qualified name.

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")
fun classpathFromFQN(
    classLoader: ClassLoader, 
    fqn: String
): List<File>?

Parameters:

  • classLoader: ClassLoader - The ClassLoader to search in
  • fqn: String - Fully qualified class name to locate

Returns: List of classpath File entries containing the class, or null if not found

Usage Example:

import org.jetbrains.kotlin.script.util.classpathFromFQN

val classLoader = Thread.currentThread().contextClassLoader
val classpath = classpathFromFQN(classLoader, "org.jetbrains.kotlin.script.jsr223.KotlinStandardJsr223ScriptTemplate")

if (classpath != null) {
    println("Found kotlin-script-util classes in: ${classpath.joinToString()}")
}

Script Compilation Classpath Functions

scriptCompilationClasspathFromContext

Resolves classpath for script compilation from the current context.

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")
fun scriptCompilationClasspathFromContext(
    vararg keyNames: String,
    classLoader: ClassLoader = Thread.currentThread().contextClassLoader,
    wholeClasspath: Boolean = false
): List<File>

Parameters:

  • keyNames: String - Names of JAR files or classpath keys to search for
  • classLoader: ClassLoader - ClassLoader to search in (defaults to current thread's context ClassLoader)
  • wholeClasspath: Boolean - Whether to return the entire classpath or just matching entries

Returns: List of classpath File entries for script compilation

Usage Example:

import org.jetbrains.kotlin.script.util.scriptCompilationClasspathFromContext

// Get compilation classpath for kotlin-script-util
val classpath = scriptCompilationClasspathFromContext(
    "kotlin-script-util.jar",
    "kotlin-stdlib.jar",
    wholeClasspath = true
)

println("Script compilation classpath:")
classpath.forEach { file ->
    println("  ${file.name}")
}

scriptCompilationClasspathFromContextOrNull

Safe version that returns null instead of throwing exceptions.

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")
fun scriptCompilationClasspathFromContextOrNull(
    vararg keyNames: String,
    classLoader: ClassLoader = Thread.currentThread().contextClassLoader,
    wholeClasspath: Boolean = false
): List<File>?

Parameters: Same as scriptCompilationClasspathFromContext

Returns: List of classpath File entries, or null if resolution fails

Usage Example:

import org.jetbrains.kotlin.script.util.scriptCompilationClasspathFromContextOrNull

val classpath = scriptCompilationClasspathFromContextOrNull("kotlin-script-util.jar")
if (classpath != null) {
    println("Successfully resolved ${classpath.size} classpath entries")
} else {
    println("Failed to resolve script compilation classpath")
}

scriptCompilationClasspathFromContextOrStdlib

Fallback version that returns stdlib classpath if context resolution fails.

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")
fun scriptCompilationClasspathFromContextOrStdlib(
    vararg keyNames: String,
    classLoader: ClassLoader = Thread.currentThread().contextClassLoader,
    wholeClasspath: Boolean = false
): List<File>

Parameters: Same as scriptCompilationClasspathFromContext

Returns: List of classpath File entries, with stdlib as fallback

Usage Example:

import org.jetbrains.kotlin.script.util.scriptCompilationClasspathFromContextOrStdlib

// Always returns a valid classpath, falling back to stdlib if needed
val classpath = scriptCompilationClasspathFromContextOrStdlib("kotlin-script-util.jar")
println("Got ${classpath.size} classpath entries (including fallback if needed)")

File Extension Functions

File.matchMaybeVersionedFile

Checks if a file matches a base name, accounting for version suffixes.

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")
fun File.matchMaybeVersionedFile(baseName: String): Boolean

Parameters:

  • baseName: String - Base name to match against

Returns: true if the file matches the base name (with or without version)

Usage Example:

import org.jetbrains.kotlin.script.util.matchMaybeVersionedFile

val kotlinStdlib = File("/path/to/kotlin-stdlib-1.8.22.jar")
val matches = kotlinStdlib.matchMaybeVersionedFile("kotlin-stdlib")
println("Matches kotlin-stdlib: $matches") // true

File.hasParentNamed

Checks if any parent directory has the specified name.

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")
fun File.hasParentNamed(baseName: String): Boolean

Parameters:

  • baseName: String - Name to search for in parent directories

Returns: true if any parent directory has the specified name

Usage Example:

import org.jetbrains.kotlin.script.util.hasParentNamed

val file = File("/home/user/projects/kotlin-project/src/Main.kt")
val inKotlinProject = file.hasParentNamed("kotlin-project")
println("File is in kotlin-project: $inKotlinProject") // true

Kotlin Jars Object

KotlinJars

Deprecated object that provides access to Kotlin compiler JARs.

@Deprecated("Use the object from kotlin.script.experimental.jvm.util")
val KotlinJars = kotlin.script.experimental.jvm.util.KotlinJars

Usage Example:

import org.jetbrains.kotlin.script.util.KotlinJars

// Access compiler classpath (redirects to new implementation)
val compilerClasspath = KotlinJars.compilerWithScriptingClasspath
println("Compiler classpath has ${compilerClasspath.size} entries")

Migration Notes

All functions in this module are deprecated and redirect to the new implementations in kotlin.script.experimental.jvm.util. When updating code:

  1. Replace imports:

    // Old
    import org.jetbrains.kotlin.script.util.classpathFromClassloader
    
    // New  
    import kotlin.script.experimental.jvm.util.classpathFromClassloader
  2. Function signatures remain the same, so no code changes are needed beyond imports

  3. The new implementations provide better performance and additional features

Types

// Standard Java types used by classpath utilities
typealias ClassLoader = java.lang.ClassLoader
typealias File = java.io.File

// Kotlin reflection types
import kotlin.reflect.KClass

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-script-util

docs

classpath-utilities.md

dependency-resolution.md

index.md

jsr223-engines.md

script-templates.md

tile.json