or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

classpath-utilities.mddependency-resolution.mdindex.mdjsr223-engines.mdscript-templates.md

index.mddocs/

0

# Kotlin Script Util

1

2

Kotlin Script Util provides essential utilities and infrastructure for Kotlin scripting functionality, including JSR-223 compliant script engines that allow Kotlin scripts to be executed within Java applications, dependency resolvers for managing script dependencies at runtime, and classpath management utilities for dynamic script compilation.

3

4

## Package Information

5

6

- **Package Name**: kotlin-script-util

7

- **Package Type**: maven

8

- **Language**: Kotlin

9

- **Maven Coordinates**: `org.jetbrains.kotlin:kotlin-script-util:1.8.22`

10

- **Installation**: Add to your Maven/Gradle dependencies

11

12

## Core Imports

13

14

```kotlin

15

// JSR-223 Script Engine Factories

16

import org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory

17

import org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory

18

19

// Script Templates

20

import org.jetbrains.kotlin.script.jsr223.KotlinStandardJsr223ScriptTemplate

21

import org.jetbrains.kotlin.script.util.templates.StandardArgsScriptTemplateWithLocalResolving

22

23

// Dependency Annotations (deprecated)

24

import org.jetbrains.kotlin.script.util.DependsOn

25

import org.jetbrains.kotlin.script.util.Repository

26

```

27

28

## Basic Usage

29

30

```kotlin

31

import org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory

32

import javax.script.ScriptEngineManager

33

34

// Create and use a JSR-223 Kotlin script engine

35

val factory = KotlinJsr223JvmLocalScriptEngineFactory()

36

val engine = factory.scriptEngine

37

38

// Execute Kotlin code

39

val result = engine.eval("2 + 2")

40

println(result) // 4

41

42

// Use bindings to pass data to scripts

43

val bindings = engine.createBindings()

44

bindings["x"] = 10

45

bindings["y"] = 20

46

val sum = engine.eval("x + y", bindings)

47

println(sum) // 30

48

```

49

50

## Architecture

51

52

The library is organized into several key components:

53

54

- **JSR-223 Script Engines**: Two implementations for executing Kotlin scripts - local compilation and daemon-based compilation

55

- **Script Templates**: Predefined templates for common scripting scenarios with JSR-223 integration

56

- **Dependency Resolution**: Deprecated utilities for resolving script dependencies (redirects to newer kotlin-scripting libraries)

57

- **Classpath Management**: Deprecated utilities for managing classpaths during script compilation

58

59

## Capabilities

60

61

### JSR-223 Script Engine Support

62

63

Core script engine implementations and factories for executing Kotlin code through the standard Java scripting API.

64

65

```kotlin { .api }

66

class KotlinJsr223JvmLocalScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase {

67

fun getScriptEngine(): ScriptEngine

68

}

69

70

class KotlinJsr223JvmDaemonLocalEvalScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase {

71

fun getScriptEngine(): ScriptEngine

72

}

73

```

74

75

[JSR-223 Script Engines](./jsr223-engines.md)

76

77

### Script Templates

78

79

Predefined templates that provide structured environments for script execution with different parameter passing mechanisms.

80

81

```kotlin { .api }

82

abstract class KotlinStandardJsr223ScriptTemplate(val jsr223Bindings: Bindings) : ScriptTemplateWithBindings {

83

fun eval(script: String, newBindings: Bindings): Any?

84

fun eval(script: String): Any?

85

fun createBindings(): Bindings

86

}

87

```

88

89

[Script Templates](./script-templates.md)

90

91

### Classpath Utilities (Deprecated)

92

93

Legacy utilities for classpath management during script compilation. All functions redirect to newer implementations in kotlin-scripting-* libraries.

94

95

```kotlin { .api }

96

fun classpathFromClassloader(classLoader: ClassLoader): List<File>?

97

fun scriptCompilationClasspathFromContext(

98

vararg keyNames: String,

99

classLoader: ClassLoader = Thread.currentThread().contextClassLoader,

100

wholeClasspath: Boolean = false

101

): List<File>

102

```

103

104

[Classpath Utilities](./classpath-utilities.md)

105

106

### Dependency Resolution (Deprecated)

107

108

Legacy dependency resolution system for scripts with annotation-based dependency declarations.

109

110

```kotlin { .api }

111

class KotlinAnnotatedScriptDependenciesResolver(

112

val baseClassPath: List<File>,

113

resolvers: Iterable<Resolver>

114

) : ScriptDependenciesResolver

115

116

@Target(AnnotationTarget.FILE)

117

@Repeatable

118

annotation class DependsOn(

119

val value: String = "",

120

val groupId: String = "",

121

val artifactId: String = "",

122

val version: String = ""

123

)

124

```

125

126

[Dependency Resolution](./dependency-resolution.md)

127

128

## Types

129

130

```kotlin { .api }

131

// From javax.script package

132

interface ScriptEngine {

133

fun eval(script: String): Any?

134

fun eval(script: String, context: ScriptContext): Any?

135

fun createBindings(): Bindings

136

}

137

138

interface Bindings : MutableMap<String, Any?>

139

140

interface ScriptContext {

141

fun getBindings(scope: Int): Bindings

142

fun setBindings(bindings: Bindings, scope: Int)

143

}

144

```