一、Rcp程序获取程序安装路径
- String path = null;
- Location location = Platform.getInstallLocation();
- if (location != null) {
- URL url = location.getURL();
- path = url.getPath();
- }
二、获取Plug-in中的资源的绝对路径
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import org.eclipse.core.runtime.FileLocator;
- import org.eclipse.core.runtime.Path;
- import org.eclipse.core.runtime.Platform;
- import org.osgi.framework.Bundle;
- public class TestGetFullPath {
- //filePath 为资源的相对路径
- public static String getFullPath(String pluginId, String filePath) {
- if (pluginId == null || filePath == null) {
- throw new IllegalArgumentException();
- }
- // if the bundle is not ready then there is no file
- Bundle bundle = Platform.getBundle(pluginId);
- if (!isReady(bundle)) {
- return null;
- }
- // look for the file (this will check both the plugin and fragment
- // folders
- URL fullPathString = find(bundle, filePath);
- if (fullPathString == null) {
- try {
- fullPathString = new URL(filePath);
- } catch (MalformedURLException e) {
- return null;
- }
- }
- if (fullPathString == null) {
- return null;
- }
- try {
- fullPathString = FileLocator.toFileURL(fullPathString);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return fullPathString.getPath();
- }
- public static boolean isReady(Bundle bundle) {
- return bundle != null && isReady(bundle.getState());
- }
- public static boolean isReady(int bundleState) {
- return (bundleState & (Bundle.RESOLVED | Bundle.STARTING
- | Bundle.ACTIVE | Bundle.STOPPING)) != 0;
- }
- public static URL find(Bundle bundle, String path) {
- if (bundle == null) {
- return null;
- }
- return FileLocator.find(bundle, new Path(path), null);
- }
- }