Reading image files with embedded spaces in file name from jar

Discussion in 'Java' started by Rhino, Feb 7, 2005.

  1. Rhino

    Rhino Guest

    I am working on a program that will get icon files from a jar. I have the
    code to get and read those files working satisfactorily except for one
    thing: if the file name (of the image file, as opposed to the file name of
    the jar) contains an imbedded blank, my reading logic fails.

    For example, if the jar is called My.jar and the icon file I want is called
    Foo.gif, everything works fine. But if the icon file is called Foo Bar.gif,
    my logic fails. I would like my code to work even if embedded blanks occur
    in the file name but I'm not sure how to accomplish it. I've googled but
    haven't found anything that solves the problem.

    Here is the relevant fragment of my code:

    try {
    URL url = this.getClass().getResource( gifName );
    if ( url == null ) {
    System.err.println("File " + gifName + " not found.");
    }
    Image image =
    Toolkit.getDefaultToolkit().createImage((ImageProducer) url.getContent() );
    // wait till it is loaded
    MediaTracker tracker;
    try {
    tracker = new MediaTracker( this );
    tracker.addImage( image, 0 );
    tracker.waitForID(0);
    }
    catch ( InterruptedException i_excp ) {
    System.err.println("Oops, failed to load image into Media
    Tracker.");
    }
    return image ;
    }
    catch ( IOException io_excp )
    {
    System.err.println("Oops, got I/O error.");
    }

    If gifName contains an embedded blank, I set the URL okay (it is NOT null),
    but url.getContent() blows up; I get:

    java.io.FileNotFoundException: JAR entry Images/Checkered%20Flag.jpg not
    found in E:\eclipse\3.0.1\eclipse\workspace\Project1\jarProject1Media.jar

    I'm certain that the jar contains a file called Images/Checkered Flag.jpg.

    How do I improve this code so that it can read an image file when the file
    name contains embedded spaces? (I assume that I will also have problems if
    the jar file name contains embedded blanks so if you know now to make my
    code handle THAT situation properly too, I'd appreciate hearing the
    solution!)

    I am using J2SE 1.5 on Windows XP.

    --
    Rhino
    ---
    rhino1 AT sympatico DOT ca
    "There are two ways of constructing a software design. One way is to make it
    so simple that there are obviously no deficiencies. And the other way is to
    make it so complicated that there are no obvious deficiencies." - C.A.R.
    Hoare
    Rhino, Feb 7, 2005
    #1
    1. Advertising

  2. Rhino

    Guest

    What about substituting %20 for spaces before you try to read the image?
    , Feb 7, 2005
    #2
    1. Advertising

  3. Rhino

    Rhino Guest

    <> wrote in message
    news:...
    > What about substituting %20 for spaces before you try to read the image?
    >

    I'm not sure exactly what you mean.

    I tried this but it failed; I got a null on the URL:

    String encodedFileName = null;
    try {
    encodedFileName = URLEncoder.encode(gifName, "UTF-8");
    } catch (UnsupportedEncodingException ue_excp) {
    System.err.println("Failed to encode file name " + gifName + ".
    Cannot continue.");
    return null;
    }

    try {
    URL url = this.getClass().getResource(encodedFileName);
    if (url == null) {
    System.err.println("File " + gifName + " not found.");
    return null;
    }

    Image image =
    Toolkit.getDefaultToolkit().createImage((ImageProducer) url.getContent());
    MediaTracker tracker;
    try {
    tracker = new MediaTracker(this);
    tracker.addImage(image, 0);
    tracker.waitForID(0);
    } catch (InterruptedException i_excp) {
    System.err.println("Oops, failed to load image into Media
    Tracker.");
    }
    return image;
    } catch (IOException io_excp) {
    System.err.println("Oops, got I/O error.");
    return null;
    }

    Did you mean something else??

    Rhino
    Rhino, Feb 7, 2005
    #3
  4. Rhino

    Roland Guest

    On 7-2-2005 1:45, Rhino wrote:

    > I am working on a program that will get icon files from a jar. I have the
    > code to get and read those files working satisfactorily except for one
    > thing: if the file name (of the image file, as opposed to the file name of
    > the jar) contains an imbedded blank, my reading logic fails.
    >
    > For example, if the jar is called My.jar and the icon file I want is called
    > Foo.gif, everything works fine. But if the icon file is called Foo Bar.gif,
    > my logic fails. I would like my code to work even if embedded blanks occur
    > in the file name but I'm not sure how to accomplish it. I've googled but
    > haven't found anything that solves the problem.
    >
    > Here is the relevant fragment of my code:
    >
    > try {
    > URL url = this.getClass().getResource( gifName );
    > if ( url == null ) {
    > System.err.println("File " + gifName + " not found.");
    > }
    > Image image =
    > Toolkit.getDefaultToolkit().createImage((ImageProducer) url.getContent() );
    > // wait till it is loaded
    > MediaTracker tracker;
    > try {
    > tracker = new MediaTracker( this );
    > tracker.addImage( image, 0 );
    > tracker.waitForID(0);
    > }
    > catch ( InterruptedException i_excp ) {
    > System.err.println("Oops, failed to load image into Media
    > Tracker.");
    > }
    > return image ;
    > }
    > catch ( IOException io_excp )
    > {
    > System.err.println("Oops, got I/O error.");
    > }
    >
    > If gifName contains an embedded blank, I set the URL okay (it is NOT null),
    > but url.getContent() blows up; I get:
    >
    > java.io.FileNotFoundException: JAR entry Images/Checkered%20Flag.jpg not
    > found in E:\eclipse\3.0.1\eclipse\workspace\Project1\jarProject1Media.jar
    >
    > I'm certain that the jar contains a file called Images/Checkered Flag.jpg.
    >
    > How do I improve this code so that it can read an image file when the file
    > name contains embedded spaces? (I assume that I will also have problems if
    > the jar file name contains embedded blanks so if you know now to make my
    > code handle THAT situation properly too, I'd appreciate hearing the
    > solution!)
    >
    > I am using J2SE 1.5 on Windows XP.
    >

    Rhino,

    There seems to be a bug in J2SE 1.5 for accessing entries in a jar file
    which contain a space character.

    I created a jar with your TempAbout app and an image whose name contains
    two spaces ('duke on swing.gif').

    When I run this Jar with J2SE 1.4, the image is displayed, and the
    printed image url shows:
    <jar:file:/F:/eclipse/workspace/EmbeddedSpaces/EmbeddedSpaces.jar!/images/duke
    on swing.gif>

    However, when I run it with J2SE 1.5, the image does not show up; the
    image url prints as:
    <jar:file:/F:/eclipse/workspace/EmbeddedSpaces/EmbeddedSpaces.jar!/images/duke%20on%20swing.gif>

    It might be worthwhile to search the Java bugdatabase for this problem,
    or submit the bug yourself.

    The apparent work-around is to use files whose name don't contain space
    characters (or any other characters that get URL escaped as %xx).

    HTH,
    --
    Regards,

    Roland de Ruiter
    ___ ___
    /__/ w_/ /__/
    / \ /_/ / \
    Roland, Feb 7, 2005
    #4
  5. Rhino

    Rhino Guest

    "Roland" <> wrote in message
    news:42075cd5$0$28983$4all.nl...
    > On 7-2-2005 1:45, Rhino wrote:
    >
    > > I am working on a program that will get icon files from a jar. I have

    the
    > > code to get and read those files working satisfactorily except for one
    > > thing: if the file name (of the image file, as opposed to the file name

    of
    > > the jar) contains an imbedded blank, my reading logic fails.
    > >
    > > For example, if the jar is called My.jar and the icon file I want is

    called
    > > Foo.gif, everything works fine. But if the icon file is called Foo

    Bar.gif,
    > > my logic fails. I would like my code to work even if embedded blanks

    occur
    > > in the file name but I'm not sure how to accomplish it. I've googled but
    > > haven't found anything that solves the problem.
    > >
    > > Here is the relevant fragment of my code:
    > >
    > > try {
    > > URL url = this.getClass().getResource( gifName );
    > > if ( url == null ) {
    > > System.err.println("File " + gifName + " not found.");
    > > }
    > > Image image =
    > > Toolkit.getDefaultToolkit().createImage((ImageProducer)

    url.getContent() );
    > > // wait till it is loaded
    > > MediaTracker tracker;
    > > try {
    > > tracker = new MediaTracker( this );
    > > tracker.addImage( image, 0 );
    > > tracker.waitForID(0);
    > > }
    > > catch ( InterruptedException i_excp ) {
    > > System.err.println("Oops, failed to load image into Media
    > > Tracker.");
    > > }
    > > return image ;
    > > }
    > > catch ( IOException io_excp )
    > > {
    > > System.err.println("Oops, got I/O error.");
    > > }
    > >
    > > If gifName contains an embedded blank, I set the URL okay (it is NOT

    null),
    > > but url.getContent() blows up; I get:
    > >
    > > java.io.FileNotFoundException: JAR entry Images/Checkered%20Flag.jpg not
    > > found in

    E:\eclipse\3.0.1\eclipse\workspace\Project1\jarProject1Media.jar
    > >
    > > I'm certain that the jar contains a file called Images/Checkered

    Flag.jpg.
    > >
    > > How do I improve this code so that it can read an image file when the

    file
    > > name contains embedded spaces? (I assume that I will also have problems

    if
    > > the jar file name contains embedded blanks so if you know now to make my
    > > code handle THAT situation properly too, I'd appreciate hearing the
    > > solution!)
    > >
    > > I am using J2SE 1.5 on Windows XP.
    > >

    > Rhino,
    >
    > There seems to be a bug in J2SE 1.5 for accessing entries in a jar file
    > which contain a space character.
    >
    > I created a jar with your TempAbout app and an image whose name contains
    > two spaces ('duke on swing.gif').
    >
    > When I run this Jar with J2SE 1.4, the image is displayed, and the
    > printed image url shows:
    >

    <jar:file:/F:/eclipse/workspace/EmbeddedSpaces/EmbeddedSpaces.jar!/images/du
    ke
    > on swing.gif>
    >
    > However, when I run it with J2SE 1.5, the image does not show up; the
    > image url prints as:
    >

    <jar:file:/F:/eclipse/workspace/EmbeddedSpaces/EmbeddedSpaces.jar!/images/du
    ke%20on%20swing.gif>
    >
    > It might be worthwhile to search the Java bugdatabase for this problem,
    > or submit the bug yourself.
    >
    > The apparent work-around is to use files whose name don't contain space
    > characters (or any other characters that get URL escaped as %xx).
    >

    Ahhh! That theory would explain why I haven't had any luck reading a file
    that has embedded blanks in its name if the file is in a jar despite many
    different attempts. The possibility that this is a new bug in J2SE 1.5 may
    also explain why I haven't seen any posts mentioning this problem; if not
    many people are using 1.5 yet and most of them don't use jars whose files
    have embedded blanks, maybe I'm the first to come across this problem....

    I'll have a look on the bug database and see what I can find.

    Thanks, Roland!

    Rhino
    Rhino, Feb 7, 2005
    #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. Eric McIntyre

    Jar files in Jar files

    Eric McIntyre, May 24, 2004, in forum: Java
    Replies:
    1
    Views:
    471
    Roedy Green
    May 24, 2004
  2. Arnold Peters
    Replies:
    0
    Views:
    564
    Arnold Peters
    Jan 5, 2005
  3. muttley
    Replies:
    0
    Views:
    2,714
    muttley
    Oct 20, 2005
  4. cyberco
    Replies:
    4
    Views:
    3,745
    Roedy Green
    Feb 14, 2006
  5. Arnold Peters
    Replies:
    0
    Views:
    653
    Arnold Peters
    Jan 5, 2005
Loading...

Share This Page