14

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. Hence I created a folder in the project and referred these files from the project. I created jar file following some tutorial but I could not able to include these external files in my jar file. Please let me about creating runnable jar with external files.

My file struture is

Test
|
|
-------src
|        |
|        default package
|                      |
|                      |
|                      test1.java
|
 -------FileFOlder
|              |
|              | 
|              abc.txt

I am accessing abc.txt in test1.java class. My code is,

public class test1 {


public static void main(String[] args) throws IOException {


    char [] read = new char[20];
    String path = new File(".").getCanonicalPath();
    path = path+"\\Newfolder\\abc.txt";
    System.out.println(path);
    File nF = new File(path);
    FileReader fR = new FileReader(nF);
    fR.read(read);
    for(char c : read){
        System.out.print(c);
    }
    fR.close();
    System.out.println(" Hi..This is test program ");
}

}

When I create executable jar using eclipse export option, I am unable to see FileFolder directory inside the jar. Please give me some information regarding this.

Anup
  • 265
  • 2
  • 6
  • 14

2 Answers2

14

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

Community
  • 1
  • 1
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • 1
    Thank you for your response. Could you please let me know how can I put my text files inside the jar file. – Anup Aug 06 '13 at 21:56
  • @Anup what have you tried? How are you building your jar file now? – Daniel Kaplan Aug 06 '13 at 22:11
  • 1
    I am building jar files through Eclipse. I select Export option and then runnable Jar and finally selects the class. I did not get any options to select other folder. – Anup Aug 06 '13 at 22:46
  • I think this will explain how to do that: http://stackoverflow.com/a/7284648/61624 – Daniel Kaplan Aug 06 '13 at 22:49
  • do you have library handling options? selecting `Extract libraries into generate JAR` might help. – scottysseus Aug 06 '13 at 22:50
  • 1
    @ScottScooterWeidenkopf - Thank you for your response. Yes I tried with that option and all other option. But there was same outcome. The jar file doesnot have file in it. – Anup Aug 06 '13 at 22:52
  • hmmm I actually have a JAR that *should* file in it right now...how do I even check if the JAR actually contains the file? – scottysseus Aug 06 '13 at 22:57
  • How about modified external files? Your method assumes the files are read only, in a lot of cases you need to change the contents of text file after you create the Jar. – Gherbi Hicham Sep 21 '16 at 14:48
14

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.

scottysseus
  • 1,922
  • 3
  • 25
  • 50
  • Thank you very much. I created jar file as you specified and I got the text file in the jar file. But now I am unable to find out a way to access that file from my code. So when I run my jar file I am getting FileNotFound exception. I am unable to use that file and read the content. Please let me know if you anything about this. – Anup Aug 07 '13 at 21:29
  • I have never done this myself, but after a little browsing I found [this](http://stackoverflow.com/questions/2593154/get-a-resource-using-getresource) stackoverflow question. it seems that you can user `ClassLoader` to access files on your classpath, which would include your abc.txt – scottysseus Aug 07 '13 at 21:58
  • oooh i think i may have found the [`answer`](http://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java) – scottysseus Aug 07 '13 at 22:00
  • Thank you for your link.. I used getResourceAsStream() method. I used the link to refer and there was one more link which I found was very clear and useful. [link] (http://alvinalexander.com/blog/post/java/read-text-file-from-jar-file)..I used this link and solved the proble. But I appreciate your help.. Thank you very much.. – Anup Aug 08 '13 at 00:55
  • @Anup Hey, the link you have specific is broken. – inquisitive Feb 26 '17 at 10:13