Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to access a text file inside a JAR

 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ove Lindström wrote:you get the URI by doing this

Almost right. Resources work with URLs, not URIs.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic