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

Similar Messages

  • Loading icons from jar file

    Hello,
    i am trying to load ALL imagefiles from my jar file. i do the following:
    Class clazz = Class.forName("com.xxx.IconSelectionDialog");
    String me = clazz.getName().replace(".", "/") + ".class";
    dirURL = clazz.getClassLoader().getResource(me);
    if (dirURL.getProtocol().equals("jar")) {
    String jarPath = dirURL.getPath().substring(6, dirURL.getPath().indexOf("!")); //strip out only the JAR file
    JarFile jar = new JarFile(jarPath );
    Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
    while (entries.hasMoreElements()) {
    JarEntry nextEntry = entries.nextElement();
    String jarEntryName = nextEntry.getName();
    this works fine with my debug local jws installation. when i try to run it online starting i get an error:
    java.io.FileNotFoundException: xentis.jar (Das System kann die angegebene Datei nicht finden)
         at java.util.zip.ZipFile.open(Native Method)
         at java.util.zip.ZipFile.<init>(ZipFile.java:114)
         at java.util.jar.JarFile.<init>(JarFile.java:133)
         at java.util.jar.JarFile.<init>(JarFile.java:70)
    how can i load ALL icons from a jar (without knowing the filename) ?
    is there a generic way to iterate over all entries of a jarfile ?
    thank you
    michael

    Look at "Loading Images Using getResource" on this page
    http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html
    It should provide some ideas.

  • Need help getting icons from jar

    I switched to the latest java plugin, 1.4.2 and now I can't get the applet to retrieve the GIF image within its jarfile. The class files and imagery are all located in the same jarfile. If I use the following code:
    ivjJToggleButton20.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/MoveVertex2.gif")));
    The jarfile is downloaded again and the image is retrieved from the downloaded jarfile instead of the jar currently displayed in the browser.
    any help or ideas are greatly appreciated!

    I usually do something like this to load an icon from a JAR:
    java.net.URL iconURL = getClass().getResource("/yourImageFile.gif");
    if (iconURL != null) icon = new javax.swing.ImageIcon(iconURL);
    JButton.setIcon(icon);
    This should work.

  • IOException: Problem loading image

    Hi foreros
    What could be the reason behind an error like this IOException: Problem loading image except it cannot find the jar path for the images?
    Regards
    Tizha

    Without more information, I'd say there's not enough available RAM to load the image.

  • Problem loading images - please help

    Hi,
    I have a slight problem loading images. I'm using netbeans 5.0 and have a few packages setup in a project called Maths. In a package called LineConnection theres a class that needs to read an image and display it on screen. To load the image I use:
    URL url = getClass().getResource("fraction/pic1.png");
    Image img1 = Toolkit.getDefaultToolkit().getImage(url);This loads the image and I can display it on screen fine.
    The problem is that the folder fractions with the images in HAS to be located within the Maths/build/class/LineConnection folder otherwise it wont load the image and I get a null pointer exception.
    I want to be able to have the folder containing the images in folder Maths so that it's one folder above the src folder. But if I put it here (or anywhere else) the images can't be found and loaded.
    Could someone tell me why the images can only be found if they're within the build folder because it's causing me some problems when I try to create an executable JAR.
    Cheers

    I've used:
    URL url = ImageHandler.class.getResource("fractions/q0.png");               
    System.out.println("URL = " + url);to determine whether the file is found.
    If I put the file in the folder build/classes/LineConection/fractions/q0.png then it finds the image and prints the file path.
    But, if I put it in another folder such as the src folder it can't find the image and url = null;
    How can I make netbeans search in other folders for the resources so I can store them where I like?

  • Problems loading RAW files from Canon 6D in Elements 10

    Elements 10 - Windows: I have been using the software for a couple of years and never had a problem loading RAW files from my Canon T2i ir 60D, however I just purchased a Canon 6D and when I try to load a RAW from the 6D I get Cannot open 'C:\User...CR2' because it is the wrong type of file.  I thought cameraraw 6.5 patch would be the fix but when trying to install I get "Error loading Updater workflow"

    Will the DNG files perform the same, meaning will I retain all the information that makes RAW important for processing?
    Yes. That's why DNGs were invented. Adobe has put a lot of work into the DNG format for this very reason.
    Raw files are proprietary and manufacturer specific. Raw formats change with every make and model of camera. Software must be updated for the new Raw format of every single camera. Adobe does an admirable job of upgrading Camera Raw for every new camera.
    DNG is also a Raw format but it's open source and works in any software which can read the DNG format. If camera manufacturers used the DNG format instead of their own Raw format, you would not have to upgrade Elements 10. But they don't, and probably never will, so you must upgrade the Camera Raw plugin (and therefore Elements 10 since it's not compatible with the new Camera Raw) to read the new Raw format from the 6D or add another step to your workflow with conversion to DNG first.
    See
    http://photographylife.com/dng-vs-raw
    http://www.adobe.com/products/photoshop/extend.displayTab2.html

  • Problems loading raw files from Nikon D750 to lightroom4

    problems loading raw files from Nikon D750 to lightroom4. Any thoughts?

    This is a recently produced model so raw support was only added in LR 5.7 / ACR 8.7, updates to LR 4 ceased about two years ago when LR 5 was issued.
    Options a.) Upgrade to LR 5.
    b.) Utilize the latest Adobe DNG Converter 8.8 to convert the nef files to dng format then you will be able to import the dng files into LR 4.

  • Problem loading image from jar file referenced by jar file

    First, I searched this one and no, I didn't find an answer. Loading images from jar files has been pretty much done to death but my problem is different. Please read on.
    I have my application, a straight up executable running from Eclipse. It uses a jar file, call it JarA. JarA launches a GUI that is located in another jar file. Call it JarB. To recap:
    My application calls JarA -> JarA loads classes from JarB -> JarB looks for images to place in a GUI it wants to show on the screen
    When JarB goes to load an image the following happens:
    java.lang.NullPointerException
         at sun.misc.URLClassPath$3.run(URLClassPath.java:316)
         at java.security.AccessController.doPrivileged(Native Method)
         at sun.misc.URLClassPath.getLoader(URLClassPath.java:313)
         at sun.misc.URLClassPath.getLoader(URLClassPath.java:290)
         at sun.misc.URLClassPath.findResource(URLClassPath.java:141)
         at java.net.URLClassLoader$2.run(URLClassLoader.java:362)
         at java.security.AccessController.doPrivileged(Native Method)
         at java.net.URLClassLoader.findResource(URLClassLoader.java:359)
         at java.lang.ClassLoader.getResource(ClassLoader.java:977)
         at org.cubrc.gmshell.gui.MainWin.preInit(MainWin.java:152)
         at org.cubrc.gmshell.gui.MainWin.<init>(MainWin.java:135)
    The code from JarB that loads the image looks like this:
              URL[] oSearch = {Main.class.getResource("images/")};
              URLClassLoader oLoader = new URLClassLoader(oSearch);
              imgIcon = new ImageIcon(oLoader.getResource("icon.gif"));
              imgMatchRunning = new ImageIcon(oLoader.getResource("gears.gif"));
              imgMatchStill = new ImageIcon(oLoader.getResource("gears-still.gif"));
              imgMagnify = new ImageIcon(oLoader.getResource("magnify.gif"));This looks right to me and JarB certainly has an images directory with those files. But I'm in hell right now because I don't know where to place the images to make this work or if you can even attempt to load images with a dependency chain like this.
    Any help very appreciated!

    Have you tried to move your image-files out of the jar file and place them in the sam folder as the jar-file? I think that would help.
    When you try to load the image-file you get the NullPointerException because the program tries to read a file it can't find. Remember that a jar file IS a file and not a directory.
    If you want to read somthing inside the jar-file you need to encode it first.
    Have you tried to read the jar-file with winRar. It makes it easy to add and remove files in your jar-file.

  • Loading icons from jar - linux problem

    Dear,
    I use the folowing code to load an iconImage from a jar file to be used on JButtons:
    private ImageIcon getImageIcon(String iconName){ //gets icon from a file
    ImageIcon icon = null;
    ClassLoader cl = this.getClass().getClassLoader();
    java.net.URL imageURL = cl.getResource("/ExcelInterface/rsrc/"+iconName);
    if (debug) System.out.println(imageURL.toString());
    try{
    if (imageURL != null) {
    icon = new ImageIcon(imageURL);
    }catch (Exception e){
    System.out.println(e.getMessage());
    return icon;
    } // end of getImageIcon(String iconName)
    this works fine on windows.
    On linux everything looks fine (i.e. there are no exceptions, the URL looks fine...) BUT the Icons are not displayed on the buttons.
    Anyone have an idea why?

    I do something similar and although I've never had any problems on Linux only the server side stuff runs there so I've never tried exactly what you are doing. Anyway, my code doesn't have the leading "/" on the URL and the URL is the full path name from the class root, literally "com/inqwell/any/client/arrowup.gif" in my case. Try that and see if it works.

  • Problem loading images from a return value of a function

    hello, I write because I uin problem in loading images from a return value of a function.
    I created a database with a field "image" of type string, where I put the physical address of the image. I have written like this: {__DIR__ }foto.jpg
    Place the code, so you understand better
    function imageViewImage(): javafx.scene.image.Image {
    connetti();
    lass.forName(driverName);
    con = DriverManager.getConnection(url,user,"");
    stmt = con.createStatement();
    var richiesta:String = "SELECT image from viaggio WHERE id_viaggio=7";
    rs1 = stmt.executeQuery(richiesta);
    var result :String;
    rs1.next();
    risultato = rs1.getString("image");
    JOptionPane.showMessageDialog(null, result);
    var imagez = Image{
    url:"{result}";
    return imagez;
    The image is in the source code package
    First I connect to the database, run the query and I take the contents of the image.
    Can anyone help me?
    Is right to put in the database {__DIR__} foto.jpg or do I put only foto.jpg?

    Hello unkus_nob,
    I would rather suggest you to save only filename of that image like "foto.jpg" in database. And just concat it with the String variable containing "{__DIR__}".
    Actually the javafx compiler converts the {__DIR__} to the existing class directory path something like : "jar:file:/..../".
    var currentDir ="{__DIR__}";
    risultato = "{currentDir}{rs1.getString("immagine"})"; 
    var image = Image{
        url:risultato
    return image;--NARAYAN G.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Having problem loading the gif from a jar for the frame.

    I got the gif showing up for the Toolbar fine but now trying to get the Frame icon from the same jar and having the problem.
    I have the following method in the MainFrame.
    * Load an ImageIcon, either from inside the jar file (during the normal execution)
    * or from the source path (during the development without jar file).
    public static synchronized ImageIcon LoadImageIcon(String name) {   
    try {
    URL url = ClassLoader.getSystemResource(name);
    if(url!=null) {
    return new ImageIcon(Toolkit.getDefaultToolkit().getImage(url));
    } else {  
    return new ImageIcon(name);
    } catch(Exception e) {
    return null;
    I gave the following in the constructor of the same class i.e the MainFrame which is extending the JFrame
    this.setIconImage(LoadImageIcon("frameicon.gif").getImage());
    I am not getting the Icon. I did check the name of icon and I don't know what else to check.
    Any suggesions or what am I missing here? Thanks.

    I have these 2 statements one below the other and
    JButton btnNew = new JButton(MainFrame.LoadImageIcon("new.gif"));
    JButton btnOpenProject = new JButton(MainFrame.LoadImageIcon("open.gif"));
    for the above it gets the icon and for the below it doesn't from the jar. Why is it so? I don't understand. help me thru this. I am not getting any icon except for the new.gif. What am I doing wrong here? Thanks.

  • Problem loading image for TrayIcon

    This code works great if I'm in Netbeans running my project. No problems.
                //grabbing image for TrayIcon
                String curDir = System.getProperty("user.dir");
                Image image = Toolkit.getDefaultToolkit().getImage
                        (curDir + "\\src\\edunasdesktop\\earth_alert.png");The problem is when I run my jar file from the dist folder in my project folder. The TrayIcon loads just fine,
    just with no picture. Do I need to point to a different location or do I need to put the picture elsewhere; or both?
    Some pictures I used with the Netbeans GUI Builder on some JLabels are in the same src directory but I used the
    Builder to add those pics so I guess I'm a bit unsure on how to do this by hand, as the Builder handled this type situation
    in previous occassions.

    The [section on icons|http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html#getResource] in the Swing tutorial has information about loading images. The general idea is you use
    URL toImage = SomeClass.class.getResource(...);Incendently, if done right then it will work in Netbeans or in a jar. The way you currently have it set the image would have to be in +/src/edunasdesktop/+ within the jar file for the Toolkit to find it.

  • Problem using Image Processor from Bridge

    I have been using the Image Processor to process photos from Bridge for quite some time. However, I recently received an error message when I try to initiate Image Processor from Bridge and I have not been able to find any help topics that address my problem: I select images in Bridge to process, then click "Tools>Photoshop>Image Processor". Photoshop opens and the familiar Image Processor dialog box opens. Item 1 in the dialog box says "Process files from Bridge only" and has the number of files I have selected in parenteses. I select the file type and other settings in #3, and preferences in #4. When I click "Run", I get an error message window that says: "Script Alert. There were no source files that could be opened by Photoshop". The Image Processor works if I open the files in Photoshop first, then run it by selecting "File>Scripts>Image Processor" and tell it to process all open files.
    Would someone be able to help me with this problem?
    Thanks, Larry

    Usually when it worked before and not now, and you have added no new software or hardware I recommend resetting the preferences.  Hold down the Ctrl key and click on the Bridge icon to start.  You should get a reset window with 3 options.

  • Unable to load any XML from JAR - Please help!!

    Hi All,
    I am a student in the final days of my degree. I have been working on my final project for some time now, an applet which converts input text to a signal plot for line encoding schemes - AMI, NRZ etc. It is pretty much finished, but one small bug threatens to scuttle the entire project! I have been searching the web all day long for answers with little success, and as the castor forum still appears to be down I am posting here. Any suggestions or advice would be greatly appreciated.
    The applet uses the castor databinding framework to load various XML data. I am using Eclipse 3.01 for development - when the program is run locally as an applet, everything works fine. When the program is bundled into a JAR file and nested into a clean folder with a html page and the jar file, when the command to read in XML is given, a NullPointerException occurs, indicating that castor was unable to access the XML files.
    Below is one of the methods used to make castor load XML data:
         public CodeSet loadCodeXML(String _codesetFilename)
              String _mappingURI = "schema/codes/codesets-mapping.xml";
              String _codesetURI = "schema/codes/" + _codesetFilename;
                    // Create a new Castor mapping object
              Mapping mapping = new Mapping();                         
            try                                                                 // Attempt to load in the selected XML character set
                 mapping.loadMapping(_mappingURI);                    // Initialize 'mapping' with the map file
                Unmarshaller unmar = new Unmarshaller(mapping); // Create a new XML Unmarshaller that uses 'mapping'
                // The line below creates a new CharSet object called _codeset and populates it with the XML data
                CodeSet _codeset = (CodeSet)unmar.unmarshal(new InputSource(new FileReader(_codesetURI)));
                // The character set was successfully loaded, so pass new CharSet object back to caller and end
                return _codeset;
            } catch (Exception e) {
                 // If an error occurs while extracting the XML data, this block will execute:
                JOptionPane.showMessageDialog(null, e);               // Display a message dialog containing the exception error
                return null;                                             // Do not return a CharSet object to caller
         }It would seem to me the problem lies within
              String _mappingURI = "schema/codes/codesets-mapping.xml";
              String _codesetURI = "schema/codes/" + _codesetFilename;I have read that files inside a JAR can be accessed in this way ( http://archive.codehaus.org/castor/user/msg00025.html ) but it won't appear to work.
    If these are set to a full system path (outside any JAR) i.e. "/home/me/proj/schema/codes/codesets-mapping.xml", the application operates fine. Clearly this is no good however, as the XML data must reside within the JAR package. I have tried many permutations such as "jar://schema/..." , "/schema/..", "schema/..", with no success. I have read of using InputStream and getResource methods to access files within the jar but have had no success. I have checked the schema dir is being put into the JAR archive.
    Could anyone suggest an appropriate way of loading XML files from within a JAR file in this context?
    Thanks in advance for any replies.

    Hi, me again..
    Re: mr_doghead - the filename of the file is passed from the calling function
    public CodeSet loadCodeXML(String _codesetFilename)eg loadCodeXML(ami.xml) will return a CodeSet object containing the ami xml data
    Anyway, I've manged to fix it up. The problem actually lies within castor, not my code at all. This is a known bug in castor that the dev's deemed 'not important' to fix, but I have to say the work around is EXTREMLY poorly documented online. Hence, this post is just to say how to fix it up if ne1 else is having trouble...
    To load mappings, use:
    mapping.loadMapping(getClass().getResource(_mappingURI).toString());where _mappingURI is a string such as "/xml/mapfile.xml"
    However, the unmarshalling method takes in a file object, so getResourceAsStream must be used:
    CharSet _charset = (CharSet)unmar.unmarshal(new InputSource(getClass().getResourceAsStream(_charsetURI)));Where CharSet is your custom object you are marshalling into, and _charsetURI is a relative path to your xml file.
    Ugly as hell? Very.
    Does it work? Perfectly.
    take it ez guys, time for me to go hand this sht in! ;D

  • 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!

Maybe you are looking for

  • Why are keywords for Delicious bookmarks no longer recognized in Firefox 25 and Firefox 26?

    I use Delicious Bookmarks 2.3.4 for synchronizing bookmarks and their keywords on different computers. After updating to Firefox 25 the keywords are no longer recognized. If I enter a keyword like "foo" it doesn't send me to the bookmark with the key

  • Why can I not make changes to a indesign file

    I was given a file to make some changes to.  I can open the file and some items are selectable but others are not.  Can not copy them or make changes.  Wondering why?  Some are pictures and some are text. elaine

  • Problem with Multisim Transient Analysis TSTOP

    Reference the attached PDF.  Page 1 is the circuit.  Page 2 is the transient simulation using nothing but default settings (TSTOP=1ms).  Not convinced that the result is correct but that is not the issue.  At least the result is within the range expe

  • Can product costing be used in service industry?

    where a material master is being maintained (type DIEN) for services provided, can product costing be used for services?  what pre-requisites are essential to implement product costing?

  • Keychain Password sync

    There is an issue with my keychain password. I can not remember having set up a keychain password at all. All the time i'd like to show the password of e.g. a wireless connection keychain asks me to enter the keychain password which i can not remembe