Reading a compress file in a jar.  JarFile or JarInputStream?

I am trying to read a file out of a jar file. With the following code. Depending on where the file is I get different types of Objects returned. The file is quite big so I would like to compress it in the jar file. When I compress it I cannot use it. It works for the first two cases. On the third case I get an out of memory error. On the third case I tried to cast it as a ZipFile or ZipInputStream I get a runtime exception in the casting.
What should I do to get this to work? I don't understand why Visual Cafe is telling me it is a ZipFile in the value section in the debugger.
Object tin=(Object) getClass().getResourceAsStream(_cinfo.pbl);
JDWC_customer.setSourceStream((java.io.InputStream) tin);
CASE 1. File read off of disk.
instance of java.io.BufferedInputStream(id=1089)
CASE 2. File read out of a jar file uncompressed file.
instance of java.util.zip.ZipFile$ZipFileInputStream(id=1089)
CASE 3. File read out of a jar file compressed file.
instance of java.util.zip.ZipFile$1(id=1417) File Compressed
Does anyone have any sample code that does this?
Thanks

Since a jar file is a zip file, it doesn't make a lot of sense to me to compress the data twice. I would do one of these two things:
1. Zip your data separately but do not include in your jar.
Instead, provide the zip along with your jar file. Place a reference to your zip in the manifest of your jar -> in the Class-Path attribute.
2. Let the jar zip your data up for you.
Either way, I like to use the system classloader to grab data:
InputStream is = ClassLoader.getSystemResourceAsStream("whatever.resource");-Ron

