Build a jar file


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 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…


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


I attached my IDEA project.

The problem is in:

java -jar lingv_kursak_kt.jar Exception in thread "main" delimeters (═х тфрыюё чэрщЄш търчрэшщ Їрщы)   at Method)   at<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( (1.57 MB)


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:
And then use getResourceAsStream on that, rather than using the class loader directly.


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)


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).


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


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?


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.


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


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.