Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2006

Tip: Looking for answers? Try searching our database.

configuration required to load icons from disk

Thread view: 
Brandon McCombs - 19 Feb 2006 00:08 GMT
What all is needed to be configured in order to load a gif from within
my own jar file and use it within my GUI?  I've set a CLASSPATH variable
in my Command window (WinXP) and that doesn't work.  I've included the
.classpath file from the Eclipse workspace directory in my jar file and
that doesn't work. I can get the icon to load if I'm in Eclipse or if I
run my jar file from within my workspace directory and I specify the
relative path of the icon using the filesystem path to it but that ends
up using the copy of the icon on the hard drive instead of the one
inside my jar. If I call LDAPMgr.class.getResource("images/icon.gif")) I
get a null pointer exception and I don't know why since the images
folder exists in my jar file at the top level and the icon file is in
that folder.  LDAPMgr is the name of my main class file and it is the
name of my jar file.  So what the hell else is there?

thanks
Ian Mills - 19 Feb 2006 09:51 GMT
> What all is needed to be configured in order to load a gif from within
> my own jar file and use it within my GUI?  I've set a CLASSPATH variable
[quoted text clipped - 11 lines]
>
> thanks

You could try ClassLoader.getSystemResource("images/icon.gif") and see
if this makes any difference.
Brandon McCombs - 19 Feb 2006 22:47 GMT
>> What all is needed to be configured in order to load a gif from within
>> my own jar file and use it within my GUI?  I've set a CLASSPATH
[quoted text clipped - 15 lines]
> You could try ClassLoader.getSystemResource("images/icon.gif") and see
> if this makes any difference.

I still get a null pointer exception with that.
hiwa - 19 Feb 2006 23:41 GMT
URL url = getResource("images/icon.gif")); per se should be no-problem.
It always works on any program if you JARed the images directory along
with your app classes.

You have bugs in other part(s) of your code.
Brandon McCombs - 20 Feb 2006 00:06 GMT
> URL url = getResource("images/icon.gif")); per se should be no-problem.
> It always works on any program if you JARed the images directory along
> with your app classes.
>
> You have bugs in other part(s) of your code.

I tried your idea. I still get null pointer.

if (entry.getNodeType().equals("domain")) {
  URL url = LDAPMgr.class.getResource("images/domIcon.gif");
  //ImageIcon ico = new ImageIcon("images/domIcon.GIF");
  // ImageIcon ico = new                            
ImageIcon(LDAPMgr.class.getResource("./images/domIcon.gif"));
  ImageIcon ico = new ImageIcon(url);
  System.out.println(ico.toString());
  setIcon(ico);
}

How can I have bugs in other parts when no other parts need to be
involved in order to get the icon to appear?  Every time I use the
normal ImageIcon("images/icon.gif") method I see it print out the full
path to the icon. WHy it would have the full path and more importantly
how did it even get the full path since I didn't specify it?  That
happens even when I run from the jar file but what's worse is that the
icon doesn't even show up despite the path being correct to the file (so
far as it is pointing to a file that actually exists).  How do I force
the damn thing to look at the files in the jar file and not something
outside of the jar file?

This works fine as long as I point to an icon outside the jar file *and*
while I use eclipse to run the app:  the path actually points to the
icon and the icon appears so I don't see how there are bugs somewhere.
The same code should work whether the icon is in or outside of the jar
file in my opinion.
hiwa - 20 Feb 2006 01:04 GMT
Then, the cause of the problem should be some combination of:
(1)Where is your jar file?
(2)Where is your current directory? -- NOT the current directory of the
Eclipse tool.
(3)What command do you issue to run the jar?
Brandon McCombs - 20 Feb 2006 01:29 GMT
> Then, the cause of the problem should be some combination of:
> (1)Where is your jar file?

