It does not appear that the Java API provides any way to MODIFY(write to) files inside a Jar file - it's a read-only container. JButton btRemove = new JButton(new ImageIcon(cl.getResource("images/remove.png"))) Reading versus Writing. When the image file is in another package of the jar file. When the image file is in the same package of the class 'SearchTab'.
The example below shows how to create an image icon to illustrate the concept.
The code here is just slightly different in that we simply ask the classloader to return us resource file directly. Java.io.InputStream in = cl.getResourceAsStream("conf/properties.xml") Īccessing a resource file, e.g., image, sounds, etc. Here we tell the classloader to return the resource as a stream.ĬlassLoader cl = this.getClass().getClassLoader() Assume the file "properties.xml" is inside of the "application.jar" file, that is stored in the "conf" package. Here are a couple of code examples:Į.g., to load a XML file, you could use the code below. ie, in the jar file.i.e., this is also where the resource files are) to help you access the desired resource files. However, the technique for successfully accessing images or other resource files inside of the jar from inside your code is not at all self evident - until you hit on the general principle of how to do it: calling on the class loader for some class you are using (which KNOWS where it's class is. Packaging an application and all of its resources in a single runnable Jar file is clean and efficient. The information below is adapted directly from this link I'm just copying it here in case this link dies. I'll try to explore this when I get some free minutes. Kind of unfortunate.and doesn't actually make much sense, ie, the class loader should be viable whether or not your running from jar.
Baier's experience (see further down) is "no, once you change it, it will only run from Jar", meaning it's the very last modification you want to do before submitting your app. One question I haven't answered to my satisfaction yet is whether this same modified resource access code will also work during development.
images, sounds, movie clips) will be required by our program at runtime. Often times we will work on a Java project where certain resources (e.g. With credit and gratitude to Stephen Baier and Andrew Arminio (CS477, S09), who