File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Relative path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Relative path" Watch "Relative path" New topic

Relative path

Jessid Leon Velez Gutierrez

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


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

Joined: Jul 08, 2003
Posts: 24199

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:
subject: Relative path
It's not a secret anymore!