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.

1
  • Define 'not able to include these external files in my jar file'. Why not? Commented Aug 7, 2013 at 0:58

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your response. Could you please let me know how can I put my text files inside the jar file.
@Anup what have you tried? How are you building your jar file now?
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.
I think this will explain how to do that: stackoverflow.com/a/7284648/61624
@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.
|
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.

5 Comments

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.
I have never done this myself, but after a little browsing I found this stackoverflow question. it seems that you can user ClassLoader to access files on your classpath, which would include your abc.txt
oooh i think i may have found the answer
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] (alvinalexander.com/blog/post/java/… used this link and solved the proble. But I appreciate your help.. Thank you very much..
@Anup Hey, the link you have specific is broken.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.