It's not a secret anymore!
The moose likes Beginning Java and the fly likes jar file help please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "jar file help please" Watch "jar file help please" New topic

jar file help please

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
jar cmvf manifest.txt jahok.jar *

why wont it keep my images in the jar file, if my jar is in the same folder as the images they show up, but when i move the jar to desktop , it doesnt draw, or read from files its supposed to...


[ February 05, 2007: Message edited by: Bear Bibeault ]

You down with OOP? Yeah you know me!
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Do you use Class/ClassLoader.getResourceXXX() to load the images, which will work out of a jar? Or do you use FileInputStream, file: URLs, or something similar, which won't?

[Jess in Action][AskingGoodQuestions]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
i have no clue what either of those are...

i just said;

i thought when the jar compresses it, it stays with that exe. jar
for ever, like a copy of that .GIF file

am I wrong?


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15083

The API documentation of the constructor of ImageIcon says:

public ImageIcon(String filename)


filename - a String specifying a filename or path

So it is looking for a file with the specified name. If you don't specify a directory (as you did), it will look for the file in the current directory. If you put your file inside a JAR, then it will not find the file.

Have a look at the API documentation for ImageIcon. It has another constructor that takes an URL. You could use that to load the image from a JAR file like this:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
URL url = MyClass.class.getResource("pic.GIF");

so this code will make the main class look for the pic inside
the jar file?

if so then thats what i need, however, what i dont need is
the jar looking for a file in a certain directory, what i need
is for the jar to run the same on different computers...

Thanks Again

Consider Paul's rocket mass heater.
subject: jar file help please
jQuery in Action, 3rd edition