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

classpath-utilities.mddocs/

0

# Classpath Utilities (Deprecated)

1

2

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.

3

4

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

5

6

## Classpath Discovery Functions

7

8

### classpathFromClassloader

9

10

Extracts classpath entries from a ClassLoader.

11

12

```kotlin { .api }

13

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")

14

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

15

```

16

17

**Parameters:**

18

- `classLoader: ClassLoader` - The ClassLoader to extract classpath from

19

20

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

21

22

**Usage Example:**

23

24

```kotlin

25

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

26

27

val classLoader = Thread.currentThread().contextClassLoader

28

val classpath = classpathFromClassloader(classLoader)

29

30

classpath?.forEach { file ->

31

println("Classpath entry: ${file.absolutePath}")

32

}

33

```

34

35

### classpathFromClasspathProperty

36

37

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

38

39

```kotlin { .api }

40

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")

41

fun classpathFromClasspathProperty(): List<File>?

42

```

43

44

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

45

46

**Usage Example:**

47

48

```kotlin

49

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

50

51

val systemClasspath = classpathFromClasspathProperty()

52

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

53

```

54

55

### classpathFromClass

56

57

Finds classpath entries containing a specific class.

58

59

```kotlin { .api }

60

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")

61

fun classpathFromClass(

62

classLoader: ClassLoader,

63

klass: KClass<out Any>

64

): List<File>?

65

```

66

67

**Parameters:**

68

- `classLoader: ClassLoader` - The ClassLoader to search in

69

- `klass: KClass<out Any>` - The class to locate in the classpath

70

71

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

72

73

**Usage Example:**

74

75

```kotlin

76

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

77

import kotlin.script.templates.ScriptTemplateDefinition

78

79

val classLoader = Thread.currentThread().contextClassLoader

80

val classpath = classpathFromClass(classLoader, ScriptTemplateDefinition::class)

81

82

classpath?.forEach { file ->

83

println("Found ScriptTemplateDefinition in: ${file.absolutePath}")

84

}

85

```

86

87

### classpathFromFQN

88

89

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

90

91

```kotlin { .api }

92

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")

93

fun classpathFromFQN(

94

classLoader: ClassLoader,

95

fqn: String

96

): List<File>?

97

```

98

99

**Parameters:**

100

- `classLoader: ClassLoader` - The ClassLoader to search in

101

- `fqn: String` - Fully qualified class name to locate

102

103

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

104

105

**Usage Example:**

106

107

```kotlin

108

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

109

110

val classLoader = Thread.currentThread().contextClassLoader

111

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

112

113

if (classpath != null) {

114

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

115

}

116

```

117

118

## Script Compilation Classpath Functions

119

120

### scriptCompilationClasspathFromContext

121

122

Resolves classpath for script compilation from the current context.

123

124

```kotlin { .api }

125

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")

126

fun scriptCompilationClasspathFromContext(

127

vararg keyNames: String,

128

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

129

wholeClasspath: Boolean = false

130

): List<File>

131

```

132

133

**Parameters:**

134

- `keyNames: String` - Names of JAR files or classpath keys to search for

135

