Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » How to access files within a jar ?
How to access files within a jar ? [message #334455] Tue, 10 February 2009 16:11 Go to next message
Mustafa is currently offline MustafaFriend
Messages: 13
Registered: July 2009
Junior Member
I have tried to access to a file within my plugin in Eclipse by using
String mainPath = getBundle().getLocation() + "fileName" and it works.

However when I deployed my product, I notice my getBundle().getLocation()
returns to a path of a jar file.

1. How can I access the file within the jar ? Or should I enable the
unpack option for the particular plug-in ?

2. How to get the absolute path of the file within plug-in ?
Re: How to access files within a jar ? [message #334458 is a reply to message #334455] Tue, 10 February 2009 16:39 Go to previous message
Walter Harley is currently offline Walter HarleyFriend
Messages: 847
Registered: July 2009
Senior Member
"Mustafa " <must_top@yahoo.com> wrote in message
news:125281cd2e6a841708c7d0f5cb2805db$1@www.eclipse.org...
>I have tried to access to a file within my plugin in Eclipse by using
>String mainPath = getBundle().getLocation() + "fileName" and it works.
>
> However when I deployed my product, I notice my getBundle().getLocation()
> returns to a path of a jar file.
> 1. How can I access the file within the jar ? Or should I enable the
> unpack option for the particular plug-in ?
>
> 2. How to get the absolute path of the file within plug-in ?

Correct, you should never assume at run-time that your bundle resources are
actual files.

The best way is probably to use ClassLoader.getResourceAsStream(), which
will find resources in any bundle that you have access to and will expose
them to you as an InputStream.

If you need actual files rather than an InputStream, that may force Eclipse
to extract them from the bundle and save them to a local file cache. For
that functionality you can use FileLocator.toFileURL(), like this:

URL installURL = plugin.getBundle().getEntry( path.toString() );
if(null == installURL)
return null; // File Not found

URL localURL = FileLocator.toFileURL( installURL );
return new java.io.File( localURL.getFile() );

FileLocator comes from org.eclipse.equinox.common.

But remember that this can be expensive in terms of time and disk space; if
you can leave the file where it is, packed up inside the bundle, and just
access its contents with InputStream, you'll be better off.
Previous Topic:Fragments And API Extensibility
Next Topic:Q: changing member sort fields
Goto Forum:
  


Current Time: Thu Dec 26 20:18:26 GMT 2024

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

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

Back to the top