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 / General / October 2007

Tip: Looking for answers? Try searching our database.

Can't find file via class.getResource()

Thread view: 
Ryan - 17 Oct 2007 21:08 GMT
I know, I know... if the file's location is on the classpath, it
should work. Well, it doesn't. No matter what I try.

I have a simple app with a toolbar which I'm putting icons on. I have
a very simple directory structure....

- bin - holds a run scripts
- lib - contains all needed jar files
- images - contains gifs, jpgs etc etc
- audio - contains audio files

My code create a JButton with an Action and adds it to a JToolbar.
That Action....

        putValue(SMALL_ICON, createIcon(smallIcon));

where the icon is created by.....

        URL imageURL = getClass().getResource("test.gif");
        if (imageURL == null)
        {
        System.out.println("Resource not found");
        return null;
        }
        return new ImageIcon(imageURL);

What is the deal? I've substituted "test.gif" with "../images/
test.gif" and every other possibility. I've even copied the gif files
into the jar file (which clearly is on the classpath since the app is
up and running) and it still can't find the resource.

Any thoughts would be great.
Mark Space - 17 Oct 2007 23:09 GMT
>  - bin - holds a run scripts
>  - lib - contains all needed jar files
>  - images - contains gifs, jpgs etc etc
>  - audio - contains audio files

Which one of these is on your class path?  Where?  Where are the .class
files?  Is this inside a .jar?  Is this inside an IDE?  Give us a bit
more info here. e.g. copy-paste your classpath so we can see it,
copy-paste a directory listing of your project, etc.

> What is the deal? I've substituted "test.gif" with "../images/
> test.gif" and every other possibility. I've even copied the gif files

Just a hunch, try "/images/test.gif"

> into the jar file (which clearly is on the classpath since the app is
> up and running) and it still can't find the resource.

Where in the jar file? root? /images? what?
Andrew Thompson - 18 Oct 2007 00:03 GMT
...
>Any thoughts would be great.

Since the success or failure of getResource() can be
determined by where it is called from the code, I suggest
you post an SSCCE* that demonstrates that.  Try and
find the source file itself, for the code example.

* <http://www.physci.org/codes/sscce.html>

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Jean-Baptiste Nizet - 18 Oct 2007 12:55 GMT
Ryan a écrit :
> I know, I know... if the file's location is on the classpath, it
> should work. Well, it doesn't. No matter what I try.
[quoted text clipped - 15 lines]
>
>         URL imageURL = getClass().getResource("test.gif");

This code is searching for a file in the classpath, in the same package
as the class on which the method above is called.
Put the image file next to your .class file.
Also, if the class is named MyClass, rather use
MyClass.class.getResource() rather than getClass().getResource, else you
will have to put the image file next to every subclass of MyClass.

JB.


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.