Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to load an image from a JAR
How to load an image from a JAR [message #451134] Thu, 24 February 2005 04:32 Go to next message
Matt Rossner is currently offline Matt RossnerFriend
Messages: 6
Registered: July 2009
Junior Member
Hi,

I am simply trying to get an icon in my title bar which I have done
successfully, but it only works with an absolute path. I want to make it
relative to the project, or better yet, relative to the JAR file that it
will be exported to. Is there some way to reference the image by package,
like com.images.myimage.gif?

This is my current code.

Image icon = new Image(display, "F:\\some_image.gif");
parent.setImage(icon);

Thanks for any advice on this

Matt
Re: How to load an image from a JAR [message #451140 is a reply to message #451134] Thu, 24 February 2005 11:40 Go to previous messageGo to next message
Philippe Marschall is currently offline Philippe MarschallFriend
Messages: 121
Registered: July 2009
Senior Member
Something like this should work

InputStream is =
this.getCalss().getClassLoader().getResourceAsStream("com/images/myimage.gif ");
Image icon = new Image(display, is);


Matt Rossner wrote:
> Hi,
>
> I am simply trying to get an icon in my title bar which I have done
> successfully, but it only works with an absolute path. I want to make it
> relative to the project, or better yet, relative to the JAR file that it
> will be exported to. Is there some way to reference the image by package,
> like com.images.myimage.gif?
>
> This is my current code.
>
> Image icon = new Image(display, "F:\\some_image.gif");
> parent.setImage(icon);
>
> Thanks for any advice on this
>
> Matt
>
>
Re: How to load an image from a JAR [message #451188 is a reply to message #451140] Thu, 24 February 2005 13:11 Go to previous messageGo to next message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
Don't forget to close the stream.

"Philippe Marschall" <kustos@gmx.net> wrote in message
news:cvkefp$tjh$1@www.eclipse.org...
> Something like this should work
>
> InputStream is =
>
this.getCalss().getClassLoader().getResourceAsStream("com/images/myimage.gif
");
> Image icon = new Image(display, is);
>
>
> Matt Rossner wrote:
> > Hi,
> >
> > I am simply trying to get an icon in my title bar which I have done
> > successfully, but it only works with an absolute path. I want to make it
> > relative to the project, or better yet, relative to the JAR file that it
> > will be exported to. Is there some way to reference the image by
package,
> > like com.images.myimage.gif?
> >
> > This is my current code.
> >
> > Image icon = new Image(display, "F:\\some_image.gif");
> > parent.setImage(icon);
> >
> > Thanks for any advice on this
> >
> > Matt
> >
> >
Re: How to load an image from a JAR [message #451194 is a reply to message #451188] Thu, 24 February 2005 14:26 Go to previous messageGo to next message
Benjamin Pasero is currently offline Benjamin PaseroFriend
Messages: 337
Registered: July 2009
Senior Member
Uhm, I was thinking the new Image would manage to close the stream?

Now looking at the API, on the one hand it is told "Application code
is still responsible for closing the input stream" on the other hand
an example is given with

"This constructor may be used to load a resource as follows:
new Image(device, clazz.getResourceAsStream("file.gif"));"

which does not allow to close the stream since it is created inside
the Image-constructor

Ben


>Don't forget to close the stream.
>
>"Philippe Marschall" <kustos@gmx.net> wrote in message
>news:cvkefp$tjh$1@www.eclipse.org...
>
>
>>Something like this should work
>>
>>InputStream is =
>>
>>
>>
>this.getCalss().getClassLoader().getResourceAsStream("com/images/myimage.gif
>");
>
>
>>Image icon = new Image(display, is);
>>
>>
>>Matt Rossner wrote:
>>
>>
>>>Hi,
>>>
>>>I am simply trying to get an icon in my title bar which I have done
>>>successfully, but it only works with an absolute path. I want to make it
>>>relative to the project, or better yet, relative to the JAR file that it
>>>will be exported to. Is there some way to reference the image by
>>>
>>>
>package,
>
>
>>>like com.images.myimage.gif?
>>>
>>>This is my current code.
>>>
>>> Image icon = new Image(display, "F:\\some_image.gif");
>>> parent.setImage(icon);
>>>
>>>Thanks for any advice on this
>>>
>>>Matt
>>>
>>>
>>>
>>>
>
>
>
>
Re: How to load an image from a JAR [message #451199 is a reply to message #451194] Thu, 24 February 2005 14:43 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: broussea.irisa.fr

I have exactly the same issue.

The following SWT code:
Menu menuNew = new Menu(shell , SWT.DROP_DOWN);
MenuItem itemNew = new MenuItem(menuFile , SWT.CASCADE);
itemNew.setText("&New");
itemNew.setMenu(menuNew);
Image img = new Image(display, "images/new.gif");
itemNew.setImage(img);

generates a org.eclipse.swt.SWTException: i/o error
(java.io.FileNotFoundException: images/xml.gif (No such file or directory))
while executing the jar.

I have tried your solution of using an InputStream but it crashes the
application all together.
Also, using the full path to this image generates the
FileNotFoundException again.

Any ideas what can I do?

Cheers.


Benjamin Pasero wrote:
> Uhm, I was thinking the new Image would manage to close the stream?
>
> Now looking at the API, on the one hand it is told "Application code
> is still responsible for closing the input stream" on the other hand
> an example is given with
>
> "This constructor may be used to load a resource as follows:
> new Image(device, clazz.getResourceAsStream("file.gif"));"
>
> which does not allow to close the stream since it is created inside
> the Image-constructor
>
> Ben
>
>
>> Don't forget to close the stream.
>>
>> "Philippe Marschall" <kustos@gmx.net> wrote in message
>> news:cvkefp$tjh$1@www.eclipse.org...
>>
>>
>>> Something like this should work
>>>
>>> InputStream is =
>>>
>>>
>>
>> this.getCalss().getClassLoader().getResourceAsStream("com/images/myimage.gif
>>
>> ");
>>
>>
>>> Image icon = new Image(display, is);
>>>
>>>
>>> Matt Rossner wrote:
>>>
>>>
>>>> Hi,
>>>>
>>>> I am simply trying to get an icon in my title bar which I have done
>>>> successfully, but it only works with an absolute path. I want to
>>>> make it
>>>> relative to the project, or better yet, relative to the JAR file
>>>> that it
>>>> will be exported to. Is there some way to reference the image by
>>>>
>>
>> package,
>>
>>
>>>> like com.images.myimage.gif?
>>>>
>>>> This is my current code.
>>>>
>>>> Image icon = new Image(display, "F:\\some_image.gif");
>>>> parent.setImage(icon);
>>>>
>>>> Thanks for any advice on this
>>>>
>>>> Matt
>>>>
>>>>
>>>>
>>
>>
>>
>>
>>
Re: How to load an image from a JAR [message #451204 is a reply to message #451199] Thu, 24 February 2005 15:03 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

For this to work it can't be in a jar. This expects only local files.

> Image img = new Image(display, "images/new.gif");

If you don't mind bring JFace in, you can do:

Image i =
ImageDescriptor.createFromURL(this.getClass().getResource("/dir/file.gif ");

Here "dir" directory must be at the root of some jar in your classpath.
The leading slash "/" is very important. It tells getResource() to look
for the file starting from the root of the jars. If you did not have the
leading slash, it would add your this.getClass()'s package name in front
(after converting "." or "/"). So for example if your class was
a.b.MyClass, then this.getClass().getResource("dir/file.gif") would
actually look in your jars for "/a/b/dir/file.gif".

Or if you don't want JFace, then you will need to do:

Image i = null;
InputStream is = this.getClass().getResourceAsStream("/dir/file.gif");
if (is != null) {
try {
i = new Image(display, is);
} finally {
try {
is.close();
} catch (IOException e) {
}
}
}

--
Thanks,
Rich Kulp
Re: How to load an image from a JAR [message #451289 is a reply to message #451204] Thu, 24 February 2005 16:44 Go to previous messageGo to next message
Matt Rossner is currently offline Matt RossnerFriend
Messages: 6
Registered: July 2009
Junior Member
Thanks for all the suggestions, I have what to try now

"Rich Kulp" <richkulp@us.NO_SPAM.ibm.com> wrote in message
news:cvkqca$qk3$1@www.eclipse.org...
> For this to work it can't be in a jar. This expects only local files.
>
> > Image img = new Image(display, "images/new.gif");
>
> If you don't mind bring JFace in, you can do:
>
> Image i =
>
ImageDescriptor.createFromURL(this.getClass().getResource("/dir/file.gif ");
>
> Here "dir" directory must be at the root of some jar in your classpath.
> The leading slash "/" is very important. It tells getResource() to look
> for the file starting from the root of the jars. If you did not have the
> leading slash, it would add your this.getClass()'s package name in front
> (after converting "." or "/"). So for example if your class was
> a.b.MyClass, then this.getClass().getResource("dir/file.gif") would
> actually look in your jars for "/a/b/dir/file.gif".
>
> Or if you don't want JFace, then you will need to do:
>
> Image i = null;
> InputStream is = this.getClass().getResourceAsStream("/dir/file.gif");
> if (is != null) {
> try {
> i = new Image(display, is);
> } finally {
> try {
> is.close();
> } catch (IOException e) {
> }
> }
> }
>
> --
> Thanks,
> Rich Kulp
Re: How to load an image from a JAR [message #451319 is a reply to message #451194] Fri, 25 February 2005 14:56 Go to previous message
Steve Northover is currently offline Steve NorthoverFriend
Messages: 1636
Registered: July 2009
Senior Member
https://bugs.eclipse.org/bugs/show_bug.cgi?id=86631

"Benjamin Pasero" <bpasero@rssowl.org> wrote in message
news:cvko7e$ere$1@www.eclipse.org...
> Uhm, I was thinking the new Image would manage to close the stream?
>
> Now looking at the API, on the one hand it is told "Application code
> is still responsible for closing the input stream" on the other hand
> an example is given with
>
> "This constructor may be used to load a resource as follows:
> new Image(device, clazz.getResourceAsStream("file.gif"));"
>
> which does not allow to close the stream since it is created inside
> the Image-constructor
>
> Ben
>
>
> >Don't forget to close the stream.
> >
> >"Philippe Marschall" <kustos@gmx.net> wrote in message
> >news:cvkefp$tjh$1@www.eclipse.org...
> >
> >
> >>Something like this should work
> >>
> >>InputStream is =
> >>
> >>
> >>
>
>this.getCalss().getClassLoader().getResourceAsStream("com/images/myimage.gi
f
> >");
> >
> >
> >>Image icon = new Image(display, is);
> >>
> >>
> >>Matt Rossner wrote:
> >>
> >>
> >>>Hi,
> >>>
> >>>I am simply trying to get an icon in my title bar which I have done
> >>>successfully, but it only works with an absolute path. I want to make
it
> >>>relative to the project, or better yet, relative to the JAR file that
it
> >>>will be exported to. Is there some way to reference the image by
> >>>
> >>>
> >package,
> >
> >
> >>>like com.images.myimage.gif?
> >>>
> >>>This is my current code.
> >>>
> >>> Image icon = new Image(display, "F:\\some_image.gif");
> >>> parent.setImage(icon);
> >>>
> >>>Thanks for any advice on this
> >>>
> >>>Matt
> >>>
> >>>
> >>>
> >>>
> >
> >
> >
> >
Previous Topic:How to get local file icons?
Next Topic:Browser (IE) not generating WindowClosing event?
Goto Forum:
  


Current Time: Sat Apr 20 03:01:48 GMT 2024

Powered by FUDForum. Page generated in 0.03800 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top