1

I have built a Java application that reads data from a txt file located in the src folder. The path I have specified in the program is /src/data.txt and it works when I run it from netbeans. However when I tried to open the jar file, nothing opens. So I tried using javac from command line and this gives me the error that data.txt can't be found.

How do I make sure that the data file is included in the jar so it will work as a standalone? Thanks. EDIT1 : Here is a snippet of the code I use to load the file. And the path used is the aforementioned /scr/data.txt

public String [] openFile() throws IOException { FileReader fr = new FileReader(this.path); BufferedReader br = new BufferedReader(fr); String []text = new String[this.numberoflines]; for(int i=0;i<this.numberoflines;++i) { text[i]=br.readLine(); } br.close(); return text; } 

EDIT2 : Well here is the tvf output:

 Error: Could not find or load main class jar

C:\Users\Abhishek>jar -tvf Scrades.jar 0 Sun Jan 22 18:47:08 IST 2012 META-INF/ 199 Sun Jan 22 18:47:06 IST 2012 META-INF/MANIFEST.MF 2562 Sun Jan 22 18:47:08 IST 2012 CombinationGenerator.class 684 Sun Jan 22 18:47:08 IST 2012 Gameplay$1.class 684 Sun Jan 22 18:47:08 IST 2012 Gameplay$2.class 684 Sun Jan 22 18:47:08 IST 2012 Gameplay$3.class 684 Sun Jan 22 18:47:08 IST 2012 Gameplay$4.class 684 Sun Jan 22 18:47:08 IST 2012 Gameplay$5.class 969 Sun Jan 22 18:47:08 IST 2012 Gameplay$6.class 18279 Sun Jan 22 18:47:08 IST 2012 Gameplay.class 2275 Sun Jan 22 18:47:08 IST 2012 PermutationGenerator.class 1252444 Sun Jan 22 18:47:08 IST 2012 eng_final1.txt 3771960 Sun Jan 22 18:47:08 IST 2012 english_huge.txt 815532 Sun Jan 22 18:47:08 IST 2012 english_long.txt 16104 Sun Jan 22 18:47:08 IST 2012 english_short.txt 1506 Sun Jan 22 18:47:08 IST 2012 readFile.class
5
  • Copy/paste the output of jar -tvf the.jar. Commented Jan 22, 2012 at 7:16
  • How do you load your file in code ? do you use the classpath ? url ? fullpath ? show us some code Commented Jan 22, 2012 at 7:18
  • Show us your code. And understand that file IO is not the right tool to access what is in a jar. Also understand that users of your app won't have any src folder on their machine. Commented Jan 22, 2012 at 7:19
  • I am using the FileReader class in order to load the file during execution. @Andrew: I am assuming you meant -xvf. Well the output generated is Error : Could not find or load main class jar. Commented Jan 22, 2012 at 8:24
  • "I am assuming you meant -xvf." 'An ass has just been made of you'. No, I meant what I wrote -tvf. After all, how could you copy/paste the output of the classes etc. to SO? The t option provides a listing that you can copy and paste to the site. Note that if you'd done as I specified and pasted the output, I'd now be in a position to answer your question to Mob. Commented Jan 22, 2012 at 9:00

1 Answer 1

0

You can manually inspect, place and remove the file in the jar archive using a rar extractor like winrar : Then access per using Class.getResourceAsStream(String);

 InputStream is = getClass().getResourceAsStream("/src/data.txt"); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null){ //Perform operations } br.close(); isr.close(); is.close(); 
Sign up to request clarification or add additional context in comments.

5 Comments

The data file is present in the jar file (I checked using winrar). Could you please elaborate on how this command works?
Google is your friend, and javadoc is there to be read. See stackoverflow.com/questions/7736149/… or stackoverflow.com/questions/7152749/access-a-file-in-package/… for an example.
Is there any way I can modify the path parameter in the FileReader constructor in order for it to work instead of using this resource stream?
@user1163203 I don't know. I've added code on how to do this.
@Mob: Thanks a lot! I rewrote my code using Class.getResourceAsStream(String) and it is working now!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.