Skip to content

Commit ebe1989

Browse files
Fix: Resolve ClassNotFoundException for KTS scripts
This commit fixes a ClassNotFoundException that occurred when running KTS scripts. The issue was caused by your script class not being packaged into the scriplet.jar during compilation. The fix involves combining your script code and the wrapper code into a single string before compilation. This ensures that kotlinc treats them as a single compilation unit and packages all necessary classes into the resulting JAR. Specifically, the changes are: - In `JarArtifactCreator.kt`: - For KTS scripts, your script content and the generated wrapper content are now concatenated into a single string. - This combined string is written to a single temporary .kt file, which is then passed to the Kotlin compiler. - In `Templates.kt`: - `createWrapperForScript` now ensures the wrapper code has the correct package declaration, consistent with your script. This approach should resolve the ClassNotFoundException and allow KTS scripts to be compiled and executed correctly.
1 parent 1507273 commit ebe1989

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/main/kotlin/io/github/kscripting/kscript/code/Templates.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ object Templates {
4848
fun createWrapperForScript(packageName: PackageName, className: String): String {
4949
val classReference = packageName.value + "." + className
5050

51-
return """
51+
var wrapperTemplate = """
5252
|class Main_${className}{
5353
| companion object {
5454
| @JvmStatic
@@ -58,6 +58,12 @@ object Templates {
5858
| }
5959
| }
6060
|}""".trimStart().trimMargin()
61+
62+
if (packageName.value.isNotBlank()) {
63+
wrapperTemplate = "package ${packageName.value};\n\n$wrapperTemplate"
64+
}
65+
66+
return wrapperTemplate
6167
}
6268

6369
fun createRunConfig(rootScriptName: String, rootScriptType: ScriptType, userArgs: List<String>): String {

src/main/kotlin/io/github/kscripting/kscript/creator/JarArtifactCreator.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,20 @@ class JarArtifactCreator(private val executor: Executor) {
4343
scriptContent = "package ${script.packageName.value}\n\n$scriptContent"
4444
}
4545

46-
FileUtils.createFile(scriptFile, scriptContent)
47-
4846
val filesToCompile = mutableSetOf<OsPath>()
49-
filesToCompile.add(scriptFile)
5047

51-
// create main-wrapper for kts scripts
5248
if (script.scriptLocation.scriptType == ScriptType.KTS) {
53-
val wrapper = FileUtils.createFile(
54-
basePath.resolve("$execClassName.kt"), Templates.createWrapperForScript(script.packageName, className)
55-
)
56-
filesToCompile.add(wrapper)
49+
// For KTS scripts, combine script content and wrapper code into a single file.
50+
// The package declaration is handled by Templates.createWrapperForScript.
51+
val wrapperContent = Templates.createWrapperForScript(script.packageName, className)
52+
scriptContent = "$scriptContent\n\n$wrapperContent"
53+
54+
FileUtils.createFile(scriptFile, scriptContent)
55+
filesToCompile.add(scriptFile)
56+
} else {
57+
// For KT files, keep the existing logic.
58+
FileUtils.createFile(scriptFile, scriptContent)
59+
filesToCompile.add(scriptFile)
5760
}
5861

5962
executor.compileKotlin(

0 commit comments

Comments
 (0)