?
Solved

Include images while exporting a jar

Posted on 2011-10-13
32
Medium Priority
?
257 Views
Last Modified: 2012-05-12
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!
0
Comment
Question by:dshrenik
  • 17
  • 9
  • 4
  • +1
32 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36966809
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
 

Author Comment

by:dshrenik
ID: 36966814
Can you provide some sample code? Thanks!
0
 

Author Comment

by:dshrenik
ID: 36966817
The images must be in the src directory with .java files or the classes directory with .class files?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 47

Expert Comment

by:for_yan
ID: 36966818


You should use:       

ClassLoader.getResourceAsStream("/path/to/your/gif/image.gif")
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36966822
tehy should be with clasess
0
 

Author Comment

by:dshrenik
ID: 36966823
Right now, I am doing:
ImageIcon im = new ImageIcon(ICON_LOCATION + "im.png");
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36966825
SomeClass.class.getResourceAsStream("path to image staring form classpath root")
0
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 500 total points
ID: 36966827
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36966828
So you should change it as resource as i showeed above - make sure it works and then  it should packeg it togeteher
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36966857
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
 

Author Comment

by:dshrenik
ID: 36966858
I have my images inside the bin folder. Still, t does not work.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36966863
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
 

Author Comment

by:dshrenik
ID: 36966870
I do, but the classes are in a package structure.
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36966872
<<I have my images inside the bin folder. Still, t does not work.>>
did you checked the URL i mentioned above?
0
 

Author Comment

by:dshrenik
ID: 36966874
@gurvinder372:
I did.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36966906
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
 

Author Comment

by:dshrenik
ID: 36966910
Can you tell me how I should do it manually? Thanks!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36966915
No it still should do it.
Probably it needs to be imported into project.
Let me see
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36966920
Which IDE (if any) are you using?
0
 

Author Comment

by:dshrenik
ID: 36966923
I am using Eclipse.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36966936
>>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
 
LVL 47

Expert Comment

by:for_yan
ID: 36966941
Yes it is true - I made amistake it should be in the src - but still it does not work
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36966956
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36966970
OK - it finally worked for me
0
 

Author Comment

by:dshrenik
ID: 36966973
How!?
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1000 total points
ID: 36966974


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
 
LVL 47

Expert Comment

by:for_yan
ID: 36966978
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36966980
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36966985
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 36966996
You should normally use a path at least one folder deep to a resource for proper organisation
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36967161
>> 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
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 1000 total points
ID: 36967173
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question