Include images while exporting a jar

I have images stored in a separate folder in my project.

When I export my project to produce a runnable .jar file, it cannot reference those images.

Please let me know what I could be missing. Thanks!
dshrenikAsked:
Who is Participating?
 
for_yanCommented:
You need to put images and other resources to the same folder as your clases and use getResources methodto use them in the program,
then it will put it in the jar
0
 
dshrenikAuthor Commented:
Can you provide some sample code? Thanks!
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
dshrenikAuthor Commented:
The images must be in the src directory with .java files or the classes directory with .class files?
0
 
for_yanCommented:


You should use:       

ClassLoader.getResourceAsStream("/path/to/your/gif/image.gif")
0
 
for_yanCommented:
tehy should be with clasess
0
 
dshrenikAuthor Commented:
Right now, I am doing:
ImageIcon im = new ImageIcon(ICON_LOCATION + "im.png");
0
 
for_yanCommented:
SomeClass.class.getResourceAsStream("path to image staring form classpath root")
0
 
for_yanCommented:
So you should change it as resource as i showeed above - make sure it works and then  it should packeg it togeteher
0
 
for_yanCommented:
This is how you do it and the HaerthFlag.gif file is in the
top-levle classes folder - once you see it works this way - it should be jarred wuith the classes

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().setLayout(new BorderLayout());

    MyTableModel model = new MyTableModel();

    JTable table = new JTable(model);
    table.setRowHeight(80);
    table.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());
    JScrollPane pane = new JScrollPane(table);
    frame.getContentPane().add(BorderLayout.CENTER, pane);
   // frame.setSize(500, 400);
      frame.pack();
    frame.setVisible(true);
      System.out.println("showed");

      try{
          while(true){
          Thread.currentThread().sleep(3000);
             model.toggleMult();
              frame.repaint();
          }

      }
          catch(Exception ex){

          }
      }

  }


class MyTableModel extends AbstractTableModel {
    boolean mult;

public void toggleMult(){
    mult = !mult;
}


  public Object getValueAt(int row, int column) {
      if(mult)
    return "" + (row * column);
      else return "" + (row + column);
  }

  public int getColumnCount() {
    return 4;
  }

  public int getRowCount() {
    return 5;
  }
}

class ImageRenderer extends DefaultTableCellRenderer {
  JLabel lbl = new JLabel();

 // ImageIcon icon = new ImageIcon("HearthFlag.gif");

    ImageIcon icon = new ImageIcon(this.getClass().getResource("HearthFlag.gif"));

    



  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column) {
    lbl.setText((String) value);
    lbl.setIcon(icon);
    return lbl;
  }
}

Open in new window

0
 
dshrenikAuthor Commented:
I have my images inside the bin folder. Still, t does not work.
0
 
for_yanCommented:
Did you change to stream

like this

 ImageIcon icon = new ImageIcon(this.getClass().getResource("HearthFlag.gif")); ?

Do you have classes iin you bin folder?
0
 
dshrenikAuthor Commented:
I do, but the classes are in a package structure.
0
 
Gurvinder Pal SinghCommented:
<<I have my images inside the bin folder. Still, t does not work.>>
did you checked the URL i mentioned above?
0
 
dshrenikAuthor Commented:
@gurvinder372:
I did.
0
 
for_yanCommented:
It looks like Eclipse does not put gif file into jar automatically, see some discussion here:
http://stackoverflow.com/questions/2073250/eclipse-export-to-jar-and-include-resource-files-ant

maybe you still would need to add it manually.
But this is anyway correct way to use it:

 ImageIcon icon = new ImageIcon(this.getClass().getResource("HearthFlag.gif")); ?
0
 
dshrenikAuthor Commented:
Can you tell me how I should do it manually? Thanks!
0
 
for_yanCommented:
No it still should do it.
Probably it needs to be imported into project.
Let me see
0
 
CEHJCommented:
Which IDE (if any) are you using?
0
 
dshrenikAuthor Commented:
I am using Eclipse.
0
 
CEHJCommented:
>>You need to put images and other resources to the same folder as your clases

That's generally incorrect. What you should do is add a resources folder under you *source* root and add the images to that. They then should be copied to the correct place by your IDE when the application is jarred
0
 
for_yanCommented:
Yes it is true - I made amistake it should be in the src - but still it does not work
0
 
for_yanCommented:
0
 
for_yanCommented:
OK - it finally worked for me
0
 
dshrenikAuthor Commented:
How!?
0
 
for_yanCommented:


yuse this description - and import your gif
http://stackoverflow.com/questions/3721706/embedding-resources-images-sound-bits-etc-into-a-java-project-then-use-those 

    1) I imported the image by Right-Click on the Project -> Import -> General/File System for the import source

    2) Selected the folder which contained the image to import

    3) Selected "Project/src/Viking/Test/Resources" for the 'Into folder' parameter


0
 
for_yanCommented:
After you import it into the project - make surre wiuthin the project to move it into the src foldeer
(you can just drag in the lft column - project browser to that src folder if ut is initially outside src)
0
 
for_yanCommented:
Actually then test that its is working for you within Eclipse  using this kind of path :
 ImageIcon icon = new ImageIcon(this.getClass().getResource("HearthFlag.gif")); ?
0
 
for_yanCommented:
And then ceate Runnable jar as we did before , I also clicked "package required libraries into generated jar" before clicking Finish
(don't know if that is cruycial, but I clicked this checkbox)
Then it will appear in the runnable jar
0
 
CEHJCommented:
You should normally use a path at least one folder deep to a resource for proper organisation
0
 
CEHJCommented:
>> I also clicked "package required libraries into generated jar"

It's nothing to do with libraries. In fact all that's required is that 'Export generated class files and resources' be checked, which should be checked by default
0
 
for_yanCommented:
The most important thisng is tio have the gif file imported into the project.
It is not enough to plavce it in the fiolder - you need to import it into the project - and the way to chek it - that it should work
with this getClass().getResource.. studd when you excute it within Eclipse
Once you achieve that - it will go into Runnable jar
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.