This discussion is archived
13 Replies Latest reply: Oct 17, 2010 10:24 AM by 796440 RSS

Reading serialized object from the JAR archive

803815 Newbie
Currently Being Moderated
How can I read a serialized object file from my JAR archive?
(There were many exceptions regarding the classpath\URI\URL I had. In addition, there is no topic (at any forum) that succeeded to deal with that - only flooded topics without answers.)

Please help.

EDIT:
Handling as JARFile:
            FileInputStream fis = null;
            ObjectInputStream in = null;
            JarFile jarFile = new JarFile(jarName);
            JarEntry entry = jarFile.getJarEntry(filename);
            fis = (FileInputStream) jarFile.getInputStream(entry);
            in = new ObjectInputStream(fis);
            sl = (SealedObject) in.readObject();
fis = (FileInputStream) jarFile.getInputStream(entry);  => Uncastable

Handeling with ClassLoader:
   fis = new FileInputStream(ClassLoader.getSystemResource("geniestills/"+filename).getFile());
   in = new ObjectInputStream(fis);
=>
java.io.FileNotFoundException: file:\C:\Users\me\Documents\NetBeansProjects\proj\proj.jar!\proj\obj.proj(The filename, directory name, or volume label syntax is incorrect)
Edited by: 800812 on Oct 16, 2010 11:50 AM