I put my jar file in a place where my code can't accidentally use the
icon that is on the filesystem so i know for sure whether it works or
not (and so far it hasn't for this problem).

My images are here:
K:\myJava\workspace\LDAPMgr\images

When I test my jar file I place it here:
K:\myJava\

In the jar file there is a top-level images/ folder where the gifs are
stored in the jar.

If I placed the jar in K:\myJava\workspace\ and in my code I use
new ImageIcon("LDAPMgr/images/icon.gif") then it will end up going to
the subdirectory LDAPMgr in my workspace directory and get the images
that are outside the jar file and seemingly load just fine but I printed
out the path to the icon and I can tell the icon loaded is not the one
from the jar file.

> (2)Where is your current directory? -- NOT the current directory of the
> Eclipse tool.

Current directory when? WHen I run the jar?  It it would be K:\myjava\
> (3)What command do you issue to run the jar?

I use java -jar LDAPMgr.jar

Is there anything wrong with any of that?  Why would any of that matter
as far as the icon in the jar file being used?  It seems right now the
first problem is figuring out why the JVM looks outside the jar file
when I give it a path to use so anything happening or configured outside
the jar file should be irrelevant since I want to reference something
inside the jar file.
hiwa - 20 Feb 2006 05:02 GMT
Try this plain vanilla simplest one on your environment after you
download it:
http://homepage1.nifty.com/algafield/BgImg.jar

If it does work on your machine, the cause of the problem might be some
config
issue for your development environment we can't make stabs in the dark
about it.

[souce code]
import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import javax.swing.border.*;
import java.net.*;

public class BgImgScrollPane extends JScrollPane{
 private ImageIcon background;

 public BgImgScrollPane(Component view){
   super(view);
 }

 public void setBackgroundIcon(ImageIcon p){
   background = p;
 }

 public final ImageIcon getBackgroundIcon(){
   return background;
 }

 public void paintComponent(Graphics g){
   if (background != null){
     g.drawImage(background.getImage(), 0, 0, getWidth(), getHeight(),
this);
   }
   Border border = getViewportBorder();
   if (border != null){
     Rectangle r = getViewportBorderBounds();
     border.paintBorder(this, g, r.x, r.y, r.width, r.height);
   }
 }

 public static void display(Component c, URL imgUrl){
   JFrame f = new JFrame();
   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

   BgImgScrollPane pane = new BgImgScrollPane(c);
   pane.setBackgroundIcon(new ImageIcon(imgUrl));
   pane.getViewport().setOpaque(false);

   JViewport column = new JViewport();
   column.setOpaque(false);
   pane.setColumnHeader(column);
   JViewport row = new JViewport();
   row.setOpaque(false);
   pane.setRowHeader(row);

   f.getContentPane().add(pane);
   f.pack();
   f.setVisible(true);
 }

 public static void main(String[] args) throws Exception{
   URL url = BgImgScrollPane.class.getResource("images/rose.gif");

   JTable table = new JTable(15, 4){
     public Component prepareRenderer
      (TableCellRenderer renderer, int row, int column){
       Component c = super.prepareRenderer( renderer, row, column);
       if (c instanceof JComponent){
         ((JComponent)c).setOpaque(false);
       }
       return c;
     }
   };
   table.setOpaque(false);

   BgImgScrollPane.display(table, url);
 }
}
Brandon McCombs - 20 Feb 2006 05:09 GMT
> Try this plain vanilla simplest one on your environment after you
> download it:
[quoted text clipped - 6 lines]
>
> [souce code]
[snipped]

Here are the results of running that(happens in eclipse or in a jar file):

K:\myJava>java -jar icon.jar
Exception in thread "main" java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(Unknown Source)
        at BgImgScrollPane.display(BgImgScrollPane.java:39)
        at BgImgScrollPane.main(BgImgScrollPane.java:69)

line 39 is pane.setBackgroundIcon(new ImageIcon(imgUrl));

Since this doesn't work what would be at fault? I'm using java 5.0 (1.5)
hiwa - 20 Feb 2006 06:37 GMT
> Since this doesn't work what would be at fault?
This simple and benign statement:
URL url = BgImgScrollPane.class.getResource("images/rose.gif");
doesn't return viable URL in your environment.

If your environment is purely a single set of a Sun JDK(javac,
java/JVM, core APIs, jar tool, and everything), this simple
BgImg.jar executable CAN'T fail, never.

If it is a mish-mash or has some incompatible components,
then anything can happen.

Try leave and forget Eclipse and all the attached IBM
products, at least for a moment.
Brandon McCombs - 20 Feb 2006 15:22 GMT
>> Since this doesn't work what would be at fault?
> This simple and benign statement:
[quoted text clipped - 4 lines]
> java/JVM, core APIs, jar tool, and everything), this simple
> BgImg.jar executable CAN'T fail, never.

well i have a 1.4 jdk and 1.5 jre installed however I have eclipse set
up to run with 1.5 (not sure what it is using to compile but I don't see
how it could be using 1.4 because I know when I manually compile
something with 1.4 it won't run with 1.5).

> If it is a mish-mash or has some incompatible components,
> then anything can happen.
>
> Try leave and forget Eclipse and all the attached IBM
> products, at least for a moment.

And then do what?  I've only been using eclipse to compile stuff (as far
as the jar file is concerned) until yesterday when I started to use it
for creating the jar file as well.  I've been running the jar file on
the command line and using the java.exe from JRE 1.5.
Brandon McCombs - 20 Feb 2006 04:56 GMT
> Then, the cause of the problem should be some combination of:
> (1)Where is your jar file?
> (2)Where is your current directory? -- NOT the current directory of the
> Eclipse tool.
> (3)What command do you issue to run the jar?

When i use :
ImageIcon ico = new ImageIcon("images/Icon.gif");

