Questions tagged [kotlin-script]

Use only for issues strictly related with Kotlin scripts running in command line, not for generic Kotlin questions

Kotlin can also be used as a scripting language using the Kotlin command line compiler to run scripts.

Working with the Command Line Compiler

30 questions
13
votes
1 answer

Gradle 6.6.1 and Kotlin 1.4 - runtime version conflict

I use the latest Gradle (v. 6.6.1) and the latest Kotlin (v. 1.4). buildSrc project defines all Kotlin versions. And unfortunately it receives the error below. The `kotlin-dsl` plugin applied to project ':buildSrc' enables experimental Kotlin…
Manushin Igor
  • 3,398
  • 1
  • 26
  • 40
10
votes
2 answers

kscript : How to get directory of current file?

Is there a way to get directory of a current script location in kotlin script? I could achieve this in bash with dirname $0 or # Absolute path to this script. /home/user/bin/foo.sh SCRIPT=$(readlink -f $0) # Absolute path this script is in.…
Ivan
  • 340
  • 3
  • 14
8
votes
2 answers

How to run a Kotlin script on GitHub Actions?

I want to run Kotlin scripts in CI without relying on a Gradle project, so I can easily do operations that would be hard to program using shell/bash/batch, and so that I can use libraries if needed. Having the Kotlin script run only on Ubuntu/Linux…
Louis CAD
  • 10,965
  • 2
  • 39
  • 58
5
votes
1 answer

How do I apply a plugin in a .kts (Kotlin script) file?

Kotlin script (.main.kts) files have the idea of providing executable Kotlin code in ONE single standalone file, which is immensely convenient for scripting or when sharing code snippets on StackOverflow for example. In contrast to that, currently…
xjcl
  • 12,848
  • 6
  • 67
  • 89
5
votes
2 answers

How to use Kotlin worksheets in Android project

I want to use Kotlin worksheets in my Android project in order to add code drafts like: draft.ws.kts package com.example.app val a = 1 + 1 a The worksheet itself is working: val a: Int 2 But building my Android app fails with the following…
mbo
  • 4,611
  • 2
  • 34
  • 54
4
votes
0 answers

How to configure kotlin script project with dependency on current module?

I'd like to have: directory with .kts files inside classic src/main/kotlin/my/package module with classes And have usage like: kotlin my-script.main.kts Where script looks like my-script.main.kts this from the example #!/usr/bin/env…
Artem Ptushkin
  • 1,151
  • 9
  • 17
4
votes
0 answers

How to use a maven dependency in Kotlin REPL?

A Kotlin script with *.main.kts format can easily pull the dependency through the @file:Repository and @file:DependsOn annotations. But I tried something similar with the REPL: kotlinc-jvm -cp /lib/kotlin-main-kts.jar (specified…
4
votes
1 answer

How to include a jar file in kotlin script

I want to separate some generic code from my kotlin script file so that it can be reused. I did this: // MyLib.kt package myLib fun say_hello(name : String) { println("hello $name") } I compiled this file to create a jar file: kotlinc myLib.kt…
virus00x
  • 713
  • 5
  • 12
4
votes
1 answer

Kotlin script (.kts) file -- no println?

I am experimenting with the use of Kotlin as a scripting language. According to their docs, you should be able to run top-level code in a Kotlin script. A simple "Hello, World" program I wrote using their official example is not outputting any…
bbuck
  • 129
  • 10
3
votes
1 answer

Is there a way to instantinate KTS script engine in the Gradle KTS?

I want to use 3d party library in my project build process. Library methods requires ScriptEngine. When I'm trying to instantiate it i got an error: java.lang.IllegalArgumentException: Unable to construct script definition: Unable to load base class…
VShcryabets
  • 341
  • 1
  • 7
3
votes
2 answers

Can I use kotlinx classes in Kotlin scripts?

Is it possible to import classes from kotlinx package in a simple Kotlin script? myscript.kts: import kotlinx.serialization.* import kotlinx.serialization.json.* println("") Running the above script with kotlinc -script myscript.kts gives this…
Mahozad
  • 18,032
  • 13
  • 118
  • 133
2
votes
0 answers

Kotlin script fails to build in maven

I'm trying to build kts scripts with maven but I'm unable to compile them. Is it even possible to build kotlin scripts using maven? Same code using gradle compiles just fine. Script base class: package com.test.scripts.api import…
Indrz
  • 131
  • 1
  • 2
  • 7
2
votes
1 answer

Kotlin script doesn't find Ktor CIO class

I'm trying to use kotlin-main-kts with a Ktor client. Here is my simple Kotlin script in a file named…
BPS
  • 1,606
  • 20
  • 37
2
votes
1 answer

What is java.constructors.single()?

I'm learning Kotlin how to eval scripts, and I saw the code in BasicJvmScriptEvaluator like the following: val ctor = java.constructors.single() val saveClassLoader = Thread.currentThread().contextClassLoader …
vesper
  • 264
  • 1
  • 11
2
votes
1 answer

Kotlin scripting support fails with "wrong number of arguments" whenever I try to run any script

I'm trying to run a very basic script with org.jetbrains.kotlin:kotlin-scripting-jvm, but I get two errors, when I should get none. This is my script: 1 I expect to get back a ResultWithDiagnostics.Success with a resultValue of 1 but instead I get…
Adam Arold
  • 29,285
  • 22
  • 112
  • 207
1
2