Edited by: 800812 on Oct 17, 2010 2:38 AM
  • 1. Re: Reading serialized object from the JAR archive
    796440 Guru
    Currently Being Moderated
    800812 wrote:
    How can I read a serialized object file from my JAR archive?
    By using Class.getResourceAsStream (if it's on the classpath) or by using java.util.zip (if it's on the file system) and then reading from the returned input stream and using ObjectInputStream to deserialize it.
    (There were many exceptions regarding the classpath\URI\URL I had.
    You'll need to fix them.
    In addition, there is no topic (at any forum) that succeeded to deal with
    I highly, highly doubt that. And since you mention other forums, if you've posted this question elsewhere, you'll need to go to each place you posted and provide links to all the others. People really hate wasting their time answering a question that somebody else has already answered. If you post the links, we can see the other discussions and make an informed decision on whether our answers add anything new.
  • 2. Re: Reading serialized object from the JAR archive
    803815 Newbie
    Currently Being Moderated
    "...if you've posted this question elsewhere... " No.

    "By using Class.getResourceAsStream (if it's on the classpath) or by using java.util.zip (if it's on the file system) and then reading from the returned input stream and using ObjectInputStream to deserialize it." Tested SSCE would be helpful.

    EDIT: Many methods were tested and nothing worked. Note that since we're reading from a JAR archive, the path of the JAR shouldn't effect the reading, thus the "toFile" & "toURL" which include
    the full path of the JAR, DO have effect on the normal work of the program and do not allow to apply the getResourceAsStream for getting FileInputStram for the sealed object.

    Edited by: 800812 on Oct 16, 2010 11:30 AM
  • 3. Re: Reading serialized object from the JAR archive
    796440 Guru
    Currently Being Moderated
    800812 wrote:
    "...if you've posted this question elsewhere... " No.

    "By using Class.getResourceAsStream (if it's on the classpath) or by using java.util.zip (if it's on the file system) and then reading from the returned input stream and using ObjectInputStream to deserialize it." Tested SSCE would be helpful.
    It doesn't work that way. This is not a code service. If you want examples, I guarantee you they're easy to find on google. Maybe not for your exact situation, but for enough of the pieces that you should be able to figure out how to put it together.

    The SSCCE has to come from you. You take your best shot, post what you have, and clearly and precisely describe what problem you're having.
  • 4. Re: Reading serialized object from the JAR archive
    803815 Newbie
    Currently Being Moderated
    Please see edited message.
  • 5. Re: Reading serialized object from the JAR archive
    793415 Pro
    Currently Being Moderated
    800812 wrote:
    Please see edited message.
    Please use code tags.

    Also note that is SSCCE (2 x 'C'), and what you posted - isn't. For details on the SSCCE see http://pscode.org/sscce.html.
  • 6. Re: Reading serialized object from the JAR archive
    796440 Guru
    Currently Being Moderated
    800812 wrote:
    EDIT: Many methods were tested and nothing worked.
    Then you have to fix what you were doing wrong. If you think that's a rather vague and unhelpful answer, all I can tell you is that it is exactly in line with the amount of detail you provided in your question. If, on the other hand, you had done as suggested and provided an SSCCE, then we could have seen what you tried and what went wrong and provided you with more concrete advice.
    Note that since we're reading from a JAR archive, the path of the JAR shouldn't effect the reading, thus the "toFile" & "toURL" which include
    What you think about what should and should not happen is completely irrelevant to what is actually happening.
  • 7. Re: Reading serialized object from the JAR archive
    803815 Newbie
    Currently Being Moderated
    I have to read a serialized object from a jar file as I mentioned (Have anybody achieved that?).
    For instance, given the following code:
          String filename = "savedObj.obj";
            FileInputStream fis = null;
            ObjectInputStream in = null;
             try
            {
             fis = new FileInputStream(ClassLoader.getSystemResource("myProject/"+filename).getFile());
    
             in = new ObjectInputStream(fis);
             arr= (ArrayList<int[]>)in.readObject();
             in.close();
             fis.close();
           }
                    catch (ClassNotFoundException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }       catch(IOException ex)
           {
             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
           }
    Output (the file is at project.jar\myProject\savedObj.ob):
    SEVERE: null
    java.io.FileNotFoundException: file:\C:\Users\me\Documents\NetBeansProjects\project\project.jar!\myProject\savedObj.obj (The filename, directory name, or volume label syntax is incorrect
    )
            at java.io.FileInputStream.open(Native Method)
            at java.io.FileInputStream.<init>(Unknown Source)
            at java.io.FileInputStream.<init>(Unknown Source)
            at project.Project.load(Project.java:100)
            at project.Main.main(Main.java:34)
    How can I solve this?
    Thanks.
  • 8. Re: Reading serialized object from the JAR archive
    796440 Guru
    Currently Being Moderated
    800812 wrote:
    I have to read a serialized object from a jar file as I mentioned (Have anybody achieved that?).
    Plenty have, I'm sure. It's not a big deal. Just do the opposite of what was done to write it.
    fis = new FileInputStream(ClassLoader.getSystemResource("myProject/"+filename).getFile());
    java.io.FileNotFoundException: file:\C:\Users\me\Documents\NetBeansProjects\project\project.jar!\myProject\savedObj.obj (The filename, directory name, or volume label syntax is incorrect
    )
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at project.Project.load(Project.java:100)
    at project.Main.main(Main.java:34)
    How can I solve this?
    Thanks.
    The error message is pretty clear on what the problem is. Forget classloaders and resources for a moment. Does new File{"C:/a/b.jar!/x.txt") work for a file x.txt in the appropriate jar? Is there anything in the docs to suggest that it should?

    Use getResourceAsStream, not getResource, and get File and all FileXxx stuff out of there completely. You want java.util.jar.JarInputStream and JarEntry.
  • 9. Re: Reading serialized object from the JAR archive
    793415 Pro
    Currently Being Moderated
    800812 wrote:
    ..
    How can I solve this?
    Perhaps you should start by improving your debugging skills. When code is failing it pays to break it down into smaller statements in order to determine exactly what is going wrong.

    OTOH I can make these general recommendations:
    ..
    fis = new FileInputStream(ClassLoader.getSystemResource("myProject/"+filename).getFile());
    ..
    1) The JavaDocs for getSystemResource state..
    Find a resource of the specified name from the search path used to load classes.
    (highlight is mine) It is not intended to find serialized objects, HTML, images or other things loaded into Jars. They might well still be found, but that is not what the method is designed for.
    2) Stop trying to treat resources inside Jars as though they are File objects, they are not. Instead, a better approach would be along the lines of..
    InputStream is = this.getResourceAsStream("myProject/"+filename);
    // sanity check!
    System.out.println("InputStream: "  + is);
    ...
  • 10. Re: Reading serialized object from the JAR archive
    803815 Newbie
    Currently Being Moderated
    (highlight is mine) It is not intended to find serialized objects, HTML, images or other things loaded into Jars.
    Stop trying to treat resources inside Jars as though they are File objects, they are not.
    However, this is what I need - to get the file object from the jar.
    I also tried this:
    FileInputStream fis = null;
    ObjectInputStream in = null;
    JarFile jarFile = new JarFile(jarName);
    JarEntry entry = jarFile.getJarEntry(filename);
    fis = (FileInputStream) jarFile.getInputStream(entry);
    in = new ObjectInputStream(fis);
    sl = (SealedObject) in.readObject();
    But casting is not allowed here.
     fis = (FileInputStream) jarFile.getInputStream(entry);  
    Getting a file object from a JarEntry\ZipEntry was a fail (for me). Maybe there is a good solution available (with byte arrays?) that can be done with JarEntry\ZipEntry, but I didn't find one.

    Edited by: 800812 on Oct 17, 2010 2:26 AM
    I didn't see jverd's answer, my bad.
  • 11. Re: Reading serialized object from the JAR archive
    DarrylBurke Guru Moderator
    Currently Being Moderated
    jverd wrote:
    You want java.util.jar.JarInputStream and JarEntry.
    Andrew Thompson wrote:2) Stop trying to treat resources inside Jars as though they are File objects, they are not.
    800812 wrote:Getting a file object from a JarEntry\ZipEntry was a fail (for me). Maybe there is a good solution available
    :headdesk:

    db
  • 12. Re: Reading serialized object from the JAR archive
    EJP Guru
    Currently Being Moderated
    Stop trying to treat resources inside Jars as though they are File objects, they are not.
    However, this is what I need - to get the file object from the jar.
    If you're going to post here it is customary and efficient and courteous to also read the answers. You don't need that. You need an input stream to the file's contents, and you've been told how to get that. You then need an ObjectInputStream and you are practically done.
    I also tried this:
    Thats just wasting time. You had already been told not to use File, Filexxxx, etc.
    fis = (FileInputStream) jarFile.getInputStream(entry);
    The result of that method isn't a FileInputStream. See the Javadoc. So don't try to cast it to one. Just use the return type documented in the Javadoc. Don't make things up.
  • 13. Re: Reading serialized object from the JAR archive
    796440 Guru
    Currently Being Moderated
    800812 wrote:
    However, this is what I need - to get the file object from the jar.
    No, you don't need that.
    Getting a file object from a JarEntry\ZipEntry was a fail (for me).
    Of course, because there are no File objects there. File objects represent paths to files on the file system, not in a jar file. You don't need a File object here. Why do you think you do?

    Edited by: jverd on Oct 17, 2010 10:24 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points