Commit fc3fa1f
committed
Okay, I've attempted to fix the
This involved a series of changes to kscript (version 4.2.3 from your repository) aimed at resolving a persistent `java.lang.ClassNotFoundException` when you run scripts. The core issue I identified is that the compiled class from your script wasn't being included in the generated `scriplet.jar`.
Here's what I found that led to these changes:
1. The `kscript` generated wrapper class (e.g., `Main_Simple_test`) was being compiled without a package declaration, placing it at the JAR root.
2. The wrapper class was trying to load your script's main class using various FQCNs or simple names, none of which were found.
3. Crucially, when I looked inside the `scriplet.jar`, only the wrapper class was present. Your script's compiled class was missing.
4. I confirmed that `kscript` calls `kotlinc` once with two temporary Kotlin files (one for the processed user script, one for the wrapper) to produce the `scriplet.jar`.
Here are the changes I made:
1. In `src/main/kotlin/io/github/kscripting/kscript/code/Templates.kt`:
- I modified `createWrapperForScript` to prepend `package kscript.scriplet;` to the generated `Main_<ScriptName>` wrapper class string.
- I modified the `loadClass` call within this wrapper to target `kscript.scriplet.${className}Kt`, assuming `${className}Kt` is the expected class name from your script (e.g., `Simple_testKt` from `Simple_test.kts`) and that it too will be in the `kscript.scriplet` package.
2. In `src/main/kotlin/io/github/kscripting/kscript/creator/JarArtifactCreator.kt`:
- I modified `create()` so that the `execClassName` for KTS scripts is set to the Fully Qualified Class Name: `kscript.scriplet.Main_${className}`. This ensures the `kotlin` runtime is instructed to run the wrapper from its correct package.
3. Regarding `src/main/kotlin/io/github/kscripting/kscript/util/ScriptUtils.kt`:
- This file is in its original state. Its `resolveCode` function, in conjunction with `ScriptResolver.kt` defaulting the package name to `kscript.scriplet` for package-less scripts, ensures your script *code string* fed to `kotlinc` has `package kscript.scriplet;`.
4. In `src/main/kotlin/io/github/kscripting/kscript/resolver/CommandResolver.kt`:
- This contains diagnostic logging (which I added during debugging) to print the exact code content passed to `kotlinc`. This was vital for understanding the compilation units.
5. I added `simple_test.kts` to your repository root:
- This is a test script with no package declaration, which I used for these debugging attempts.
Current Status & Remaining Issue:
Despite these changes, the `ClassNotFoundException` for `kscript.scriplet.Simple_testKt` persists. My latest test run showed:
- The `KOTLINC_INPUT_CODE` logs confirm both the processed user script and the wrapper script correctly declare `package kscript.scriplet;`.
- The `kotlin` runtime is correctly told to execute `kscript.scriplet.Main_Simple_test`.
- The `scriplet.jar` *now correctly contains* `kscript/scriplet/Main_Simple_test.class`.
- However, the `scriplet.jar` is *still missing* the compiled user script class (e.g., `kscript/scriplet/Simple_testKt.class`).
The final remaining problem seems to be that `kotlinc`, when given the two temporary source files (both declaring `package kscript.scriplet;`), is only packaging the `Main_Simple_test.class` into the JAR and not the class from your script. Further investigation would require understanding why `kotlinc` is omitting this second class or how `kscript` might be mismanaging the temporary files or the `kotlinc` invocation parameters for this multi-file compilation into a single JAR.ClassNotFoundException in kscript.1 parent 59a83e5 commit fc3fa1f
File tree
3 files changed
+31
-13
lines changed- src/main/kotlin/io/github/kscripting/kscript
- code
- creator
3 files changed
+31
-13
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
| 2 | + | |
5 | 3 | | |
6 | 4 | | |
7 | 5 | | |
Lines changed: 11 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
54 | 54 | | |
55 | | - | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
56 | 60 | | |
57 | 61 | | |
58 | 62 | | |
59 | 63 | | |
60 | | - | |
| 64 | + | |
61 | 65 | | |
62 | 66 | | |
63 | 67 | | |
| |||
Lines changed: 18 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
| |||
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
44 | 59 | | |
45 | | - | |
| 60 | + | |
| 61 | + | |
46 | 62 | | |
47 | 63 | | |
48 | 64 | | |
| |||
0 commit comments