Results 1 to 9 of 9
  1. #1
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default Couldn't load my image from the jar file

    Hi guys,
    I've run into a jar packaging problem. I would like to add some images to my program. You can find my simplified code below.
    I use console application (so I can see what happens in the "background";) ) First I create a class file, subsequently run my application. Up to now everything is OK.
    After that, I want to package my class and images files into a jar archive file. (I would like give my little application to others, and they can run my app in their machine.)

    I used this command:

    jar -cfv Demo.jar ImagesToJar.class imgs

    and here was the result of the output:

    added manifest
    adding: ImagesToJar.class(in = 1337) (out= 813)(deflated 39%)
    adding: imgs/(in = 0) (out= 0)(stored 0%)
    adding: imgs/drum.gif(in = 617) (out= 511)(deflated 17%)
    adding: imgs/Pluto.gif(in = 2159) (out= 2122)(deflated 1%)
    adding: imgs/skate.gif(in = 359) (out= 349)(deflated 2%)


    My images files are in the imgs subdirectory which is under the same directory with the class file. I would like to store the images in separate directory.

    After packaging I edited the manifest file, I added the main-class name to the file.
    Here is the content of my manifest:

    Manifest-Version: 1.0
    Created-By: 1.6.0 (Sun Microsystems Inc.)
    Main-Class: ImagesToJar

    When I run the jar application, i noticed the image file couldn't load on the frame. It seems my frame freezed after start the jar file.
    Does anyone know what I'm doing wrong and how I might solve this issue? Might I set the Class-Path value in Manifest file?

    I suppose there are some hocus-pocus around my jar structure, so I enclosed my image file. You can reproduce easily my problem to create the imgs directory next to my class file, and put my image into it.
    I hope it helps to discover my problem.


    Here is my simplified code: (If i omit the drawImage line, frame would display correctly without the image.)

    Any suggestion would be great
    Very thanks.

    Java Code:
    import javax.swing.*;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import java.awt.Graphics;
    
    public class ImagesToJar extends JPanel{
    
    public void paintComponent(Graphics g)
    {
    	BufferedImage img;
    	try{
    	img = ImageIO.read(getClass().getResource("imgs\\skate.gif")); //IT CAUSES MY PROBLEM
    	g.drawImage(img,20,20,this);
    	}
    	catch(java.io.IOException ioe)
    	{
    		ioe.printStackTrace();
    	}
    	g.drawString("demo	 for testing",30,10);
    }
    
    public static void main(String[] args)
    {
    	ImagesToJar itj = new ImagesToJar();
    	JFrame frame = new JFrame("gui with images...");
    	frame.getContentPane().add(itj);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setSize(400,400);
    	
    	frame.setVisible(true);
    }
    
    }
    Attached Thumbnails Attached Thumbnails Couldn't load my image from the jar file-skate.gif  

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default Re: Couldn't load my image from the jar file

    Very thanks Fubarable, your suggestion is engraved to my head. I will use a single Forward-Slash in the file path. (But the javac and java commands get on well with the double BackSlash. It's strange for me:) )
    This problem is solved, but I have found some images which couldn't load in jar file.

    I start a new post for this issue.Some "special gif file" couldn't load from my jar file

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Couldn't load my image from the jar file

    But the javac and java commands get on well with the double BackSlash. It's strange for me:)
    When using javac, you're in the goofy windows command line, so you need to stick to those non-standard \ vs / conventions. Java does it the way everything else in the world does it (with a / ) so internally (programmatically and not from the windows command line), you use / as a directory delimiter. On non-windows systems, you would use / everywhere.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Couldn't load my image from the jar file

    Quote Originally Posted by quad64bit View Post
    On non-windows systems, you would use / everywhere.
    Erm. Open VMS uses the dot (.), Tops 20 (DEC) used it too; the old Mac OS used a colon (:) and VOS (IBM) uses a > sign. I know, it's a mess everywhere ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Couldn't load my image from the jar file

    Yeah, but Jos, how much of that stuff is in use anymore? I still have some old system 7 macs in a closet somewhere, but come on. OS X has been around for 10+ years. Even our VAX at the university was retired almost 20 years ago! :D

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Couldn't load my image from the jar file

    Quote Originally Posted by quad64bit View Post
    When using javac, you're in the goofy windows command line, so you need to stick to those non-standard \ vs / conventions.
    Not unless you're on some old non-supported Windows version, which actually ran on top of DOS and had a "MSDOS Prompt" feature. In all current versions, the a "Command Prompt" launched from cmd.exe accept both \ and / and even a mix of them in the same path. Just verified:
    Java Code:
    Microsoft Windows [Version 6.1.7601]
    Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
    
    C:\Users\Darryl>e:
    
    E:\>cd artwork/cards\50
    
    E:\Artwork\Cards\50>
    The old-style prompt launched from command.com requires the\ as a path separator.

    But the javac and java commands get on well with the double BackSlash.
    Double backslash?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Couldn't load my image from the jar file

    TIL something new! Thank you Darryl!

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Couldn't load my image from the jar file

    Quote Originally Posted by DarrylBurke View Post
    In all current versions, the a "Command Prompt" launched from cmd.exe accept both \ and / and even a mix of them in the same path. Just verified:[code]Microsoft Windows [Version 6.1.7601]
    Yep, but there's a small gotcha again: path completion (the TAB key) doesn't work with foreward slashes while it does work with back slashes. Who on earth invented that?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Load image from jar
    By sublixt in forum Java 2D
    Replies: 2
    Last Post: 10-17-2011, 12:15 AM
  2. Error: cannot load image .
    By iswan in forum Java Applets
    Replies: 1
    Last Post: 09-29-2011, 02:26 AM
  3. Load image to jPanel
    By DJIT in forum AWT / Swing
    Replies: 7
    Last Post: 12-27-2010, 12:48 PM
  4. ImageIcon won't load my image
    By JaiRaj in forum AWT / Swing
    Replies: 5
    Last Post: 03-04-2010, 06:35 PM
  5. Help with load image
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 07:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •