Skip to main content



      Home
Home » Eclipse Projects » Eclipse Platform » Path to resources
Path to resources [message #286226] Sun, 05 June 2005 14:15 Go to next message
Eclipse UserFriend
Originally posted by: thomas.larsson.xxx.xxx

Hello,

I don't know how to reference images (resources in general) in a situation
described below. Can someone please help me with this?

My simple plugin project has the folder structure shown here.

workspace/
|
myplugin/
| src/
| org/
| myorg/
| mypackage/
| gui.java (etc)
| icons/
sample.gif (etc)

In plugin.xml, under Run-time information. I have exported
org.myorg.mypackage.jar as "code and resources" and added the folders
/src and /icons as library content. The "Export the entiry library" is
chosen.

My build.properties has two values:
---
bin.includes: plugin.xml, \
org.myorg.mypackage.jar,\
icons/
source.org.myorg.mypackage.jar = icons/,\
src/
---

Now in gui.java I have the following lines:
---
Display display = Display.getCurrent();
shell = new Shell(display);
shell.setSize(800, 600);
Image fileImage = ImageDescriptor.createFromURL(getClass().
getResource("icons/sample.gif")).createImage();
---

I've exported the plugin as a zip file and deployed it and everything
works except that I don't know how to reference resources below the folder
of the calling file. This is as it should be since the path
icons/sample.gif is relative to the calling files location. So how can I
do it?

I've tried an alternative that also didnt work:

Image fileImage =
ImageDescriptor.createFromURL(mypackagePlugin.getDefault().g etClass().getResource( "icons/sample.gif")).createImage();`

Can anyone give me a solution? Im sure this is simple since there is not
any examples of it on the web.

Best Regards
-Thomas
Re: Path to resources [message #286234 is a reply to message #286226] Sun, 05 June 2005 18:17 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

your icons need to be in your jar, in fact using the
getclass().getresource(("icons...) means the icons directory needs to be
under the mypackage directory. In other words it must look like this:

..
>
> workspace/
> |
> myplugin/
> | src/
> org/
> myorg/
> mypackage/
> gui.java (etc)
> icons/
> sample.gif (etc)
>


The reason for this is because getClass().getResource() only looks in
the classpath of the loader that loaded the class gui. Since icons was
not built in the jar, it wasn't on the class path.

The second important point is why must icons be under mypackage. If must
be because when you say getClass().getResource("icons/..."), the
getResource method sees that the string DOES NOT START with a "/", so
it will assume the resource is relative to the class. Therefor the icons
directory must be under the mypackage directory.

--
Thanks,
Rich Kulp
Re: Path to resources [message #286248 is a reply to message #286234] Mon, 06 June 2005 04:22 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: thomas.larsson.xxx.xxx

Thanks Richard.

Your suggestion works perfectly, BUT ( :) ) then I wonder, is that
location considered as a best practices location for resources? Inside the
jar-file i mean? Is there no way to access resources that lie outside the
jars?
I had an impression that the recommended location for resources such as
images was in separate folders diverging from the plugin-root folder, i.e

eclipse/
plugins/
my.myorg.mypackage/
icons/
resources/
plugin.xml
somejar.jar

But perhaps I have the wrong idea?

Best Regards
-Thomas
Re: Path to resources [message #286258 is a reply to message #286248] Mon, 06 June 2005 09:35 Go to previous message
Eclipse UserFriend
Originally posted by: richkulp.us.NO_SPAM.ibm.com

You only put resources outside the jar for resources that need to be
accessed from the plugin.xml (e.g. icon="..." attributes). All other
resources that are to be accessed programatically are best to be within
the jar itself and referenced that way.


--
Thanks,
Rich Kulp
Previous Topic:Deleting projects
Next Topic:org.eclipse.swt.win64?
Goto Forum:
  


Current Time: Thu Apr 24 00:17:13 EDT 2025

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

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

Back to the top