Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Can't find file via class.getResource()

Reply
Thread Tools

Can't find file via class.getResource()

 
 
Ryan
Guest
Posts: n/a
 
      10-17-2007
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.

 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      10-17-2007
Ryan wrote:

> - 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?

 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      10-17-2007
Ryan wrote:
...
>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>

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

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200710/1

 
Reply With Quote
 
Jean-Baptiste Nizet
Guest
Posts: n/a
 
      10-18-2007
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.
>
> 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");


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 3 02-14-2011 05:28 AM
How to exclude action of Find::Find::find in subdirectories withknown names? vdvorkin Perl Misc 0 02-10-2011 05:18 PM
find a matching pattern in file and find it in another file too nani Perl Misc 2 03-14-2008 05:20 AM
Find.find does not find orphaned links? Wybo Dekker Ruby 1 11-15-2005 02:50 PM
Detect if stdin is via keyboard or via < file? srbstar@sbcglobal.net C Programming 3 08-31-2005 11:20 AM



Advertisments