How to build executable jar file to run standalone on jvm? I tried to build it with IDEA Artifacts, but it doesn't find main class.
I also tried to make java class named Main, define there psv main method, call kotlin code using java and build it via DEA Artifacts . It build’s, but running this jar causes java.io.FileNotFoundException(i have to read 2 txt files in jar file). Debug and Run mode run’s in IDEA well.
p.s. i read my files via var sc = Scanner(File(filename))
var arr = sc.nextLine().split(" "); and so on. It doesn’t work only in jar…
java -jar lingv_kursak_kt.jar
Exception in thread "main" java.io.FileNotFoundException: delimeters (═х тфрыюё
чэрщЄш търчрэшщ Їрщы)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at kt.ReadHelper.readAllIntoList(ReadHelper.kt:81)
at kt.Lexer.<init>(Lexer.kt:21)
at Main.main(Main.java:5)
Your resource files are not files if you are running with a jar (they are when you are running your app directly from the ide).
You have commented lines with getResourceAsStream. This was the right approach.
Note that from kotlin, you can access a class instance like this:
javaClass<ReadHelper>()
And then use getResourceAsStream on that, rather than using the class loader directly.
val clazz = javaClass<Lexer>()
val inputStream = clazz.getResourceAsStream("/res/lexems/delimeters") or clazz.getResourceAsStream("res/lexems/delimeters")
but inputStream is always null. Please give me some more hints)
Jerome David wrote that access jar file resources we need through getResourceAsStream method. For me it doesn't work now even for non jar app. As i wrote, the variable is always null.
Recently i put res directory into src and tried again
val clazz = javaClass<Lexer>()
val inputStream = clazz.getResourceAsStream("/res/lexems/delimeters") or clazz.getResourceAsStream("res/lexems/delimeters")
but still get no success.