[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform] Re: How to access files within a jar ?

"Mustafa " <must_top@xxxxxxxxx> wrote in message 
news:125281cd2e6a841708c7d0f5cb2805db$1@xxxxxxxxxxxxxxxxxx
>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.