Login  Register

Accessing picture file in a jar file

Posted by Joris FA Meys on Feb 06, 2009; 2:23pm
URL: http://imagej.273.s1.nabble.com/Accessing-picture-file-in-a-jar-file-tp3693821.html

Dear all,

I want to access a .jpg file in the jar file I constructed for distribution
of my plugin. It's a picture with the "About" information, like the "ImageJ
About". If the picture itself is in the plugin directory, it works perfectly
fine. However, if it's only in the jar-file, it doesn't want to find the
picture. What am I doing wrong?

private void showPic() {
        ImageJ ij = IJ.getInstance();
        ClassLoader cl = this.getClass().getClassLoader();
        URL url = cl.getResource("FA_About.jpg");
        if (url!=null) {
            Image img = null;
            try {img = ij.createImage((ImageProducer)url.getContent());}
            catch(Exception e) {}
            if (img!=null) {
                ImagePlus imp = new ImagePlus("", img);
                ImageWindow.centerNextImage();
                imp.show();
            }
        }


Kind regards
Joris