I don't get a nullPointer exception but if I print out the size of the
image it is -1 (and it also never shows up in the GUI).  WHy would it do
that?
hiwa - 20 Feb 2006 05:07 GMT
> WHy would it do that?
The jar tool had used wrong directory and wrong files in it ????
hiwa - 20 Feb 2006 05:10 GMT
> WHy would it do that?
Or, your jar command command-line was wrong when you had made the jar
file in question.
Try extracting all the files from the jar file. Icon.gif is still an
emty file?
Brandon McCombs - 20 Feb 2006 05:13 GMT
>> WHy would it do that?
> Or, your jar command command-line was wrong when you had made the jar
> file in question.
> Try extracting all the files from the jar file. Icon.gif is still an
> emty file?

I'm using eclipse now to build the jar file (seems to take longer doing
it that way then just running the jar command and specifying all my
parameters) and either way (eclipse or commandline) the images folder
has images in it and they are not 0 size. I extracted one of them and it
was perfectly viewable.
Brandon McCombs - 20 Feb 2006 05:11 GMT
>> WHy would it do that?
> The jar tool had used wrong directory and wrong files in it ????

if that is the case then where the hell is the authority in this java
mess that tells the jar file from where it is supposed to load its
images whenever the getResource() method is called?  If it's using the
wrong directory then something had to tell it the wrong directory.
Roedy Green - 23 Feb 2006 05:46 GMT
>When i use :
>ImageIcon ico = new ImageIcon("images/Icon.gif");
>
>I don't get a nullPointer exception but if I print out the size of the
>image it is -1 (and it also never shows up in the GUI).  WHy would it do
>that?

see http://mindprod.com/jgloss/image.html
see http://mindprod.com/jgloss/resource.html

To track resource problems down, examine the jar to make sure the
resources are exactly in the jars named exactly as you planned with
perfect spelling and case.

use getResource and print out the URL to see if it is as you expected.

Narrowing down where it is screwing up is 80% of solving it.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Nigel Wade - 20 Feb 2006 10:01 GMT
>> URL url = getResource("images/icon.gif")); per se should be no-problem.
>> It always works on any program if you JARed the images directory along
[quoted text clipped - 6 lines]
> if (entry.getNodeType().equals("domain")) {
>    URL url = LDAPMgr.class.getResource("images/domIcon.gif");

Try specifying an absolute path in the JAR file:

   URL url = LDAPMgr.class.getResource("/images/domIcon.gif");

What is the directory structure of your jar?

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

Brandon McCombs - 20 Feb 2006 15:24 GMT
>>> URL url = getResource("images/icon.gif")); per se should be no-problem.
>>> It always works on any program if you JARed the images directory along
[quoted text clipped - 12 lines]
>
> What is the directory structure of your jar?

that produces a null pointer exception as well when I call:
    new ImageIcon(url).
Brandon McCombs - 20 Feb 2006 15:26 GMT
>>> URL url = getResource("images/icon.gif")); per se should be no-problem.
>>> It always works on any program if you JARed the images directory along
[quoted text clipped - 12 lines]
>
> What is the directory structure of your jar?

is LDAPMgr.class.getResource() supposed to pull a file from a jar file
no matter what or will it just pull something out of the images/
subdirectory that exists (or not) below the current working directory
when I run the jar file?  If it's the latter that is obviously not what
I want so how would I change that?
Brandon McCombs - 20 Feb 2006 17:21 GMT
>>> URL url = getResource("images/icon.gif")); per se should be no-problem.
>>> It always works on any program if you JARed the images directory along
[quoted text clipped - 12 lines]
>
> What is the directory structure of your jar?

i forgot to mention the directory structure of my jar.

I have a bunch of class files and a folder called images. Inside the
images folder are my GIF files. There should be no reason why they can't
be loaded, of course it would be nice if the getResource() method would
actually look at the images folder in my jar instead of the images
folder that is on the file system outside the jar. If it did that then
it would probably be working correctly.
hiwa - 21 Feb 2006 06:57 GMT
Seems we are all stuck.
Will you try to make an extremely simple program
with a single class of which only method is main()
which accesses images/icon.gif file via
Class.getResource() method. Make a jar file for
the program and run java -jar command on it.

If the jar file do not run on your environment, put
it on a universally downloadable site. We will
try it on our environment. It will be better to
include the source file in the jar file.
opalpa@gmail.com - 22 Feb 2006 02:34 GMT
Using windows?  getResource in jar file is case-sensitive, getResource
outside of jar file is case-insenstivie.  Anyway getResource works.
Here is an example program.  All the best folks.

