problem embedding resources

Andrew Haley aph@redhat.com
Thu Oct 20 15:37:00 GMT 2005


Camilla Orlandi writes:
 > Andrew Haley ha scritto:
 > 
 > >If you give us an example we can reproduce, with the commands and the
 > >files you use, we could help yopu.
 > >
 > >OK, you don't have to attach the GIF; we can make one ourselves.  But
 > >you do have to attach all the source code and all the commands.
 > 
 > ok, sorry, here is the HelloWorld.java code:

Ah, yes, this is a bootstrap class loader issue.

public InputStream getResourceAsStream(String name)

    ... This method delegates the call to its class loader, after
    making these changes to the resource name: if the resource name
    starts with "/", it is unchanged; otherwise, the package name is
    prepended to the resource name after converting "." to "/".  If
    this object was loaded by the bootstrap loader, the call is
    delegated to ClassLoader.getSystemResourceAsStream.

I suspect the issue here is whether or not a class that is compiled
into the executable is effectively "loaded by the bootstrap loader".
We always treat such classes as compiled by the system class loader,
not the bootstrap loader, so we don't delegate to
ClassLoader.getSystemResourceAsStream.

To fix your app, just use "/swt.gif", not "swt.gif".  Otherwise, your
resource will be resolved relative to HelloWorld.class, and gcj will
look for it as "ex/swt.gif".

Andrew.


 > 
 > 
 > *********************************
 > 
 > package ex;
 > 
 > import org.eclipse.swt.widgets.*;
 > import org.eclipse.swt.SWT;
 > import org.eclipse.swt.graphics.*;
 > 
 > public class HelloWorld {
 >    
 >     public HelloWorld(){
 >    
 >         Display d=new Display();
 >         Shell s=new Shell(d);
 >         s.setSize(100,200);
 >        
 >         Label aLabel=new Label(s, SWT.BORDER);
 >         aLabel.setBounds(0,0,100,100);
 >         aLabel.setImage(
 >                 new Image(d,
 >             HelloWorld.class
 >             .getResourceAsStream("swt.gif")));
 >        
 >         s.open();
 >        
 >         while(!s.isDisposed()){
 >             if(!d.readAndDispatch()){
 >                 d.sleep();
 >             }
 >         }
 >        
 >         d.dispose();
 >     }
 >    
 >     public static void main(String[] args){
 >         HelloWorld hw=new HelloWorld();
 >     }
 >    
 > }
 > 
 > *********************************
 > 
 > commands:
 > 
 > gcj -c -o swigtif.o --resource=swt.gif swt.gif
 > 
 > gcj -s -fjni --main=ex.HelloWorld -s -o HelloWorld.exe 
 > ex/HelloWorld.java swtgif.o
 > 
 > 
 > 
 > using the same code, not includeing it in the ex package, it's ok.
 > thanks
 > 
 > Camilla
 > 



More information about the Java mailing list