1

I'm new to Kotlin so forgive me if this is an easy question. I'm writing a kotlin script that I hope will utilize a custom Hashtable implementation to store data from a file. I'm having trouble getting the script to find the HashTable class. Here is my structure:

.
├── scripts
│   ├── kotlin
│   │   ├── [other scripts]
│   │   └── wordcount.kts
│   └── tests
│       └── wc
│           └── smallfile.txt
└── src
    ├── main
    │   └── kotlin
    │       └── dataStructures
    │           └── HashTable.kt
    └── test

The script is wordcount.kts and the class I'm trying to import is in HashTable.kt. I tried import dataStructures.HashTable and import kotlin.dataStructures.HashTable to no avail. I also tried adjusting the PWD (in IntelliJ runtime configuration) to the project directory, also with no luck. How do I import HashTable correctly? Let me know if I can provide any further information!

1 Answers1

0

import is used to link to things that are on your classpath, so before you can use that you need to allow the compiler to actually find that HashTable class.

You have a couple of options, I would however recommend to rename wordcount.kts to wordcount.main.kts (kotlin script requires the executable to be named x.main.kts for most features to work), HashTable.kt to HashTable.kts and link it with @file:Import(<path-to-hashtable.kts>).

If you can't rename the hashtable you will need to import it either by compiling it to a class file and adding it to the classpath with kotlinc -script -cp <dir-with-.class> wordcount.main.kts. Or compile to a jar and link the jar with @file:DependsOn<path-to-jar> in the script.

For the reference to all this stuff, look here: https://github.com/Kotlin/KEEP/blob/master/proposals/scripting-support.md

somethingsomething
  • 1,746
  • 6
  • 8