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
  • Copy/paste the output of `jar -tvf the.jar`. – Andrew Thompson Jan 22 '12 at 07:16
  • How do you load your file in code ? do you use the classpath ? url ? fullpath ? show us some code – A.B.Cade Jan 22 '12 at 07: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. – JB Nizet Jan 22 '12 at 07: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. – user1163203 Jan 22 '12 at 08: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. – Andrew Thompson Jan 22 '12 at 09:00

1 Answers1

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();
Mob
  • 10,958
  • 6
  • 41
  • 58
  • The data file is present in the jar file (I checked using winrar). Could you please elaborate on how this command works? – user1163203 Jan 22 '12 at 08:29
  • Google is your friend, and javadoc is there to be read. See http://stackoverflow.com/questions/7736149/how-to-specify-path-for-properties-file/7736239#7736239 or http://stackoverflow.com/questions/7152749/access-a-file-in-package/7153574#7153574 for an example. – JB Nizet Jan 22 '12 at 09:01
  • 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 Jan 22 '12 at 16:51
  • @user1163203 I don't know. I've added code on how to do this. – Mob Jan 22 '12 at 17:23
  • @Mob: Thanks a lot! I rewrote my code using Class.getResourceAsStream(String) and it is working now! – user1163203 Jan 22 '12 at 18:41