my dog learned polymorphism*
The moose likes Beginning Java and the fly likes How to access a text file inside a JAR Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to access a text file inside a JAR" Watch "How to access a text file inside a JAR" New topic
Author

How to access a text file inside a JAR

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Hi Folks,

I have a text file bundled inside a JAR, how do i access from my code that is also part of the jar?
So for example the package where my code is "com.test.services.LoadRunner.FireCollection" and the text file is also under that package. How do i access it?

-Aditya
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Do you have access to that text file when you are running the unzipped code? How did you gain access? Presumably something like new File("myFile.txt")? Did that path work?

I always thought you are supposed to put files in their own folder and quote the path to that folder when you open the files.
Have you seen the examples in the Java Tutorials? (Try the "creating" page.)
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

You need to address the resource with an URI. The URI for a resource within a jar-file is received using the Class.getResource(String name). (See http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String%29).

If your file is in the package com.test.services.LoadRunner.FireCollection and the file needed is named fire.txt then you get the URI by doing this:



getResource will search from all the classpath starting points. Normally, you put resources like this under a folder of its own. Remember that this is a read only file, but useful when storing templates or default properties.


Edit:
This works, as mentioned below, for resources on the classpath, but not on resources in a Jar. See Ernest addition.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

There's a simple and straightforward answer to this: use the Class.getResource() family of methods. For example, if you have "/data/file.txt" in the jar file, then you can open that file for reading using something like



The nice thing about this is it works perfectly whether the files are in a JAR or not. If they're not, then it works as long as the parent of the data directory is on your classpath.

Edit: Ove's answer won't work if the file is in a JAR, although it can be used to find physical files on the classpath.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Ove Lindström wrote:you get the URI by doing this

Almost right. Resources work with URLs, not URIs.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Ernest Friedman-Hill wrote:Edit: Ove's answer won't work if the file is in a JAR, although it can be used to find physical files on the classpath.


Correct. Thanks for correcting it. Have changed in my post also for the future.


Rob Spoor wrote:
Almost right. Resources work with URLs, not URIs.


Note to self: Read the Javadoc in a correct way. ;)
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Thanks Guys,

FileReader paramReader = new FileReader("com/test/services/LoadRunner/FireCollection/fire.txt"); and will this file be read if its inside com.test.services.LoadRunner.FireCollection package?

-Aditya
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Aditya Sirohi wrote:
FileReader paramReader = new FileReader("com/test/services/LoadRunner/FireCollection/fire.txt"); and will this file be read if its inside com.test.services.LoadRunner.FireCollection package?


Not if the files are in a JAR, and only if the directory "com" is a subdirectory of the current working directory.

Use this instead: it will find a file anywhere on the classpath, even inside a JAR:



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to access a text file inside a JAR
 
Similar Threads
JWS: Executable jar runs but doesn't show the data
Creating a folder in a JAR file ...
Problem while executing jar file
Reading from a file from inside a jar
XML Schema in a jar file?