Home > Blockchain >  Recommended way to get eclipse workspace path for Eclipse RCP with target 2022-09
Recommended way to get eclipse workspace path for Eclipse RCP with target 2022-09

Time:11-23

We are developing an Eclipse RCP application, and up to target version 2022-03, we always used this to get the current workspace path:

ResourcesPlugin.getWorkspace().getRoot().getLocation().toString()

But: with Eclipse target version 2022-09, we get an IllegalStateException, if we run the product outside of eclipse, i.e. the exported product:

java.lang.IllegalStateException: Workspace is already closed or not ready yet. Consider tracking the org.eclipse.core.resources.IWorkspace service (using your favorite technique, e.g. Declarative Services, ServiceTracker, Blueprint, ...) instead of calling the static method here to prevent such issues!
at org.eclipse.core.resources.ResourcesPlugin.getWorkspace(ResourcesPlugin.java:502)

I mean, the exception does propose some alternatives. But: is it really not possible anymore to use ResourcesPlugin.getWorkspace()? This was quite convenient. Is there any equally simple solution?

CodePudding user response:

The simplest way of getting the workspace location is:

URL location = Platform.getInstanceLocation().getURL();

Platform is org.eclipse.core.runtime.Platform.

This does not rely on the resources plug-in at all (so it even works in a e4 RCP without the resources plug-in).

  • Related