This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Relative path Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Relative path" Watch "Relative path" New topic
Author

Relative path

Jessid Leon Velez Gutierrez
Greenhorn

Joined: Dec 04, 2006
Posts: 22
Hello. I have a question: I am creating an application that want to pack in a jar file. That app uses a couple of plain text files. I want to include thouse 2 files into the jar, but i havent been able to make the app to read them. I dont know how to doit. Perhaps some of you could help me please with that.

Thanks a lot!
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Jessid,

In order to help you, we'll need some more detail.

Where are you putting the file?

What's the code that's trying to open it?

What exception are you seeing (full stack trace helps)?


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

If he doesn't know how to do it, I don't think we need any more detail!

To read files out of the jar a program is packed into, you can't use things like FileReader and FileInputStream. Instead, use the ClassLoader methods that find files along the classpath, like this:

InputStream theFile = getClass().getResourceAsStream("thefile.txt");


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Relative path
 
Similar Threads
Question on ClassLoaders
Jrun 4 & installing app
How to run servlet which send email, on client's computer
Multiple JDK's and JRE's
Adding a jar without touching existing code