Comments (14)
They suggested a workaround and I will release a patched version of kscript
later today or tomorrow.
from kscript.
Thanks for the nice explanation. I've changed it accordingly.
from kscript.
Sorry for inconvenience. What version of kotlin are you using? Just type
kotlinc -version
from kscript.
MBP-MarcinMac:~ marcin$ kotlinc -version
info: Kotlin Compiler version 1.1.0-rc-91
from kscript.
Indeed kscript
is not yet working with 1.1. Unfortunately this seems more an issue with Kotlin itself, and I have filed a ticket for the problem:
https://youtrack.jetbrains.com/issue/KT-16562
Feel welcome to vote for it, because JB is more likely to act upon it if there are some votes.
from kscript.
I've released kscript v1.2 this morning which should solve the problem. You can run the auto-update with
kscript --self-update
Feel welcome if you have other issues when using kscript
from kscript.
I'm getting the same error on Linux:
kscript: 1.2 (15c7cc4)
kotlin: 1.1.1
OS: Linux (Arch)
$ kscript --clear-cache
Cleaning up cache...
$ cat hello.kt
println("Hello, world!")
$ kscript hello.kt
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/script/templates/standard/ScriptTemplateWithArgs
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at Main_Kscript_stdin_e159f6.main(Main_Kscript_stdin_e159f6.java:4)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jetbrains.kotlin.runner.AbstractRunner.run(runners.kt:60)
at org.jetbrains.kotlin.runner.Main.run(Main.kt:104)
at org.jetbrains.kotlin.runner.Main.main(Main.kt:110)
Caused by: java.lang.ClassNotFoundException: kotlin.script.templates.standard.ScriptTemplateWithArgs
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 19 more
kotlinc is installed correctly and working OK.
from kscript.
I see the fix makes use of KOTLIN_HOME
. It's not set on my system, but setting it fixes the problem:
$ KOTLIN_HOME=/usr/share/kotlin kscript hello.kt
Hello, world!
It could be a safe default if it's not defined:
-exec kotlin ${kotlin_opts} -classpath ${jarFile}:${KOTLIN_HOME}/lib/kotlin-script-runtime.jar:"$classpath" Main_${className} "$@"
+exec kotlin ${kotlin_opts} -classpath ${jarFile}:${KOTLIN_HOME:-/usr/share/kotlin}/lib/kotlin-script-runtime.jar:"$classpath" Main_${className} "$@"
Alternatively, you could adopt the same approach as kotlin-script.
Or possibly even scrape it out of kotlinc e.g.:
$ JAVACMD=echo kotlin
-Xmx256M -Xms32M -Dkotlin.home=/usr/share/kotlin -cp /usr/share/kotlin/lib/kotlin-runner.jar org.jetbrains.kotlin.runner.Main
from kscript.
Thanks for the hint.
from kscript.
Thanks, but that particular solution doesn't work on my system:
$ KOTLIN_HOME="$(dirname $(which kotlinc))/.."
$ readlink -f $KOTLIN_HOME
/usr
That means it looks for the JAR in:
$ echo ${KOTLIN_HOME}/lib/kotlin-script-runtime.jar
/usr/lib/kotlin-script-runtime.jar
Rather than where it actualy resides:
/usr/share/kotlin/lib/kotlin-script-runtime.jar
Like I say, the correct path can be exposed by the kotlinc script (or its kotlin wrapper) if you don't mind doing a bit of light scraping:
$ JAVACMD=echo kotlin
-Xmx256M -Xms32M -Dkotlin.home=/usr/share/kotlin -cp /usr/share/kotlin/lib/kotlin-runner.jar org.jetbrains.kotlin.runner.Main
from kscript.
Just out of curiosity. How did kotlin end up under /usr/share/? homebrew? I always use sdkman which just dumps it in a hidden directory in the user home.
I'd think it would be better to avoid another JVM launch (which costs another few hundred milliseconds). Especially for small inlined command-line tools this may make a difference for some users.
So what about (see also latest commit):
if [[ $OSTYPE != darwin* ]]; then minusFarg="-f"; fi
abspath() { echo "$(readlink ${minusFarg} $1)"; }
ll $(dirname $(abspath $(which kotlinc)))/../lib/kotlin-script-runtime.jar
from kscript.
Just out of curiosity. How did kotlin end up under /usr/share/
It's the standard location for application data on Arch Linux.
I'd think it would be better to avoid another JVM launch (which costs another few hundred milliseconds).
It doesn't launch the JVM:
$ time KOTLIN_RUNNER=1 JAVACMD=echo kotlinc
real 0m0.012s
user 0m0.000s
sys 0m0.000s
So what about (see also latest commit):
ls: cannot access '/usr/bin/../lib/kotlin-script-runtime.jar': No such file or directory
from kscript.
Thanks for your patience. Could you try again. I've changed it to
KOTLIN_HOME=$(dirname $(dirname $(JAVACMD=echo kotlin | awk 'NR>1' RS=' ' | grep kotlin-runner)))
I'm still confused/amazed by JAVACMD=echo kotlin
. It looks like a variable assignment but it somehow resolves kotlin
?
from kscript.
Could you try again.
Yes, that works. Thanks!
I'm still confused/amazed by JAVACMD=echo kotlin. It looks like a variable assignment but it somehow resolves kotlin?
It's just an environment variable assignment, the same as running:
env JAVACMD=echo kotlin
or setting:
JAVACMD=echo
in kotlinc.
The kotlin
wrapper sets KOTLIN_RUNNER=1
, which selects a branch at the end of kotlinc
, which defines java_args
, which defines the kotlin.home
property. On my system, it's faster to run kotlinc
directly as suggested above:
$ time JAVACMD=echo kotlin
real 0m0.019s
user 0m0.000s
sys 0m0.000s
$ time KOTLIN_RUNNER=1 JAVACMD=echo kotlinc
real 0m0.012s
user 0m0.000s
sys 0m0.000s
kotlinc
allows the path to java
to be supplied via the JAVACMD environment variable:
"${JAVACMD:=java}" $JAVA_OPTS "${java_args[@]}" -cp "${kotlin_app[@]}" "${kotlin_args[@]}"
If not supplied it defaults to java
. We abuse this to replace java
with echo
, so the last line of kotlinc
becomes:
echo $JAVA_OPTS "${java_args[@]}" -cp "${kotlin_app[@]}" "${kotlin_args[@]}"
This prints out the kotlin.home
property via java_args
:
java_args=("${java_args[@]}" "-Dkotlin.home=${KOTLIN_HOME}")
IMO, it's safer to just use the defined kotlin.home
property rather than scraping the name of a JAR file, which might be removed/renamed. Putting this together gives:
$ KOTLIN_HOME=$(KOTLIN_RUNNER=1 JAVACMD=echo kotlinc | awk 'NR>1' RS=' ' | grep -F kotlin.home= | cut -d= -f2)
$ echo $KOTLIN_HOME
/usr/share/kotlin
from kscript.
Related Issues (20)
- [kscript] [ERROR] Error executing command HOT 1
- Create kscript package for scoop HOT 7
- Don't work @file:Import("absolute/path/to/myutils.kt") HOT 2
- Basic hello world example does not work with interpreter HOT 1
- Allow setting an alternate kotlinc binary (or use kotlinc-jvm)
- Add an option to suppress final error output HOT 10
- [Windows] path to kotlinc not quoted HOT 4
- How to load text file via relative path? HOT 2
- kscript --interactive does not resolve the variables defined inside .kts file HOT 1
- kscript environment variable substitution for repositories no longer works HOT 6
- Does not work with Kotlin 1.9.0 HOT 12
- Can kscript used in Android project written in kotlin HOT 1
- Can you change kscript's included java version? HOT 1
- [question] daemon concept HOT 4
- Does not support spaces in KOTLIN_HOME variable.
- --package is broken in 4.2.3 - doesn't include stuff in local dependencies
- Fresh checkout of repo causes failure on `gradle run` and other ways of invoking `kscript` fail for the same reason
- kscript downloading Kotlin Native jars during dependency resolution - why? HOT 1
- Example in README with @file:Import fails
- Use version 4.2.3 on Dockerfile
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kscript.