Creating Runnable Jar with external files included

后端 未结 2 1637
日久生厌
日久生厌 2020-11-28 14:28

I want to build a runnable jar in java. I need to include some of the files in the jar so that when I execute jar the files are automatically read from the java class. Henc

相关标签:
2条回答
  • 2020-11-28 14:55

    Here's what you should do instead:

    Put that file back in your jar file. Use class.getResourceAsStream() to read it instead of File and FileReader. Here is an explanation of how to do that: How to really read text file from classpath in Java

    0 讨论(0)
  • 2020-11-28 15:04

    Problem Solved!

    here is how:

    1) right click your project folder and create a new folder.

    2) move all of your files that you want packed into the jar into that folder.

    3) click project -> properties -> Build Path -> Source -> Add Folder and select that folder you just created.

    4) create your JAR!

    Since you already have created your folder with abc.txt insideit, you can skip steps 1 and 2

    EDIT: one way you can make sure your JAR contains these files is to use 7zip.

    0 讨论(0)
提交回复
热议问题