(Not So) Stupid Questions 10: JAR Files Blog

    (Not So) Stupid Questions

    How to work with the contents of JAR files

    Editor's note: Sometimes the most interesting discussions begin when someone says, "This may be a stupid question, but ...." If the person asking the question has taken the time to think about the problem before asking, the question is often not stupid at all. The uncertainty points out an ambiguity in the specs, holes in the docs, or a search for how more experienced programmers might address a particular problem. From time to time, we will print one of the "(Not So) Stupid Questions" we receive and invite our readers to answer the question in the feedback section.

    Remember that new people are joining the Java community all the time and may be looking for help from those with more experience. Also, those who began with Java as their first language can benefit from those coming to the community with experience in other languages. As always, answer the questions with kindness. You are also welcome to submit your questions to

    This may be a stupid question, but ... "Other than bundling my classes, what good does a JAR do me?"

    First thoughts:

    I've seen desktop applications distributed as double-clickable JARs. I understand how to put all my classes into a JAR and to use the manifest option to indicate which class is the "Main-Class," meaning which one I want to run when I double-click it.

    What I don't get is how to put other stuff in a JAR. It seems like it must be possible, because these double-clickable JARs have icons and images, and unless it's downloading them from the Web every time, it seems like those images have to be living somewhere inside the JAR file. But there's nothing about that in Sun's JAR tutorial.

    So that makes me wonder how I work with images inside JAR files. If I were writing a normal Swing app, I would load an image icon with something like:

       ImageIcon myIcon = new ImageIcon ("images/myIcon.gif");

    In this case, there's obviously an images folder with a myIcon.gif file inside it. But what do I replace "images/myIcon.gif" with if the images are in a JAR?

    I've also noticed some JAR-related classes in the Javadocs, like JarFile and JarEntry. I guess I could use those to find my images, but that means that I have to build a JAR every time I do a development build, because now I wouldn't be loading from a file anymore. Or maybe I need some sort of "switch" to figure out whether I'm running from a JAR?

    It seems like this should be easier than I'm understanding. I guess my big question is:

    How do I get stuff like images out of a JAR?