Home » Eclipse Projects » Standard Widget Toolkit (SWT) » How to load an image from a JAR
| | |
Re: How to load an image from a JAR [message #451194 is a reply to message #451188] |
Thu, 24 February 2005 14:26 |
Benjamin Pasero 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 |
Eclipse User |
|
|
|
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 |
Eclipse User |
|
|
|
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 #451319 is a reply to message #451194] |
Fri, 25 February 2005 14:56 |
Steve Northover 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
> >>>
> >>>
> >>>
> >>>
> >
> >
> >
> >
|
|
|
Goto Forum:
Current Time: Tue Sep 24 02:16:29 GMT 2024
Powered by FUDForum. Page generated in 0.04952 seconds
|