7 Replies Latest reply on Dec 15, 2010 3:58 AM by baftos

    Images in Jar files

    tyelford
      Hi,

      I am creating a recipe book application. Everything is exactly how I want to so I am trying to make a jar file with it.

      On the first screen that loads there is one image, when I make the jar file it doesn't seem to be able to find the image once I am outside of eclipse and running the jar.
      I have created a folder in called resources in my scr folder (src/resources) this is where I am storing the image.

      The method near the bottom (private void addPic()) is where I am having troubles.
      package recipe1_5;
      
      import javax.swing.*;
      import java.awt.event.*;
      import java.awt.*;
      import java.io.*;
      import javax.imageio.*;
      import java.awt.image.*;
      
      /*
       * This provides an entry point into the program
       * as well as the first window
       * the main() function is at the bottom
       * 
       */
      
      public class Main implements ActionListener {
           
           JFrame f = new JFrame();
           Container p = f.getContentPane();
           
           JButton newR = new JButton("Add a New Recipe");
           JButton oldR = new JButton("Look Up An Old Recipe");
           
           JLabel title = new JLabel("Please make a selection");
           
           
           public void actionPerformed(ActionEvent e){
                String whichButton = e.getActionCommand();
                
                if(whichButton.equals("Add a New Recipe")){
                     @SuppressWarnings("unused")
                     Recipe newRecipe = new Recipe();
                     f.setVisible(false);
                     f.dispose();
                }
                
                if(whichButton.equals("Look Up An Old Recipe")){
                     @SuppressWarnings("unused")
                     OldRecipeChooser orc = new OldRecipeChooser();
                     f.setVisible(false);
                     f.dispose();
                }
           }//END actionPerformed
           
           public Main(){
                f.setSize(200,300);
                f.setLocation(500,100);
                f.setTitle("Recipe Magic");
                p.setLayout(new FlowLayout());
                p.add(title);
                p.add(newR);
                p.add(oldR);
                newR.addActionListener(this);
                oldR.addActionListener(this);
                addPic();
                
                
                f.setVisible(true);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           }//END contr ChooseWindow()
           
           private void addPic(){
                try{
                     String imgLocal = System.getProperty("user.dir");
                     if(System.getProperty("os.name").contains("Windows")){
                          imgLocal += "\\RecipeMagic1_5.gif";
                          JOptionPane.showMessageDialog(null, imgLocal);
                     }
                     else
                          imgLocal += "/RecipeMagic1_5.gif";
                     BufferedImage image = ImageIO.read(new File(System.getProperty("user.dir" + imgLocal)));
                     JLabel pic = new JLabel(new ImageIcon(image));
                     p.add(pic);
                }
                catch(Exception e){
                     JOptionPane.showMessageDialog(null, "Image Load Error","ERROR", JOptionPane.ERROR_MESSAGE);
                     e.printStackTrace();
                }
           }
           
           
           public static void main(String args[]){
                Main m = new Main();
           }
      }//END class ChooseWindow
      I have tryed this two different ways. The first how it is. And the second with something like this:
      ImageIcon ii = new ImageIcon(getClass().getResource("resources/RecipeMagic1_5"));
      Any insight on how to get the image from inside of the jar file would be great

      Forgot to add, it either throws a NullPointerException or a FileNotFoundException in the catch clause of addPic()

      Edited by: tyelford on Dec 15, 2010 11:46 AM
        • 1. Re: Images in Jar files
          baftos
          Assumming you have inside the jar /resources/RecipeMagic1_5, i.e. 'RecipeMagic1_5' is in a directory 'resources', which is located in the root of your jar, than
          getClass().getResource("/resources/RecipeMagic1_5")
          should work.

          Edited by: baftos on Dec 14, 2010 9:16 PM
          1 person found this helpful
          • 2. Re: Images in Jar files
            tyelford
            Thanks for that,

            I had the wrong folder, this is what did it:
            ImageIcon ii = new ImageIcon(getClass().getResource("resources/RecipeMagic1_5/RecipeMagic1_5.gif"));
            • 3. Re: Images in Jar files
              tyelford
              Ok, so it works good on Mac, but if I try to use it on Windows it will not load the image,

              Here is the code that works on Mac but not windows:
              private void addPic(){
                        try{
                             String imgLocal = System.getProperty("user.dir");
                             if(System.getProperty("os.name").contains("Windows")){
                                  imgLocal = "resources\\RecipeMagic1_5\\RecipeMagic1_5.gif";
                             }
                             else
                                  imgLocal = "resources/RecipeMagic1_5/RecipeMagic1_5.gif";
                             ImageIcon ii = new ImageIcon(getClass().getResource(imgLocal));
                             JLabel pic = new JLabel(ii);
                             p.add(pic);
                        }
                        catch(Exception e){
                             JOptionPane.showMessageDialog(null, "Image Load Error","ERROR", JOptionPane.ERROR_MESSAGE);
                             e.printStackTrace();
                        }
                   }
              I get an Image Load Error on Windows, but the same file on Mac runs perfect
              • 4. Re: Images in Jar files
                tyelford
                See Last post
                • 5. Re: Images in Jar files
                  camickr
                  There is no need to check the OS. Windows will also recognize the forward slash. Just use:
                  imgLocal = "resources/RecipeMagic1_5/RecipeMagic1_5.gif";
                  in all cases.
                  1 person found this helpful
                  • 6. Re: Images in Jar files
                    tyelford
                    I also needed a / in front of it all
                    I am not sure why it needed a / but it worked with it and not with out it:
                    imgLocal = "/resources/RecipeMagic1_5/RecipeMagic1_5.gif";
                    • 7. Re: Images in Jar files
                      baftos
                      The initial forward slash is needed for Class.loadResource(). Re-read carefully the javadoc for this method.
                      Loading a resource is very different from file IO. A resource location is relative to the classpath. It may be a file, but it may also be a jar entry, for instance. File IO, on the other hand, refers to actual files seen as such by the file system when you do 'dir' in Windows or 'ls' in Unixes.