eclipse rcp 开发如何得到当前工作空间的路径?

2024-12-27 01:21:55
推荐回答(2个)
回答1:

  一、Rcp程序获取程序安装路径

  [c-sharp] view plaincopy
  String path = null;
  Location location = Platform.getInstallLocation();
  if (location != null) {
  URL url = location.getURL();
  path = url.getPath();
  }

  
  二、获取Plug-in中的资源的绝对路径

  [c-sharp] view plaincopy
  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);
  }
  }

回答2:

.IWorkspace workspace = ResourcesPlugin.getWorkspace();
WorkspaceRoot root = workspace.getRoot();
这两句是得到eclipse插件运行时产生的工作空间的根,再进一步root.getProjects()可以得到运行时工作空间下建立的所有工程;

Platform.getInstanceLocation().getURL().getPath();这句代码也可以得到runtimeworkspace;