I wrote:

> What is the meaning of url.toExportedForm() from image contained in JAR 
> called something like this?
> 
> systemresources://FILE1/+/images/new.gif
> 
> I had a lot of trouble reading image file (GIFs) from JARs.

I had a long look at the FAQ (www.afu.com) and applied the getResourceAsStream()
example there. 


This normally results in stack trace, which I forget to add in the last post:

java.lang.NullPointerException: 
        at java.util.Hashtable.get(Hashtable.java)
        at sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:117)
        at sun.awt.SunToolkit.getImage(SunToolkit.java:129)
        at com.sun.java.swing.ImageIcon.<init>(ImageIcon.java:64)
        at com.sun.java.swing.ImageIcon.<init>(ImageIcon.java:83)
        at xenon.jsql.editor.JsqlEdit.<init>(JsqlEdit.java:173)
        at xenon.jsql.editor.JsqlEditList.createNewFrame(JsqlEditList.java:101)
        at xenon.jsql.editor.Jsql.<init>(Jsql.java:144)
        at xenon.jsql.editor.Jsql.main(Jsql.java:592)


Here is some code:

    /** retrieves the image (GIF/JPEG) data from URL.
     * implements performs URL.
     */
    protected byte[] getImageData( String url_image )
    {
    byte[] imageBytes=null;
    if (debug) System.out.println( "getImageData("+url_image+")" );
    
    //
    // First try reading the resource as a system file.
    //
    InputStream imageStream=null;        // Reset this
    try {
        imageStream = new FileInputStream( url_image );
    }
    catch ( FileNotFoundException fe ) { ; }
    
    if (imageStream == null) {
        //
        // Well that didn't work
        // Second, Try to opening the resource as an URL specification.
        //
        try {
        URL url = new URL( url_image );
        // Try creating a socket which connects to the
        // int port = (url.getPort() == -1) ? 80: url.getPort();
        // Socket s = new Socket( url.getHost, port );
        imageStream = url.openStream();
        if (debug) System.out.println("*opening stream URL*");
        }
        catch (IOException ioe ) { ; }
    }
    
    if (imageStream == null)
        //
        // Well that also didn't work
        //
        // Third, Try to opening the resource as as CLASSPATH specification.
        // e.g. It may be part of JAR or ZIP
        imageStream = getClass().getClassLoader().getSystemResourceAsStream(url_image);
    Image img = null;
    try {
        if (debug) 
        System.out.println("imageStream:" + 
                   ((imageStream==null) ?"NULL!!":"EXISTS") );
        imageBytes = new byte[imageStream.available()];
        imageStream.read(imageBytes);
    }
    catch (IOException ex) {
        System.err.println( getClass().getName()+".getImageData: failed to load image 
from URL: " + url_image );
    }

    if (imageBytes != null)
        System.out.println( "imageBytes.length =" + imageBytes.length );

    return (imageBytes);
    }
    

Solution?

BTW: linux-jdk1.1.6-libc5 .tar.gz   swing-1.1beta.zip 



Cheers

Peter

--
import java.std.disclaimer.*;           // "Dontcha just love the API, baby bop!"
Peter Pilgrim      Dept:OTC Derivatives IT, 
Deutsche Bank (UK) Ltd, Groundfloor 133 Houndsditch, London, EC3A 7DX
Tel: +44-545-8000  Direct: +44 (0)171-545-9977  Fax: 0171-545-4313
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Reply via email to