How to get the Eclipse installation directory through programming in swt/java. I actually want to get the plugins directory of the eclipse.
2 Answers
Update (January 2012)
James Moore mentions in the comments that the FAQ and API are quite old.
FileLocator.resolve(URL)
is now preferred to the deprecated Platform.resolve()
.
As this example shows, you need to pass the actual resource (here a bundle), not the name of the resource, in order to resolve it:
private static URI locateFile(String bundle, String fullPath) {
try {
URL url = FileLocator.find(Platform.getBundle(bundle), new Path(fullPath), null);
if(url != null)
return FileLocator.resolve(url).toURI();
} catch (Exception e) {}
return null;
}
}
See also "How to refer a file from jar file in eclipse plugin" for more.
Original answer (January 2011)
Maybe the FAQ "How do I find out the install location of a plug-in?" can help here:
You should generally avoid making assumptions about the location of a plug-in at runtime.
To find resources, such as images, that are stored in your plug-in’s install directory, you can use URLs provided by thePlatform
class. These URLs use a special Eclipse Platform protocol, but if you are using them only to read files, it does not matter.In Eclipse 3.1 and earlier, the following snippet opens an input stream on a file called
sample.gif
located in a subdirectory, calledicons
, of a plug-in’s install directory:
Bundle bundle = Platform.getBundle(yourPluginId);
Path path = new Path("icons/sample.gif");
URL fileURL = Platform.find(bundle, path);
InputStream in = fileURL.openStream();
If you need to know the file system location of a plug-in, you need to use
Platform.resolve(URL)
. This method converts a platform URL to a standard URL protocol, such as HyperText Transfer Protocol (HTTP), or file.
Note that the Eclipse Platform does not specify that plug-ins must exist in the local file system, so you cannot rely on this method’s returning a file system URL under all circumstances in the future.In Eclipse 3.2, the preferred method seems to be:
Bundle bundle = Platform.getBundle(yourPluginId);
Path path = new Path("icons/sample.gif");
URL fileURL = FileLocator.find(bundle, path, null);
InputStream in = fileURL.openStream();
-
Thanks for the above but I've a doubt.Consider I have an plug-in project named com.abhishek.html.test and inside it I've a folder named html carrying few html files. Now I want to read those html in plug-in, then which approach I should follow. Locally it works but how a plug-in will detect the folder named html ? – Abhishek Choudhary Jan 18 '11 at 09:28
-
@Abhishek: I am not sure about the "locally it works". As opposed to what configuration exactly? Because your program should be able to detect the `html` folder in the plugin by getting the bundle and using `FileLocator`. It should return null (or throw an exception, I forgot which) if `html` don't exist within that plugin. – VonC Jan 18 '11 at 12:01
-
1That FAQ entry is horrible. 3.[1,2] are ancient - why not talk about recent releases? It recommends using Platform.resolve, but Platform.resolve is marked "Deprecated. use FileLocator.resolve(URL) instead" And why in the world is a FAQ saying something like "the preferred method seems to be"? Why qualify with such wishy-washy language? – James Moore Feb 01 '12 at 20:39
-
1@JamesMoore: good point. I have updated the answer in order to reflect the right API and added references to examples. – VonC Feb 01 '12 at 22:19
Use the below code to get the plugin path :
this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();

- 83
- 6
-
-
this - user defined class object In which the above code is used.for ex. if you have a class Myclass.java then the above code can be used like "Myclass.class.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();" – V_Dev Jun 28 '16 at 05:30