Build a jar file


#1

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…


#2

I can't seem to reproduce your problem: my artifact runs perfectly. Could you share your project? Thanks.


#3

I attached my IDEA project.

The problem is in:

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)



kt.zip (1.57 MB)

#4

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.


#5

I tried to do

  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)


#6

File 'delimeters' is found if it exists in the corresponding path =). Just put your jar in the folder that contains "res/lexems/delimeters".

To invoke kotlin main function from ‘xxx’ package just write
  Main Class: ‘xxx.namespace’ (‘kt.namespace’ in your case).


#7

thanx, but i want to pack file "delemiters" and its path to jar file, and then read it with getResourceAsStream()


#8

I looked at the jar and it didn't contain the res files. Maybe add the content of the res directory to the artifact output?


#9

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.


#10

I think it's because the build doesn't know about that res directory. Have you tried to move that res directory under src?


#11

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.