- `classLoader: ClassLoader` - ClassLoader to search in (defaults to current thread's context ClassLoader)

136

- `wholeClasspath: Boolean` - Whether to return the entire classpath or just matching entries

137

138

**Returns:** List of classpath File entries for script compilation

139

140

**Usage Example:**

141

142

```kotlin

143

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

144

145

// Get compilation classpath for kotlin-script-util

146

val classpath = scriptCompilationClasspathFromContext(

147

"kotlin-script-util.jar",

148

"kotlin-stdlib.jar",

149

wholeClasspath = true

150

)

151

152

println("Script compilation classpath:")

153

classpath.forEach { file ->

154

println(" ${file.name}")

155

}

156

```

157

158

### scriptCompilationClasspathFromContextOrNull

159

160

Safe version that returns null instead of throwing exceptions.

161

162

```kotlin { .api }

163

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")

164

fun scriptCompilationClasspathFromContextOrNull(

165

vararg keyNames: String,

166

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

167

wholeClasspath: Boolean = false

168

): List<File>?

169

```

170

171

**Parameters:** Same as `scriptCompilationClasspathFromContext`

172

173

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

174

175

**Usage Example:**

176

177

```kotlin

178

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

179

180

val classpath = scriptCompilationClasspathFromContextOrNull("kotlin-script-util.jar")

181

if (classpath != null) {

182

println("Successfully resolved ${classpath.size} classpath entries")

183

} else {

184

println("Failed to resolve script compilation classpath")

185

}

186

```

187

188

### scriptCompilationClasspathFromContextOrStdlib

189

190

Fallback version that returns stdlib classpath if context resolution fails.

191

192

```kotlin { .api }

193

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")

194

fun scriptCompilationClasspathFromContextOrStdlib(

195

vararg keyNames: String,

196

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

197

wholeClasspath: Boolean = false

198

): List<File>

199

```

200

201

**Parameters:** Same as `scriptCompilationClasspathFromContext`

202

203

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

204

205

**Usage Example:**

206

207

```kotlin

208

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

209

210

// Always returns a valid classpath, falling back to stdlib if needed

211

val classpath = scriptCompilationClasspathFromContextOrStdlib("kotlin-script-util.jar")

212

println("Got ${classpath.size} classpath entries (including fallback if needed)")

213

```

214

215

## File Extension Functions

216

217

### File.matchMaybeVersionedFile

218

219

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

220

221

```kotlin { .api }

222

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")

223

fun File.matchMaybeVersionedFile(baseName: String): Boolean

224

```

225

226

**Parameters:**

227

- `baseName: String` - Base name to match against

228

229

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

230

231

**Usage Example:**

232

233

```kotlin

234

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

235

236

val kotlinStdlib = File("/path/to/kotlin-stdlib-1.8.22.jar")

237

val matches = kotlinStdlib.matchMaybeVersionedFile("kotlin-stdlib")

238

println("Matches kotlin-stdlib: $matches") // true

239

```

240

241

### File.hasParentNamed

242

243

Checks if any parent directory has the specified name.

244

245

```kotlin { .api }

246

@Deprecated("Use the function from kotlin.script.experimental.jvm.util")

247

fun File.hasParentNamed(baseName: String): Boolean

248

```

249

250

**Parameters:**

251

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

252

253

**Returns:** true if any parent directory has the specified name

254

255

**Usage Example:**

256

257

```kotlin

258

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

259

260

val file = File("/home/user/projects/kotlin-project/src/Main.kt")

261

val inKotlinProject = file.hasParentNamed("kotlin-project")

262

println("File is in kotlin-project: $inKotlinProject") // true

263

```

264

265

## Kotlin Jars Object

266

267

### KotlinJars

268

269

Deprecated object that provides access to Kotlin compiler JARs.

270

271

```kotlin { .api }

272

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

273

val KotlinJars = kotlin.script.experimental.jvm.util.KotlinJars

274

```

275

276

**Usage Example:**

277

278

```kotlin

279

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

280

281

// Access compiler classpath (redirects to new implementation)

282

val compilerClasspath = KotlinJars.compilerWithScriptingClasspath

283

println("Compiler classpath has ${compilerClasspath.size} entries")

284

```

285

286

## Migration Notes

287

288

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

289

290

1. Replace imports:

291

```kotlin

292

// Old

293

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

294

295

// New

296

import kotlin.script.experimental.jvm.util.classpathFromClassloader

297

```

298

299

2. Function signatures remain the same, so no code changes are needed beyond imports

300

301

3. The new implementations provide better performance and additional features

302

303

## Types

304

305

```kotlin { .api }

306

// Standard Java types used by classpath utilities

307

typealias ClassLoader = java.lang.ClassLoader

308

typealias File = java.io.File

309

310

// Kotlin reflection types

311

import kotlin.reflect.KClass

312

```