4

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 -include-runtime -d myLib.jar

Then I created a script file:

// myScript.kts

import myLib.*
say_hello("Arvind")

But i can not compile the script file as it neither recognizes myLib package nor say_hello() function.

What is the correct way to do this.

Question Update: I am using kscript to run my script. Typing a lot e.g.,

kotlin -cp myLib.jar myScript.kts

every time I have to run the script, thus defeats the motive of using kscript.

Is not there any way so that I need not give path of jar every time command line. Instead I want to use it in a kscript way, i.e.

./myScript
virus00x
  • 713
  • 5
  • 12
  • I had the same problem and your solution using the kotlin command works. But this means that the documentation of kotlin is wrong: https://kotlinlang.org/docs/tutorials/command-line.html This uses the kotlinc compiler to invoke the script and I cannot make it work to load the contents of the jar file. – shaft Oct 26 '20 at 23:00

1 Answers1

2

You need to include the myLib.jar in the classpath, for example:

kotlin -cp myLib.jar myScript.kts

Also, you do not need to compile myLib with -include-runtime, unless you want to create a self-contained and runnable jar (see example).

Update:

Rename myScript.kts to myScript.main.kts and change its content to:

#!/usr/bin/env kotlin

@file:DependsOn("myLib.jar")

import myLib.*

say_hello("Arvind")

You now can call it (don't forget to set execute permissions):

./myScript.main.kts

If instead of the myLib.jar file you want to include the myLib.kt script replace @file:DependsOn("myLib.jar") by @file:Import("myLib.kt").

Edgar Domingues
  • 930
  • 1
  • 8
  • 17
  • Thanks for the reply. Please clarify one more thing. You said "Also, you do not need to compile myLib with -include-runtime". Is it true even if the jar file uses the kotlin standard library functions?? For example what if I am using time or calander functions or may be nio package. – virus00x Aug 28 '20 at 06:03
  • @arvindpavariya I edited the the answer to explain why you don't need -include-runtime. – Edgar Domingues Aug 28 '20 at 06:11
  • Please refer to the question update. I need a different solution. – virus00x Aug 29 '20 at 09:12
  • @arvindpavariya Answer updated to show how you can call the script with less command line typing – Edgar Domingues Sep 04 '20 at 06:19
  • Thanks @Edgar Domingues. It worked. If you dont mind, Could you please explain me the need of renaming the script file to myScript.main.kts. Because the *.kts files already considers their content as if written inside the main function. – virus00x Sep 14 '20 at 07:50
  • Honestly I'm not sure. I think it's for Kotlin to include kotlin-main-kts.jar in the classpath, which it's need to resolve the dependencies (e.g. file:DependsOn and file:Import). – Edgar Domingues Sep 14 '20 at 17:47
  • How to add JAVA_HOME/lib/tool.jar with @file:DependsOn()? It doesn't work well if path is dynamic – OpenGG Aug 25 '21 at 07:41