Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get resources path from an Eclipse plugin

I am developing an Eclipse plugin using Eclipse Plugin-in-project where it will add a menu item in the toolbar.

My plugin project is depending on one file which is located in the same plugin project I want the path of that file.

Below is the sample code I have used to get the path:

Bundle bundle = Platform.getBundle("com.feat.eclipse.plugin");
URL fileURL = bundle.getEntry("webspy/lib/file.txt");
File file = null;
String path=null;
try {
    file = new File(FileLocator.resolve(fileURL).toURI());
    path = file.getAbsolutePath();
} catch (URISyntaxException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
}

While running from Eclipse Run as > Eclipse Application it is giving me the correct path. But when I export my plugin as jar and add it in my Eclipse plugins folder its not giving me the correct path.

Please help me to resolve this!

like image 405
user3355101 Avatar asked Jan 21 '16 12:01

user3355101


People also ask

Where does Eclipse install plugins?

As far as I know, Eclipse stores its plugins in its installation directory ( eclipse ). They might reside in eclipse/plugins or eclipse/dropins . You can copy the whole eclipse directory from your old box.


1 Answers

Use:

URL url = FileLocator.find(bundle, new Path("webspy/lib/file.txt"), null);

url = FileLocator.toFileURL(url);

File file = URIUtil.toFile(URIUtil.toURI(url));

When your files are packed in a jar FileLocator.toFileURL will copy them to a temporary location so that you can access them using File.

like image 184
greg-449 Avatar answered Oct 19 '22 15:10

greg-449