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:
You would not BELIEVE how helpful it was for me to consciously DECIDE to ask questions with a girl tonight. It helped TREMENDOUSLY. I learned so much. It sounds so fucking simple but I talked SO much usually that I didnt get the info I needed so I could use that info back to her. mysteryseduceamogflirtpickup
You would not BELIEVE how helpful it was for me to consciously DECIDE to ask questions with a girl tonight. It helped TREMENDOUSLY. I learned so much. It sounds so fucking simple but I talked SO much usually that I didnt get the info I needed so I could use that info back to her. mysteryseduceamogflirtpickup
There is Unzip component
2006-06-19 10:41:56 dnamiot
[Reply | View]
You can access any file (whether Java-class, properties-file, xml-file or data file - you name it) from a jar which is in your class path using getClass().getResourceAsStream (...). This returns an InputStream which you can read as if it were a normal file.
Using this technique has the added advantage that your program keeps working when deployed in a different way (as an expanded jar, as jars on a web server, ...). The program doesn't need to know whether the files really belong to a jar.
Alternatives: Difference between getClass().getResource() and getClass().getClassLoader().getResource()
2006-06-19 02:48:23 jjasper
[Reply | View]
Note: if the class files are packaged in a jar make sure the images are also packaged in that jar.
The image cursor.gif is available in the directory /usr/images.
Create a zip or jar file …/dist/lib/usr.zip containing the image and including the subfolders
/usr/images. Then add the following line to your code:
<font color="red">Note</font>: In UIManager.put("swingx.busy.cursor", "<font color="red">/</font>home/images/custom-cursor"); remove the first slash,
because there is a difference between
getInstance().getClass().getClassLoader().getResource(…); which I used and
getInstance().getClass().getResource(…);
in the way they retrieve the resource.
By the way: Due to a Win32 problem the cursor to must be 32 x 32. To create a 16 x 16 icon use for example the upper left
16 x 16 pixels and fill the rest with a transparent color.
</td>
</tr>
</table>
get 'm out
2006-06-15 05:01:08 stephendevelop
[Reply | View]
this.getClass().getClassLoader().getResourceAsStream( "images/whatever.gif" ) gives you a inputStream to the resources inside a jar from there I guess you'll know what to do