Results 1 to 9 of 9
- 12-27-2011, 03:19 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
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); } }
-
Re: Couldn't load my image from the jar file
Don't use \\ in a jar file. Use / instead.
- 12-28-2011, 06:43 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
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
- 12-28-2011, 08:21 PM #4
Re: Couldn't load my image from the jar file
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.But the javac and java commands get on well with the double BackSlash. It's strange for me:)
- 12-28-2011, 09:20 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: Couldn't load my image from the jar file
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-29-2011, 09:32 PM #6
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
- 12-29-2011, 10:16 PM #7
Re: Couldn't load my image from the jar file
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:
The old-style prompt launched from command.com requires the\ as a path separator.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>
Double backslash?But the javac and java commands get on well with the double BackSlash.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-30-2011, 03:41 PM #8
Re: Couldn't load my image from the jar file
TIL something new! Thank you Darryl!
- 12-30-2011, 03:54 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: Couldn't load my image from the jar file
When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Load image from jar
By sublixt in forum Java 2DReplies: 2Last Post: 10-17-2011, 12:15 AM -
Error: cannot load image .
By iswan in forum Java AppletsReplies: 1Last Post: 09-29-2011, 02:26 AM -
Load image to jPanel
By DJIT in forum AWT / SwingReplies: 7Last Post: 12-27-2010, 12:48 PM -
ImageIcon won't load my image
By JaiRaj in forum AWT / SwingReplies: 5Last Post: 03-04-2010, 06:35 PM -
Help with load image
By trill in forum New To JavaReplies: 1Last Post: 08-01-2007, 07:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks