File tree Expand file tree Collapse file tree 3 files changed +18
-7
lines changed
application/src/main/kotlin/de/micromata/kotlinscripting Expand file tree Collapse file tree 3 files changed +18
-7
lines changed Original file line number Diff line number Diff line change @@ -30,3 +30,5 @@ Just run:
30
30
31
31
1. `./gradlew clean build`
32
32
2. `java -jar application/build/libs/application-0.0.1.jar`
33
+ 3. See the output in the console.
34
+ 4. Use CTRL+C to stop the application.
Original file line number Diff line number Diff line change @@ -2,6 +2,8 @@ package de.micromata.kotlinscripting
2
2
3
3
import org.springframework.boot.autoconfigure.SpringBootApplication
4
4
import org.springframework.boot.runApplication
5
+ import kotlin.script.experimental.api.ResultWithDiagnostics
6
+ import kotlin.script.experimental.api.valueOrNull
5
7
6
8
@SpringBootApplication
7
9
class DemoApplication {
@@ -13,7 +15,14 @@ class DemoApplication {
13
15
14
16
val scriptExecutor = ScriptExecutor ()
15
17
val result = scriptExecutor.executeScript()
16
- println (" Script result: $result " )
18
+ if (result is ResultWithDiagnostics <* >) {
19
+ println (" Script result: ${result.valueOrNull()} " )
20
+ result.reports.forEach {
21
+ println (" Script report: ${it.message} " )
22
+ }
23
+ } else {
24
+ println (" Script result: $result " )
25
+ }
17
26
}
18
27
}
19
28
}
Original file line number Diff line number Diff line change @@ -62,16 +62,16 @@ class ScriptExecutor {
62
62
63
63
companion object {
64
64
val script = """
65
- val sb = StringBuilder()
66
- sb.appendln( "Hello world!")
65
+ // Don't use StringBuilder here, because it's might not be available in the script classpath.
66
+ var result = "Hello world!\n"
67
67
var loader = Thread.currentThread().contextClassLoader
68
68
while (loader != null) {
69
- sb.appendln( "ClassLoader: ${' $' } loader")
69
+ result += "ClassLoader: ${' $' } loader\n"
70
70
loader = loader.parent
71
71
}
72
- sb.appendln( "Classpath: ${' $' } {System.getProperty("java.class.path")}")
73
- //sb.appendln (de.micromata.springbootkotlinscripting.ScriptExecutor.script)
74
- sb.toString()
72
+ result += "Classpath: ${' $' } {System.getProperty("java.class.path")}\n"
73
+ //sb.appendLine (de.micromata.springbootkotlinscripting.ScriptExecutor.script)
74
+ result
75
75
""" .trimIndent()
76
76
}
77
77
}
You can’t perform that action at this time.
0 commit comments