File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How do I refer to a text data file so a .jar can find it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I refer to a text data file so a .jar can find it?" Watch "How do I refer to a text data file so a .jar can find it?" New topic
Author

How do I refer to a text data file so a .jar can find it?

Simon Hunt
Greenhorn

Joined: Jul 17, 2006
Posts: 11
Sorry to ask such a basic question, but I have a simple app. reading data from a text file I have put in the src directory resources section as detailed below.



This works fine when running the project from within my IDE. However, when I build the project and try and run it from the .jar the program fails to find the file (I can see the error when I run it from the command line, because I've put the code line I detailed above within an appropriate try - catch block and it throws a file not found error). I've tried editing the Path but nothing seems to work, even though I can see the file within the .jar using winzip. There are also two other (empty) resources folders within the .jar, one at the top level and one below the level I've stored the file at (i.e. within resources!), however I chose the mid level one because all the other resources like .jpg images are there and the program seems to find them without trouble. I thought, just removing the src\\ ought to do the trick because the path within the .jar is solowarforedadhopponent\resources\MasteryCardData.esd, but this stops the program functioning both within the IDE and as a .jar.

So, my question is this: Where should this data file actually go and how can I refer to the file so that the program runs correctly both inside the IDE and as a .jar?

References to other threads, articles and tutorials all welcome.

Many Thanks


"I was born in a water moon..."
The Algebraist by Iain M. Banks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18750
    
  40

Simon Hunt wrote:
I've tried editing the Path but nothing seems to work, even though I can see the file within the .jar using winzip.


The File class is used to access files in the filesystem. It is not used to get resource files from within a jar file, in the class path. To do that take a look at the getResourceXXX() methods from the Class class (google java resource files).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Simon Hunt
Greenhorn

Joined: Jul 17, 2006
Posts: 11
Dear Henry

Many thanks for the initial clues, but I'm still struggling.


I'm not sure what you mean by the getResourceXXX() methods. When I look at the Class class in the API it only lists two similar methods:
1. getResource(String name) which returns a URL object.
To test this I put a "test print" line into my code
which gives something that looks correct but doesn't work - even if I make the string "resources\\MasteryCardData.esd". (Not sure about \\ and / either - using \\ inserts %5c into the string in this case)

2. getResourceAsStream(String name) returns an InputStream object which doesn't have a readLine() method which I need to parse my csv file.

My problem seems to be that ultimately I need a File object to pass to my FileReader to pass to my BufferedReader for that.



Just in case it's relevant, here's the code that follows the file open:


Many Thanks for any additional clues.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41586
    
  54
Try "/solowarforedadhopponent/resources/MasteryCardData.esd"


Ping & DNS - my free Android networking tools app
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18750
    
  40

Simon Hunt wrote:
2. getResourceAsStream(String name) returns an InputStream object which doesn't have a readLine() method which I need to parse my csv file.

My problem seems to be that ultimately I need a File object to pass to my FileReader to pass to my BufferedReader for that.


First of all, why do you absolutely need the readLine() method? Are you saying that you can't parse it, if the BufferedReader class isn't available? Second, why do you need a FileReader ? Are you saying that you can't get a BufferedReader without a FileReader ?

Henry
Simon Hunt
Greenhorn

Joined: Jul 17, 2006
Posts: 11
Gentlemen

Ulf - many thanks for the suggestion, but I'm afraid it causes the program to fail in both the IDE (Netbeans) and in the .jar. Curiously enough "src/solowarforedadhopponent/resources/MasteryCardData.esd" works just as well in the IDE as "src\\solowarforedadhopponent\\resources\\MasteryCardData.esd". As I said in my last post, I really don't understand the \\ vs / thing, but I hope it's not key.

Henry - it seems from your tone that you're finding this exchange irritating, for which I apologise, but the reason I'm posting to this forum is that I'm very much a Java beginner despite having joined this forum some years ago. I'm a hobbyist programmer, and I only usually get to programming about once a year, and it usually involves me largely re-learning Java from scratch (I use "Head First Java" mainly because it's fun).

Henry Wong wrote: First of all, why do you absolutely need the readLine() method? Are you saying that you can't parse it, if the BufferedReader class isn't available?
I'm saying I don't know how to parse it if the BufferedReader class isn't available. I'm sure that there are plenty of other ways of parsing a file I don't know about. During my scouring of the API I found a bunch of bit-wise methods in the getResourceAsStream section, so I know that's possible to do it bit-wise, but that seems a crazy way to tackle a .csv file; the readLine() method gives me a line at a time as a string which I know how to manipulate satisfactorily.

Essentially I've written some code to parse the .csv file which works fine, and re-writing it to solve what I perceive (albeit probably wrongly) as a path issue looks like tail wagging dog. That said, there are probably more elegant ways of doing this I don't know about as well, however I'm relying on a code segment from the "Head First Java" book, which only mentions this way of handling text files, so it's the only method I know.

Here's the full code section (I was avoiding posting this because it's long but I think the time has come) so you can see what I'm up to:
I've left in all the ideas I've tried and failed with, as comments, to avoid re-treading the same ground; happily it also allows you to see what I've tried too.

Henry Wong wrote:Second, why do you need a FileReader ? Are you saying that you can't get a BufferedReader without a FileReader ?
I'm really not asserting anything. It just appears from the section of the book and the code examples that a BufferedReader takes a FileReader as a constructor argument.
*looks at API*
The API suggests I simply need a Reader of which BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader are all sub-classes. It's interesting that FileReader is not on that list.

However, InputStreamReader looks promising...

I'll try this:
This works inside the IDE fine... but not in the .jar

Okay Simon, don't panic yet. Try "resources/MasteryCardData.esd"
This works inside the IDE fine... AND AS A .jar

Yes! He can be taught!

Many Thanks Henry
p.s. I still don't know why the IDE takes \\ but the jar only likes /?


Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18750
    
  40

Simon Hunt wrote:
Henry - it seems from your tone that you're finding this exchange irritating, for which I apologise, but the reason I'm posting to this forum is that I'm very much a Java beginner despite having joined this forum some years ago. I'm a hobbyist programmer, and I only usually get to programming about once a year, and it usually involves me largely re-learning Java from scratch (I use "Head First Java" mainly because it's fun).


Nope. No irritation whatsoever. It is just a bunch of hints phrased in a form of a question. Pretty common technique for me.... And trust me, it takes a *LOT* of effort to get me irritated....

Henry
Simon Hunt
Greenhorn

Joined: Jul 17, 2006
Posts: 11
Henry Wong wrote:Nope. No irritation whatsoever. It is just a bunch of hints phrased in a form of a question. Pretty common technique for me.... And trust me, it takes a *LOT* of effort to get me irritated....


All's good then. It just goes to show how tricky conversations are without voice tone and body-language cues.

Many thanks for leading me to the answers. I admit freely to thinking "Just give me the damn fish!", but the question phrased hints really did work and it really is the best way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I refer to a text data file so a .jar can find it?