Similar Messages

  • Help! Reading a text file in a JAR

    How do I read a text file in a jar? (NON-applet).
    Following doesn't work:
    File f = new File("res\\dictionaryuk.txt");
    Neither does this:
    InputStream in = this.getClass().getResourceAsStream("res\\dictionary.txt");
    (I read somewhere else here that one should use resources instead of files in a JAR, but I'm not certain about that, or why?)
    It's not the pathname because I've tried everything.
    "res\\dictionaryuk.txt"
    "res/dictionaryuk.txt"
    "\\res\\dictionaryuk.txt"
    "\\dictionaryuk.txt"
    "dictionaryuk.txt"
    ...you name it
    And yes, the text file IS in the JAR.
    Any suggestions? Sample code?

    Okay, here is a quick example of how to do it that I tested and works...
    I get the same values for the ZipEntry method and the InputStream.available method, but the available method is used for slightly different purposes, and may not always return the full size of the file (I have seen some instances where the value returned was always 0). Anyway, here it goes:
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.jar.JarFile;
    import java.util.zip.ZipEntry;
    public class JARFileReading
         public static void main(String[] args)
              String fileName = "res/TestApplet.html";
              InputStream is = JARFileReading.class.getResourceAsStream(fileName);
              try
                   System.out.println("Input Stream.avaliable: "+is.available());
                   if (is != null)
                        is.close();
              } catch (IOException e)
                   e.printStackTrace();
              File jarFile = new File("JarFile.jar");
              if (!jarFile.exists()) System.exit(0);
              JarFile theJar = null;
              try
                   theJar = new JarFile(jarFile);
                   ZipEntry theFile = theJar.getEntry(fileName);
                   System.out.println("Zip Entry.getSize: "+theFile.getSize());
                   System.out.println("Zip Entry.getCompressedSize: "+theFile.getCompressedSize());
                   is = theJar.getInputStream(theFile);
                   System.out.println("JarFile.getInputStream.available: "+is.available());
                   if (theJar != null)
                        theJar.close();
                   if (is != null)
                        is.close();
              } catch (IOException e1)
                   e1.printStackTrace();
    }

  • Reading an xml file from a jar file

    Short question:
    Is it possible to read an xml file from a jar file when the dtd is
    placed inside the jar file? I am using jdom (SAXBuilder) and the default
    sax parser which comes with it.
    Long Question:
    I am trying to create an enterprise archive file on Weblogic 6.1. We
    have a framework that is similar to the struts framework which uses it's
    own configuration files
    I could place the dtd files outside the jar ear file and specify the
    absolute path in an environment variable in web.xml which is
    configurable through the admin console.
    But I want to avoid this step and specify a relative path within the jar
    file.
    I have tried to use a class which implements the entityresolver as well
    as try to extend the saxparser and set the entity resolver within this
    class explicitly, but I always seem to sun into problems like:
    The setEntityresolver method does not get called or there is a
    classloader problem. i.e. JDOM complains that it cannot load My custom
    parser which is part of the application
    Vijay

    Please contact the main BEA Support team [email protected]
    They will need to check with product support to determine
    the interoperatablity of Weblogic Server with these other
    products.

  • Reading a Compressed File from a ZIP File, which is an entry of ZIP File

    Hello, Experts,
    Would it be possible somebody to help me with code example for the following problem?
    I want to read a compressed file from a ZIP file, which is an entry of ZIP File, without extacting/writing files on file system. Is this possible?
    Lets say we have a ZipFile1. There is ZipFile2 inside ZipFile1. And inside ZipFile2 is FileA. The scenario is reading FileA without extracting on file system.
    Thank you in advance for your help.
    Cheers
    RADY

    The classes you want to be using are java.util.zip.ZipInputStream and and ZipEntry from the same package. Construct the ZipInputStream with the input you have for the outer zip. Loop with ZipInputStream.getNextEntry until you find the inner zip you're looking for - that method returns a ZipEntry, and you get the name of the zip entry with ZipEntry.getName. Read the ZipInputStream from that point into some buffer, and read that buffer into a new ZipInputStream - rinse and repeat until you have the contents of the file in the zip in the zip...

  • Reading external xml files from a jar

    Hi,
    I am trying to read an xml file from a jar (which is not present inside the jar ) .
    I am passing the file name as a string (like C:/folder/filename) to the SAXBuilder but it throwing
    unknown protocol: c error.
    i tried using an url , tried using a relative path but to no use.
    Need help in this regard urgently.
    TIA,
    Regards,
    Harsha

    Hi,
    Actually, my application needs to read two xml files , parse it, perform some operation and write
    the result to an output file.
    The names of the two xml files i mentioned, are specified in a properties file as absolute paths. (I even tried converting them to URIs)
    The xml files are in the same directory as the jar ( i dont know if it should matter as i am giving the absolute path).
    The main class reads the names of the files and passes the names as strings to the SAXBuilder.
    This is where i am getting an exception.
    Going by what you said, is it not possible for a java class to read a fie outside of the jar ? Is there no way to do this ? And right now i am not sure of how to go about this or if there's any work around . Any help would be appreciated.
    Kindly reply at the earliest
    TIA,
    Harsha

  • How to read a text file from a Jar file

    Hi
    How can I read a text file from a Jar file?
    Thanx in advance..

    thanx
    helloWorld it works.damn, I didn't remove it fast enough. Even if it is urgent, it is best not to mention it, telling people just makes them take longer.

  • How do I read a text file in a Jar Executable?

    Hello All,
    I have a need to package a text file into a Jar Exectable file and then have my java code read that text file in using a BufferedReader. Can anyone out there tell me how to do this? My first question is how do I reference this file by the path name? O, I'm also using Windows.
    Thanks for your help.
    Karl

    I have the same problem with kportner . I did by joop_eggen but I got the error at:
    BufferedReader in = new InputStreamReader(is);Can't conver from BufferedReader to InputStreamReader
    When I up my applet to server and get it from client. My applet couldn't read the text file.
    Any one help me!
    Thanks.

  • Unable to read a .gif file from a jar

    I have made an application which requires some .gif images.
    I packed all the classes and .gif's in a jar file.
    I have used no package statement in files, all are in one folder.
    While retrieving the .gif file from the jar I have used..
    Image img1 = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("Lock.gif"));
    ImageIcon i1=new ImageIcon(img1,"No Users");
    but this does not works and throws nullpointer exception.
    I have also tried getClass().getResource() etc.
    But none of the methods are working..
    Plz. help.
    Nimesh

    Is the class that is loading the image contained in a package? If the resource name does not start with a forward slash, it is interpreted as relative to the package of your class(when doing getClass().getResource())

  • Reading a text file in a jar

    I make an application that need to acces to a small text file within a jar file.
    Never used it before, and don't really know how to do it correctly.
    could anyone show me the way to create this small part of code ?
    Thanks.

    Just use the getResource or getResourceAsStream. They exist in the java.lang.Class class (which actually call the current ClassLoader which has the same methods).
    An example follows below, the text file should be added to the root of the jar file.
    Don't forget the first / in the filename, if you do the method will return null..
    ----- Code begin -----
    import java.io.*;
    public class Test
         public static void main(String args[]) throws Exception
              InputStream in = Test.class.getResourceAsStream("/test.txt");
              BufferedReader reader = new BufferedReader(new InputStreamReader(in));
              System.out.println(reader.readLine());
              reader.close();
    ----- Code end -----
    Good luck,
    Daniel

  • To read a gif file from a jar

    I have made an application which requires some .gif images.
    I packed all the classes and .gif's in a jar file.
    I have used no package statement in files, all are in one folder.
    While retrieving the .gif file from the jar I have used..
    Image img1 = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("Lock.gif"));
    ImageIcon i1=new ImageIcon(img1,"No Users");
    but this does not works and throws nullpointer exception.
    I have also tried getClass().getResource() etc.
    But none of the methods are working..
    Plz. help.
    Nimesh

    Hi,
    Use a / with the name of your gif e.g.
    frame().setIconImage((new ImageIcon(getClass().getResource("/myimage.gif")).getImage()));

  • Read a text File inside a JAR

    I want to create a plain text file and pack into a JAR file together with my Java Applet such that I can read something inside the flat file as a parameter for the Java Applet. Is that possible to do that? Any sample code?

    Look at Class.getResourceAsStream()
    It'll let you load non-Java resources from the Jar file.
    Then, you'll have an input stream to read directly.
    Probably something along the lines of
         // to load as text file
         // Note : assumes that myfile.txt is in root directory of jar file
         InputStream is = getClass().getResourceAsStream ("/myfile.txt");
         BufferedReader bufReader = new BufferedReader ( new InputStreamReader ( is );
         // to load as properties file
         InputStream is = getClass().getResourceAsStream ("/myfile.txt");
         Properties myProps = new Properties();
         myProps.load ( is );regards,
    Owen

  • How to read a .wav file from a .jar  - Please Help!!!

    Hello,
    I know this is a stupid question but I keep having trouble doing this. My code is the following:
        Display display;
        Alert alert;
        InputStream stream;
        Player player;
    stream = getClass().getResourceAsStream("newalert.wav");
                player = Manager.createPlayer(stream, "audio/wav");
                player.prefetch();
                player.start();
    ...I'm running this Midlet on a Motorola A1000 and I keep getting the following exception:
    java.lang.IllegalArgumentException:
    stream is null
    I've created the .jad package using KToolBar of the J2ME WTK2.2 with the appropriate .wav file on res folder.
    What I'm doing wrong?
    Thanks,
    Paulo

    Hi
    If your stream is null, then the wav file cannot be reached. Try this code:
    stream = getClass().getResourceAsStream("/newalert.wav");If this doesn't work either, make sure your wav file is directly in the res directory and not in another subdirectory of it. I suggets not running your application directly from the phone until it works from the WTK emulator.
    Mihai

  • Reading an xml file from jar

    I have an xml file in a jar file other than the class that needs to read it. Most of teh posts I've seen reccomend using class.getResource to get access to the jar, but since the file is in a different jar this wont work.
    I load classes using URLClassLoader and explicitely access the class I need from a specific jar by creating a url thusly....
    jar:file:/" + workingDirectory + "/folder/jarfile.jar!/specificfolder/specificfile.class"
    Is there a similar thing for reading an xml file for a jar?

    Have you tried:
    "jar:file:/" + workingDirectory + "/folder/jarfile.jar!/specificfolder/specificfile.xml"
    ;o)
    V.V.

  • Reading a text file in archive in jar

    Hi,
    I am unable to read a text file in my jar archive.
    Example code:
    try
    InputConnection con =
    (InputConnection) Connector.open("file://text/info.txt", Connector.READ);
    InputStream in = con.openInputStream();
    StringBuffer buf = new StringBuffer();
    int ch;
    while((ch = in.read()) != -1 )
    buf.append((char) ch);
    in.close();
    form.append(buf.toString());
    } catch (Exception e) { e.printStackTrace(); }
    Any ideas how I can do that?
    Thanks.

    You have to use java.lang.Class`s getResourceAsStream method. Your code:
    try
    StringBuffer buf;
    Class mc = buf.getClass();
    InputStream in = mc.getResourceAsStream("/text/info.txt");
    buf = new StringBuffer();
    int ch;
    while((ch = in.read()) != -1 )
    buf.append((char) ch);
    in.close();
    form.append(buf.toString());
    } catch (Exception e) { e.printStackTrace(); }

  • Reading a file in the JAR

    i have a jar with class files and xml files in it. my application uses this jar and a particular class has a link to the xml. but while compiling, it shows path not found. the application couldnt read the xml file in the jar. how to set the path or read the xml file
    XML file in JAR:
    SQLMaps/admin/sample.xml [folder structure inside the jar file]
    my application has a path like SQLMaps/admin/sample.xml
    but how do we point to the xml file in the jar
    Regards
    Pradheep

    thanks anyway
    but i need the application to read the xml file in the jar file
    to be more clear.
    the jar file has xml in a specified folder.

Maybe you are looking for