Packing Resource-Based App Into a .JAR

Discussion in 'Java' started by cppaddict, Dec 9, 2004.

  1. cppaddict

    cppaddict Guest

    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
     
    cppaddict, Dec 9, 2004
    #1
    1. Advertising

  2. cppaddict

    Guest

    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.
     
    , Dec 9, 2004
    #2
    1. Advertising

  3. 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.
     
    sanjay manohar, Dec 9, 2004
    #3
  4. In article <>,
    wrote:

    >: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 =
    --
    Steve W. Jackson
    Montgomery, Alabama
     
    Steve W. Jackson, Dec 9, 2004
    #4
  5. Andrew Thompson, Dec 9, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arnold Peters
    Replies:
    0
    Views:
    632
    Arnold Peters
    Jan 5, 2005
  2. muttley
    Replies:
    0
    Views:
    2,791
    muttley
    Oct 20, 2005
  3. cyberco
    Replies:
    4
    Views:
    3,869
    Roedy Green
    Feb 14, 2006
  4. Arnold Peters
    Replies:
    0
    Views:
    702
    Arnold Peters
    Jan 5, 2005
  5. Gubatron
    Replies:
    2
    Views:
    595
    Gubatron
    Dec 3, 2008
Loading...

Share This Page