package experiment;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.net.URL;
import java.io.*;
import javax.swing.*;
final class GetResourcePic {
 private GetResourcePic() {}
 public static void window(final String title, final Image body)
   throws java.awt.AWTException
 {
   javax.swing.SwingUtilities.invokeLater(new Runnable() {
     public void run() {
       JFrame frame = new JFrame(title);
       // JFrame.setDefaultLookAndFeelDecorated(true);
       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       frame.getContentPane().add(new JLabel(new ImageIcon(body)));
       frame.pack();
       frame.setLocationRelativeTo(null);
       frame.setVisible(true);
     }
   }); // funky
 }
 public static void main(String a[]) throws AWTException, IOException
{
   URL picurl =
GetResourcePic.class.getResource("GetResourcePic.gif");
   System.out.println(""+picurl);
   BufferedImage img = ImageIO.read(picurl);
   window("GetResourcePic", img);
 }

}

with manifest file:
Main-Class: experiment.GetResourcePic

Make/Get an image file, name it case sensitively,GetResourcePic.gif
(NOTE: case matters).

Compile into classes.  Move image file to same directory that contains
GetResourcePic.class. See code run.  Make jar.  (check jar has all
classes and image file in correct directory, check case sensitivity)
See code run.

Change gif in source to giF (NOTE: capital at end).  Compile into
classes.  See code run.  Make jar.  See code fail.

Opalinski
opa...@gmail.com
http://www.geocities.com/opalpaweb/
Brandon McCombs - 22 Feb 2006 04:17 GMT
> Using windows?  getResource in jar file is case-sensitive, getResource
> outside of jar file is case-insenstivie.  Anyway getResource works.
> Here is an example program.  All the best folks.

Just wanted to respond here so people know that the case-sensitivity
issue was the problem for me.  My file extensions were uppercase (thanks
to MS Paint) and I was using lowercase in my code.

thanks Opalinski
hiwa - 22 Feb 2006 04:55 GMT
Brandon McCombs :

> > Using windows?  getResource in jar file is case-sensitive, getResource
> > outside of jar file is case-insenstivie.  Anyway getResource works.
[quoted text clipped - 5 lines]
>
> thanks Opalinski
Struck dumb.
Too anticlimatical to comment ...

We take it for granted that modern programmer use correct file name in
his/her coding because DOS era is completely far far past.
Brandon McCombs - 23 Feb 2006 03:03 GMT
> Brandon McCombs のメッセージ:
>
[quoted text clipped - 12 lines]
> We take it for granted that modern programmer use correct file name in
> his/her coding because DOS era is completely far far past.

what does DOS era have to do with anything? DOS wasn't case sensitive so
I don't see the relevancy. If Java wasn't case sensitive with respect to
the jar files there wouldn't be an issue (or if Paint didn't default to
uppercase for its file extensions). I technically was using the correct
filename because it was working as expected in eclipse. And oddly enough
I did NOT see anything on any website regarding case sensitivity with
jar files- not even on the sun tutorial pages for java.
hiwa - 23 Feb 2006 03:40 GMT
> I technically was using the correct
> filename because it was working
It's just a rotten relationship between some
of Windows system calls and DOS legacy.
Java file related API happens to call the
system call directly -- they should have a
rationale for doing it.

But, we programmer should write "ICON.GIF"
if the file name is ICON.GIF. We should not
depend on platform specific quirk in Java
coding.
Roedy Green - 23 Feb 2006 05:56 GMT
>But, we programmer should write "ICON.GIF"
>if the file name is ICON.GIF. We should not
>depend on platform specific quirk in Java
>coding

Exactly.  Similarly when I write HTML I have to be careful about case
in URLs. It works fine locally with bad case, but screws up when I
post it on a Unix server which is case sensitive.

One nice thing about using jars is they are picky.  You don't get
lulled into thinking all is ok when it is not.  It is so embarrassing
when you give code to other people that does not work, that works fine
on your machine.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 23 Feb 2006 06:37 GMT
>But, we programmer should write "ICON.GIF"
>if the file name is ICON.GIF. We should not
>depend on platform specific quirk in Java
>coding.

see http://mindprod.com/jgloss/casesensitivity.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 23 Feb 2006 05:53 GMT
>what does DOS era have to do with anything? DOS wasn't case sensitive so
>I don't see the relevancy

Exactly. Case did not matter in the old days and still sometimes does
not, e..g. fetching a windows file name.   It is hard for oldtimers to
see case errors as equally glaring as spelling errors.

My  way of handling this is to be rigid in the ways I name things so I
KNOW how the caps will go. Even though I use windows, I make sure all
my filenames are precise in case.  Windows balks when I try to fix
those errors. I sometimes have to rename the file to something else
then to what I really want to get it to believe me that I truly care
about getting the case right.  To add to the confusion, some programs
change the true case before displaying it, thinking old all-caps names
are uncouth, for example.

e.g. package names are ALWAYS pure lower case.

classes ALWAYS start with a capital letter and have important embedded
words in caps.

IF you are the tiniest bit sloppy about his, you can stare and stare
and not see the error.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.