Packing Resource-Based App Into a .JAR

C

cppaddict

I have an application which uses outside data files. That is, it has
lines of code like this:

DataReader.loadFile("data/MyFile.dat");

Right now the app is just a bunch of .class files and I start it on
the command line from the appropriate directory to ensure that the
data files can be found.

But I want to package the app as a .jar (a .jar which includes the
data files) and be able to run the .jar from any directory. That is,
when it's running, java should search for the data files relative to
the .jar root, rather than relative to the root where the process was
started.

How can I do this?

Thanks,
cpp
 
K

klynn47

In the package java.util.jar, there is a class called JarFile. You can
create a JarFile using the name that you eventually choose for the jar.
In JarFile there is a method called entries() that gives you an
Enumeration of the elements in your .jar file. Once you have the name
of the file you want within your .jar file, you can use the URL
"jar:file:filename.jar!/" in order to load a text file from within the
..jar. The URL class contains the method openStream() which produces an
InputStream. You can wrap an InputStreamReader around that and a
BufferedReader around the InputStreamReader. Then you can read from it
like you would from a text file.
 
S

sanjay manohar

In your class DataReader, you could try using
Class.getResourceAsStream() rather than FileInputStream?
This searches within the current package rather than the current
working directory, and works whether or not you are in a JAR file.
 
S

Steve W. Jackson

:In the package java.util.jar, there is a class called JarFile. You can
:create a JarFile using the name that you eventually choose for the jar.
:In JarFile there is a method called entries() that gives you an
:Enumeration of the elements in your .jar file. Once you have the name
:eek:f the file you want within your .jar file, you can use the URL
:"jar:file:filename.jar!/" in order to load a text file from within the
:.jar. The URL class contains the method openStream() which produces an
:InputStream. You can wrap an InputStreamReader around that and a
:BufferedReader around the InputStreamReader. Then you can read from it
:like you would from a text file.

Here's what I think might be a simpler alternative, and it works equally
well before and after making a jar of the app. We use this technique
most often with images, but it's also used to load files of other types.

If my pics are in com/acme/images, you can say:

ImageIcon icon = new ImageIcon(this.getClass().getResource(
"/com/acme/images/picfile.jpg"))

And I've got an ImageIcon from the file, whether it's in a jar or not.
In other cases, a class wanting access to a file of any kind can get it
as an InputStream:

InputStream is = this.getClass().getResourceAsStream(
"/com/acme/datafiles/filename.dat");

I can do lots of things with an InputStream to get at the file's
contents.

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top