• Post Reply Bookmark Topic Watch Topic
  • New Topic

JAR-files  RSS feed

 
niklas aronsson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have problems building a jar-file for my application:
C:\> jar -cvmf manifest.mf Jar.jar *.*
gives me an executable jar that works, but
when I move that jar-file to an other location,
the gif-images doesn't show.
All files exist in the jar-file, but if they aren't present in the jar-file directory, it
doesn't work...
Is this a manifest-problem?
My manifest only says:
Main-class: Classname
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi niklas,
You can't directly open a file that's contained in your jar file, unless you use the getResourceAsStream(String name) method. This method uses a classloader to lookup your resource and returns an InputStream object.
It's located in the Class object, so from any class located in the jar containing the pictures, just call getClass().getResourceAsStream(imageFilename);
Good luck.
 
niklas aronsson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx, but I still get the same problem):

InputStream inp= getClass().getResourceAsStream( "img1.gif" );
byte[] b= new byte[4096];
inp.read( b);
ImageIcon img1 = new ImageIcon( b);
JButton button1 = new JButton( "Txt", img1);
panel.add(button1);

Did I miss something?
 
niklas aronsson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
If I turn of compression it works.
So that's the solution?
No compressing of jar-files if you want to read
text- or image-files in a jar?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niklas,
I'm not so sure that the source of the problem comes is compression or not, but rather of the way you read the file contents.
The documentation for the read method(byte[] b), does not guaranty that you will get the whole file contents in one go. I would guess that your call to the read method in cas of compressed jar would return a number of bytes that is less than the size of your buffer.
Instead, use the more flexible read(byte[] b, int off, int len) method and look at the return value. While it is not -1, continue reading.
Also, using a BufferedInputStream might be more efficient.
Cheers,
 
niklas aronsson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, reading the whole file solves the problem;
I'm in your debt
/n
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!