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 |
Hi,
On Fri, 6 Feb 2009, joris meys wrote: > 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? Are you sure that the class is read from the .jar file right where the image is? Or is the class in a package, while the image is on the top-level of the .jar file? Ciao, Dscho |
Hi,
Frankly, I don't really have a clue. I tried it with the tutorial I got from sir Rasband (http://rsb.info.nih.gov/ij/plugins/pcl-demo.html). I tried to put the picture in a folder inside my jarfile, but that didn't help either. I have following code now : void showPic() { URL url = getClass().getResource("/resources/FA_About.jpg"); Image img = null; try { img = Toolkit.getDefaultToolkit().getImage(url); ImagePlus imp = new ImagePlus("About Frap_Norm", img); ImageWindow.centerNextImage(); imp.show(); } catch(Exception e) { showAbout(); } } And I get this error message in the Console : Uncaught error fetching image: java.lang.NullPointerException at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:97) at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:107) at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240) at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172) at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136) The strange thing is that the code of the resource demo works just fine. Might it be caused by the fact that I put a plugins.config file in the jar which makes the void Pics() run from the help menu? I'm a bit stuck here... Kind regards On Fri, Feb 6, 2009 at 4:16 PM, Johannes Schindelin < [hidden email]> wrote: > Hi, > > On Fri, 6 Feb 2009, joris meys wrote: > > > 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? > > Are you sure that the class is read from the .jar file right where the > image is? Or is the class in a package, while the image is on the > top-level of the .jar file? > > Ciao, > Dscho > |
In reply to this post by dscho
Now I think about it, my class has some nested classes inside. I used the
jar command "jar cvfM Frap_Norm.jar *" to pack everything. Can it be that this messes up with the package thing? I have to admit, I'm quite a newbie at Java. It's my first in fact... On Fri, Feb 6, 2009 at 4:16 PM, Johannes Schindelin < [hidden email]> wrote: > Hi, > > On Fri, 6 Feb 2009, joris meys wrote: > > > 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? > > Are you sure that the class is read from the .jar file right where the > image is? Or is the class in a package, while the image is on the > top-level of the .jar file? > > Ciao, > Dscho > |
Oops...
It works. Next time when I debug a jar-file, I should remember to delete the original class-files from the directory. That seemed to be the problem. Apparently the get.Class() command will refer to the original class file, and not to the jar file. Thank you Johannes for putting me on the right track. For those who look this up later, my final code is included. Not the most elegant solution, but it does what I want it to do. Be sure to check out the tutorial of Wayne Rasband, mentioned earlier in the thread. Kind regards Joris private void showPic() { ImageJ ij = IJ.getInstance(); URL url = this.getClass().getResource("/resources/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(); } } else showAbout(); } |
Free forum by Nabble | Edit this page |