kscript icon indicating copy to clipboard operation
kscript copied to clipboard

How to load text file via relative path?

Open dnltsk opened this issue 2 years ago • 2 comments

I want to open files that are located beside the main script file.

@file:Import("Models.kts")
val data = Gson().fromJson(FileReader("data.json"), Models.Data::class.java)

fails with

java.io.FileNotFoundException: data.json (No such file or directory)

Using the absolute path works straight away. Cannot find anything guidance in the documentation.

dnltsk avatar Apr 03 '23 11:04 dnltsk

The sample code above is working finr when I execute the script from console like

> ./Main.kts

But not when executed from within IntelliJ, when the project was created via

> kscript --idea Main.kts

Then, it expects to find the text file inside the .../Library/Caches/kscript/idea_59... path.

dnltsk avatar Apr 03 '23 13:04 dnltsk

Yes, because --idea creates IntelliJ project, which is intended for debugging, not as a runtime environment. The project is located in the cache, so you must copy the data file if you want to use it.

aartiPl avatar Jun 13 '23 19:06 aartiPl