Image Icon with JAR

Hi,
I would like to replace the standard coffee cup window icon with a custom image for a stand-alone Swing application, all packaged as a JAR file. As a quick test, I wrote the following:
package me.swinginfo1;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class IconWindow extends JFrame {
     public IconWindow() {
          this.setSize(300, 200); //set the window size
          this.setLocationRelativeTo(null); //centers window on screen
          this.setTitle("Window with Icon"); //set the window title
          this.setDefaultCloseOperation(EXIT_ON_CLOSE); //end the program when window closed
          java.net.URL imageURL = this.getClass().getResource("images/red_circle.png");
          this.setIconImage(new ImageIcon(imageURL).getImage());
     * Executes Swing window... no arguments.
     public static void main(String[] args) {
          IconWindow myWindow = new IconWindow();
          myWindow.setVisible(true);
When I run this class from the IDE (Eclipse, in this case), the program runs fine, and my icon image appears in the corner of the window as desired. However, when I package the program into a JAR, the JAR will not execute. My method of packaging is simply to use File -> Export -> JAR.
Note: if I comment out the two lines related to the Icon Image, then the JAR executes as expected (but without my custom image icon, of course).
I am concerned that perhaps the resource folder containing the image is not being included in the JAR. Is there an easy way to check this? Or is there something else I am doing wrong?
Thanks!

Thanks for the information atmguy. After checking the jar contents, I found that my resource files (the "red_circle.png" in this case) were not being packaged into the jar. It looks like the reason for this was that I had just added the directory to my project "src" directory by hand, rather than importing the files with Eclipse. Thus, when I packaged the jar using Eclipse, it was unaware of the resource files and therefore did not include them in the jar.
Also, I found that in Windows a handy way of quickly viewing jar files is to rename the jar as a .zip, and then just explore the zip file. (As mentioned here: http://en.kioskea.net/faq/sujet-556-to-see-the-content-of-a-jar-file).
The jar now executes as expected. Thanks!

Similar Messages

  • Problem loading image icons from jar

    I have created an Applet , that works as an application too.I created the certificate the jar, with the images and the classes, i signed it... The html page is working fine as the exe i have made from the jar, but ONLY if the folder with the images is in the same directory!!!I have searched in java and other forums for an answer that fits but..
    Here is a sample of my code.
    images[0]=new ImageIcon((Applet15.class.getResource("/palaio/Image8.jpg")));
    images[1]=new ImageIcon((Applet15.class.getResource("/palaio/Image22.jpg")));
    images[2]=new ImageIcon((Applet15.class.getResource("/palaio/Image30.jpg")));
    images[3]=new ImageIcon((Applet15.class.getResource("/palaio/Image36.jpg")));
    images[4]=new ImageIcon((Applet15.class.getResource("/palaio/Image42.jpg")));
    images[5]=new ImageIcon((Applet15.class.getResource("/palaio/Image63.jpg")));
    public Applet15() {
          private void jbInit() throws Exception {
      public String getAppletInfo() {
        return "Applet Information";
      public String[][] getParameterInfo() {
        return null;
      public static void main(String[] args) {
        Applet15 applet = new Applet15();
        applet.isStandalone = true;
        Frame frame;
        frame = new Frame();
        frame.setTitle("Applet Frame");
        frame.add(applet, BorderLayout.CENTER);
        applet.init();
        applet.start();
        frame.setSize(450,400);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
        frame.setVisible(true);Thank you!!

    This tutorial should resolve the problem
    http://java.sun.com/docs/books/tutorial/applet/appletsonly/data.html

  • Help plz~ URGENT: Display image icon with specific coordinates

    I want to view an ImageIcon with specific cooridnate (ie, I know the coordinate of the top left hand corner, and i know the height and the width of the "want to display part")
    how can i make it such that it will onli view the part i want?
    for example
    i want to display say from (40, 40) (left hand corner) to (90,90) (right hand corner)
    ImageIcon thePic= new ImageIcon("theimage.jpg");
    ImageIcon newPic= new ImageIcon( XXXXXXXXX);
    what should i put at the XXXXXXXXXX part?
    Thanks in advance!

    Use getSubImage method of BufferedImage. Something like:
    Image originalImage = ImageIO.read(imageFile) ;// see ImageIO API
    Image scaledImage = originalImage.getScaledInstance(40.40,90,90);
    ImageIcon imageIcon = new ImageIcon(scaledImage);ImageIO.read is overloaded for files, inputStreams and URL's so you can choose how to load it.
    I'll leave the rest to you
    DB

  • Hi I have been having a problem with Bridge in CS4 recently. So uninstalled CS4 and Lightroom 4.4 and reinstalled them on my Desktop PC. When I turned on Bridge through CS4, My thumb nail images disappeared and a thumb nail icon with CR2 appeared. Now som

    Hi I have been having a problem with Bridge in CS4 recently. So uninstalled CS4 and Lightroom 4.4 and reinstalled them on my Desktop PC. When I turned on Bridge through CS4, My thumb nail images disappeared and a thumb nail icon with CR2 appeared. Now some of my images have disappeared from Light room 4.4, but I can find them in CS4 and Bridge now shows some thumb nails as images and some as an icon with CR2. I have spent two weeks going through preferences etc. to find how to resolve my problem. Please can you Help? Meany thanks in advance
      Derek Randall

    I don't use LR and rarely use the bridge so I can not answers about thumbnails in them,  However thumbnails in windows file explorer and windows dialog for CS2 files will only show if you have installed codec into windows that will produce them. Windows does not do CR2 thumbnails on its own. I use FastPictureViewer Codec package for RAW File and PSD files thunbmail support.

  • Blue icon with question mark instead of image in email

    Sometimes I get a blue icon with question mark instead of image in email, when viewing in Apple Mail Program.
    Does anyone know why?????

    I'm asking your thread get redirected to the proper forum. Please read this tip on how to post on Discussions for future threads:
    http://discussions.apple.com/thread.jspa?threadID=1574700
    Is this issue with Tiger or Leopard? Your profile indicates you have a machine with Tiger, yet your signature of your post says you have Leopard.
    Also, there is no such thing as a Powerbook Pro. Read the text under the screen to tell us exactly which model you have. MacBook Pro is the name given to Macs that are for the pro lineup of notebooks once Apple shifted to the Intel CPU.
    Message was edited by: a brody

  • No thumbnail images in icons with Preview since reinstall of Tiger

    Since a HDD crash and reinstall of Tiger at Apple Store with updates to 10.4.11, Preview.app is now NOT making image thumbnails but only shows an icon with the Preview logo and file type, ie. JPEG, PNG.
    Also in Folders most images, but not all, are not showing up as they used to with miniature images.
    This makes it very hard to tell what the image is merely with a number for the filename.
    I tried deleting the plist but that didn't help. Anyone have any suggestions how to get real thumbnails back?

    Hi BDAqua,
    Thanks for the suggestions. I tried it several times to no avail and noticed that an older version of Preview which had been archived on an external drive was 13.2 MB on disk while the one which the Apple Store put onto the new hard drive was only 3 MB, even though they are both v3.0.9 (build 409) I've tried both and there doesn't appear to be any difference in functionality.
    Still no thumbnails except on a few .jpgs. They do work from iPhoto if they are normal .jpg pictures if I drag the pic to the desktop from the iPhoto Library, but not panoramas which have been stitched, photos which have been cropped, modified or .gif or .tiff.
    Guess we'll just have to keep looking for the answer to why they won't show up all the time.
    Thanks anyway.
    If anyone else has any ideas about this Preview weirdness, please join in...

  • Why does some images saved on memory card show up as a little icon with lightning bolt through it

    I been saving images on memory card.. well as of lately some images have a icon with a lightening bolt on it..and images are lost

    There is a problem with the SD card.  Reboot your phone and see if the problem comes back. If the problem comes back un-mount and remove the card and see if a computer can read the card.  If the computer can read it then move all the photos to the computer then reformat the card in the phone. Put the reformatted card back in the phone and take a picture.  If the problem persists then the card is corrupted in some way.  This is more common than you would think especially if you have a galaxy S3 and use SanDisk cards. I would try a new card from a different manufacturer.

  • HT2486 want to know how to add pictures to address book that DO NOT appear as fullscreen images on the iPhone but remain as icons with the iPhone background visible

    want to know how to add pictures to address book that DO NOT appear as fullscreen images on the iPhone but remain as icons with the iPhone background visible (see attached image). for some reason, all new contacts added through Mountain Lion appear as fullscreen caller ID images.

    This would be better addressed in the iPhone forum, I would post there as well.

  • Issues with Loading Images from a Jar File

    This code snippet basically loops through a jar of gifs and loads them into a hashmap to be used later. The images all load into the HashMap just fine, I tested and made sure their widths and heights were changing as well as the buffer size from gif to gif. The problem comes in when some of the images are loaded to be painted they are incomplete it looks as though part of the image came through but not all of it, while other images look just fine. The old way in which we loaded the graphics didn't involve getting them from a jar file. My question is, is this a common problem with loading images from a jar from an applet? For a while I had tried to approach the problem by getting the URL of the image in a jar and passing that into the toolkit and creating the image that way, I was unsuccessful in getting that to work.
    //app is the Japplet
    MediaTracker tracker = new MediaTracker(app);
    //jf represents the jar file obj, enum for looping through jar entries
    Enumeration e = jf.entries();
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    //buffer for reading image stream
    byte buffer [];
    while(e.hasMoreElements())
    fileName = e.nextElement().toString();
    InputStream inputstream = jf.getInputStream(jf.getEntry(fileName));
    buffer = new byte[inputstream.available()];
    inputstream.read(buffer);
    currentIm = toolkit.createImage(buffer);
    tracker.addImage(currentIm, 0);
    tracker.waitForAll();
    images.put(fileName.substring(0, fileName.indexOf(".")), currentIm);
    } //while
    }//try
    catch(Exception e)
    e.printStackTrace();
    }

    compressed files are not the problem. It is just the problem of the read not returning all the bytes. Here is a working implementation:
    InputStream is = jar.getInputStream(entry);
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    try{
    byte[] buf = new byte[1024];
    int read;
    while((read = is.read(buf)) > 0) {
    os.write(buf, 0, read);
    catch(Exception e){
         e.printStackTrace();
         return null;
    image = Toolkit.getDefaultToolkit().createImage(os.toByteArray());
    This works but I think you end up opening the jar a second time and downloading it from the server again. Another way of getting the images is using the class loader:
    InputStream is = MyApplet.class.getResourceAsStream(strImageName);
    In this case, the image file needs to be at the same level than MyApplet.class but you don't get the benefit of enumerating of the images available in the jar.

  • Why do I have "broken image" icons after uploading a file with safari 7.0

    I have a wesite where I can upload images.  We show the directory of uploaded files with an icon that is a thumbnail of the image.  This is working fine with all versions of safari except 7.0.
    All users with 7.0 see the small blue icon with a "?" in place of the thumbnail (png and jpg).  When the full image is displayed it look fine.
    The wierd thing is that if I go to Chrome and view my directory and then go back to safari 7.0 and refresh, all thumbnails appear correctly.
    I suspect there is a bug in Safari 7.0.  Anyone else have this problem?

    Has the 1 year limited hardware warranty expired? Apple would only charge to perform a repair or a replacement. If iOS 7.0.3 caused the issue then that would be a software issue, not hardware. Take it into the Genius Bar because the technicians are the only people that will be able to determine the cause. But if the iPhone needs to be replaced and it is not covered then Apple are in the right. 

  • Call  form   with out  broken image icon

    Question: How can fit the tags of a <form> into a small
    cell without the tags causing misalingment in the table?
    I need to put a PayPal button on my site. The shopping cart I
    was using up until now used only a link so I simply used hotspots
    on button images. This new PayPal button sends information from my
    site to PayPal with a <form>. The problem I am having is with
    the elements in the red dotted form box ( yellow tags and broken
    image icon). I have made the yellow tags invisible but the grey
    broken image icon won't fit into the small cell( 86x22 ) of my
    sites menu bar with out misaligning the table of cells. I spent
    considerable time building my menu bar from layers so I would like
    to keep intact.
    The gray broken image icon shows because PayPal requires
    that you copy the button image location directly from the browser
    webpage <input type="image" src="
    http://www.glotosleep.com/images_2007/img_buy_now_paypal.gif"
    Thank You for any help. I am sure there is a simple solution
    I just was not able find it anywhere on the net.
    my site is www,glotosleep.com and the button i would like to
    link to the PayPal form is the "Buy Now" in the menu bar

    Question: How can fit the tags of a <form> into a small
    cell without the tags causing misalingment in the table?
    I need to put a PayPal button on my site. The shopping cart I
    was using up until now used only a link so I simply used hotspots
    on button images. This new PayPal button sends information from my
    site to PayPal with a <form>. The problem I am having is with
    the elements in the red dotted form box ( yellow tags and broken
    image icon). I have made the yellow tags invisible but the grey
    broken image icon won't fit into the small cell( 86x22 ) of my
    sites menu bar with out misaligning the table of cells. I spent
    considerable time building my menu bar from layers so I would like
    to keep intact.
    The gray broken image icon shows because PayPal requires
    that you copy the button image location directly from the browser
    webpage <input type="image" src="
    http://www.glotosleep.com/images_2007/img_buy_now_paypal.gif"
    Thank You for any help. I am sure there is a simple solution
    I just was not able find it anywhere on the net.
    my site is www,glotosleep.com and the button i would like to
    link to the PayPal form is the "Buy Now" in the menu bar

  • Need help with loading images in executable Jar

    Hi,
    I've developed an application using netbeans that needs to load and display images.
    I use netbeans Clean and Build function to create an executable Jar file. When I run the executable Jar file it runs fine but when I come to a section of the application that needs images it freezes and can't load the images.
    When I then return to netbeans the part of the program that did successfully run before Clean and Build doesn't work anymore and I get an error message saying Uncaught error fetching image:
    I use,
    URL url = getClass().getResource("images/image1.png");
    Image image1 = Toolkit.getDefaultToolkit().getImage(url);to load an image.
    Can someone tell me why, when I clean and build my project to create a JAR, my images fail to load.
    Should I be storing the images in a specific folder or something??
    Thanks

    I've opened the JAR using winzip and, for some reason, the JAR hasn't preserved my file structure. So, when I try to look for an image as follows:
    URL url = getClass().getResource("images/file1.png");
    Image img= Toolkit.getDefaultToolkit().getImage(url);The folder doesn't exist and so it can't find the image.
    Can someone tell me how to keep my file structure when I create the JAR or an alternative way to find the images within the JAR file.
    Cheers

  • Deploying icons as jar file in Forms 9i

    We (Cancer Research UK) don't quite understand what is happening -
    We followed the guidelines in the white paper "Oracle 9IAs Forms Services - How to Deploy Icons"
    to deploy the images in a jar file:
    Altered the formsweb.cfg file to contain
    archive_jini=f90_jinit.jar, our_images.jar
    imagebase=codebase
    Seemed to work, jar file downloaded to browser (IE 6 ) ok , web deployed form shows toolbar icons, great..
    However, when we removed an existing entry in orion-web.xml which maps our application/icon directory
    or remove the *.gif files from this directory, our web deployed form no longer shows the icon images.
    Does it still need these to be present on the server, if we direct it to use cached images ?
    We are test running just the Application server components installed with the IDs 9.0.2 on a Windows 2000 server.

    Hi Steve,
    I have the exact same problem. The jar is downloaded and everything is 100% best practises, but there is no way I can make the damn system use the files in the JAR file. No matter if imagebase=codebase: it will only use files on the server, and if I remove the gif files they don't show up.
    I think there must be a glitch in the system somewhere. I don't completely understand this part of it: the codebase is mapped to /forms90/java - but how does that add up when you put your files in c:\my_gifs (as detailed in Best Practices section of the Deploying Icons paper). How does the server know where to look when codebase are two different places - I'm thinking it can't for some reason.
    My Apache/Apache/logs/error_log clearly states that files (real files) are not found in c:\my_gifs if I remove them from the file system. The JAR is downloaded, but the server doesn't use it.
    However, most people have all their gif files in the same folder as their images.jar file, so they don't ever discover that the server ignores the jar and uses the files instead.
    Regards,
    Jesper Vad Kristensen
    Aarhus, Denmark

  • How to use/create icons with transparent background ?

    I would like to use icons with transparent background on JButtons, but I can't find how to do it. Until now, I've been using the ImageIcon class to create icons from GIF images. These images were created with MSPaint (sorry, I don't know what I could have used instead of that). Here is how I create my buttons :
    bPencil = new JButton(new ImageIcon("Pencil.GIF"));What could I do to make the background transparent and/or what software could I use instead of M$Paint ?
    P.S. : sorry if my question seems silly, but I'm a newbie in Java coding...

    Transparent images are GIF (256 colours) and PNG (true-colour, half transparency).
    You can store the image with your code, *.class files. You then even can deliver all inside a clickable, executable .jar file.
    This kind of usage is called "resource," and hence use getResource like this:
        java.net.URL imgURL = getClass().getResource("pencil.gif");
        ImageIcon icon = null;
        if (imgURL != null) {
            icon = new ImageIcon(imgURL, description);
        }You can use paths.
    There exist versions of MSPaint that easily loose their transparent colour on saving.
    For GIFs try internet palettes, or try to save as PNG (recent version).
    Start with a transparent image from elsewhere.

  • Image Icon not showing in "get info" for OS 10.9.4

         I  now have PS CC 2014 and the image icons are showing the "generic" .jpg and .psd files in the "get info" finder window, but on my desktop the images are showing their
         proper image icon.
         I was previously using PS CS 5 and the icons are showing up in the "get info" window, but with PS CC 2014 these icons are showing up as generic.
         To be clear the images I saved using PS CS 5 are showing the icons vs. the images I saved in CC 2014 where the icons are not showing in the "get info" window.
         I've compared preference settings with PS 2014 and PS 5 but their basically about the same.
         I'm including 2 images so you can see what I'm explaining. IMG_1627 is a PS CC2014 created image and Arturo is a PS 5 image.
         I did read about other PS users having similar issues but it looked to be unresolved.
         You may think what's the big deal, but I use these image icons to label my folders. A "trick" I've doing for over decade which helps keep my folders visually on track for me.
         Is anyone out there able to solve this issue??  

    I do on occasion get jpgs and pngs that won't render a thumbnail or preview in MacOS.  I have 10.9.5
    I don't have this problem, although I just have CC and CC 2014 on my Macbook.
    I would open those up in Preview.app and try a Save As... and I would get the thumbnail and thumbnail preview back.
    Not really the best idea,but you can see if it helps.
    These  are my file saving options, perhaps there is something there you can try.
    Gene

Maybe you are looking for

  • Problem with javascript in Firefox

    Hi, In my JSP I call a Javascript function when a radio button is selected and it works fine if I use IE, When I use firefox I get the following error Error: document[getNetuiTagName(formId)] has no properties Source File: http://gio5wl06.mt.att.com:

  • Mac Mini color question - no crisp white?

    I just bought my Mac Mini and plugged it into a Dell monitor with the VGA cable. How do I get a crisp white on active windows? Everything is sorta purple . . .

  • Can't open or download on my macbook air

    I can't view or download docs from oracle on my Mac Book Pro

  • I have no connection with skype,

    I I have no connection with skype,can not call,video.. Enough credit.Wat is the problem

  • 1.4.6 system crash with mathematica

    If have installed the new security update on my MacBook Pro. I have not ahd any boot problems like the ones many have had. I have been using Mathematica 4.1, for ppc, for a logn time and it has been working well on the new Macbook too. After the secu