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 / March 2006

Tip: Looking for answers? Try searching our database.

Help with getClass().getResourceAsStream() in static context

Thread view: 
Knute Johnson - 31 Mar 2006 00:14 GMT
I've got a Swing app where I need to load an image out of the jar file
when the GUI is created.  As a result I have no 'this' to call
getClass() on.  How do I get a class that I can call
getResourceAsStream() on in a static context?

Signature

Knute Johnson
email s/nospam/knute/

Knute Johnson - 31 Mar 2006 00:36 GMT
> I've got a Swing app where I need to load an image out of the jar file
> when the GUI is created.  As a result I have no 'this' to call
> getClass() on.  How do I get a class that I can call
> getResourceAsStream() on in a static context?

Stupid me, you can get there from ClassLoader too.

Signature

Knute Johnson
email s/nospam/knute/

Knute Johnson - 31 Mar 2006 01:00 GMT
>> I've got a Swing app where I need to load an image out of the jar file
>> when the GUI is created.  As a result I have no 'this' to call
>> getClass() on.  How do I get a class that I can call
>> getResourceAsStream() on in a static context?
>
> Stupid me, you can get there from ClassLoader too.

Turns out that doesn't work.  Help!

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;

public class test {
    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                try {
                    BufferedImage bi = null;
                    InputStream is = ClassLoader.getSystemClassLoader().
                     getResourceAsStream("background.jpg");
                    if (is == null)
                        System.out.println(is);
                    else
                        bi = ImageIO.read(is);
                } catch (IOException ioe) { ioe.printStackTrace(); }
            }
        };
        EventQueue.invokeLater(r);
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Knute Johnson - 31 Mar 2006 01:15 GMT
>>> I've got a Swing app where I need to load an image out of the jar
>>> file when the GUI is created.  As a result I have no 'this' to call
[quoted text clipped - 4 lines]
>
> Turns out that doesn't work.  Help!

I love it when I answer my own questions just not when I'm talking to
myself in public.  The answer is:

test.class.getResourceAsStream()

'test' being the static class name.

Signature

Knute Johnson
email s/nospam/knute/

Roedy Green - 31 Mar 2006 03:52 GMT
On Thu, 30 Mar 2006 16:15:49 -0800, Knute Johnson
<nospam@ljr-2.frazmtn.com> wrote, quoted or indirectly quoted someone
who said :

>I love it when I answer my own questions just not when I'm talking to
>myself in public.  The answer is:
>
>test.class.getResourceAsStream()

Alternatively, you could have looked in the Java glossary under
either resource or getResourceAsStream.

http://mindprod.com/jgloss/resource.html
http://mindprod.com/jgloss/getresourceasstream.html
Signature

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

Knute Johnson - 31 Mar 2006 04:53 GMT
> Alternatively, you could have looked in the Java glossary under
> either resource or getResourceAsStream.
>
> http://mindprod.com/jgloss/resource.html
> http://mindprod.com/jgloss/getresourceasstream.html

I don't take enough advantage of the excellent work you have done on the
glossary.  I spent a long time trying to figure out what I was looking
for before I found it.

Thanks,

Signature

Knute Johnson
email s/nospam/knute/



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.