Kotlin script classpath in IDEA


I am not sure whether I should put it here on IDEA discussion, but since it is kotlin-specific, I will try here first.
I am using kotlin for some simulation and data analysis so sometimes I want to work right from IDE without building the project first. I can easily do it with gradle tasks or by creating main function in a file, but it requires additional body movements which I do not like. When I am working with Groovy, I just create a script file and execute it in IDE.
Kotlin also supports scripting and even REPL inside IDE.
The problem is that script does not seems to have access to other project classes. Its classpath does not include build directory. Is it intended to be like this? And, by the way, it for some reason changes encoding to system default instead of project default (which is probably a bug).

REPL inside IDE is a great thing, but it seems to be unstable, since it throws some vague NoClassDefFoundError when using kotlin global functions.


No, it’s not intended to be like this. There’s an open YouTrack issue for this problem; you can vote for it to get notified when it’s resolved.


Oh, great! Thank you very much. It would also be great to be able to use gradle runner for it.


What about support for standalone live JVM REPL just to explore the language itself like JShell? Does anything like that exist currently that you know of? If not I’m really tempted to hack together something on top of the jdk.jshell API just for fun


Have you read this: https://kotlinlang.org/docs/tutorials/command-line.html?
As far as I can understand it still is not the full analogue of JShell or GroovyShell since it does not have stable API and ability to be configurable from program. But I believe it is what you want if all you need it sot explore language.
They also have web-tool: https://try.kotlinlang.org/#/Examples/Hello,%20world!/Simplest%20version/Simplest%20version.kt.


I don’t mean anything official but a 3rd party fork maybe. I’ve tried kotlinc CLI tool and it’s not very interactive and doesn’t promote exploratory programming because it’s not like a live jvm process that it compiles against and runs in like jshell. Try out jshell if you haven’t, it’s a bit akward but super powerful once you get the hang of it.
It’s nice if you want to test something in your existing code really quickly with a one liner or whatever. Jshell exposes an interface on the SPI for hotswapping types and whatnot so I don’t thing it’d be that difficult to implement. Was just curious for myself I guess, not requesting someone else to do it :slight_smile: Kotlinc makes understanding javac look like a walk in the park though, especially since source & building the language tools isn’t documented

Do scripts accept arguments/parameters? Is there a way to pass objects to them?

You can always call kotlin classes from JShell or GroovyShell. But I agree, more powerful REPL engine would be good. In my case, I want to be able to embed the REPL inside my own code like I do it with GroovyShell.