Eclipse RCP 程序获取程序路径

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

view plain
  1. Stringpath=null;
  2. Locationlocation=Platform.getInstallLocation();
  3. if(location!=null){
  4. URLurl=location.getURL();
  5. path=url.getPath();
  6. }

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

view plain
  1. importjava.io.IOException;
  2. importjava.net.MalformedURLException;
  3. importjava.net.URL;
  4. importorg.eclipse.core.runtime.FileLocator;
  5. importorg.eclipse.core.runtime.Path;
  6. importorg.eclipse.core.runtime.Platform;
  7. importorg.osgi.framework.Bundle;
  8. publicclassTestGetFullPath{
  9. //filePath为资源的相对路径
  10. publicstaticStringgetFullPath(StringpluginId,StringfilePath){
  11. if(pluginId==null||filePath==null){
  12. thrownewIllegalArgumentException();
  13. }
  14. //ifthebundleisnotreadythenthereisnofile
  15. Bundlebundle=Platform.getBundle(pluginId);
  16. if(!isReady(bundle)){
  17. returnnull;
  18. }
  19. //lookforthefile(thiswillcheckboththepluginandfragment
  20. //folders
  21. URLfullPathString=find(bundle,filePath);
  22. if(fullPathString==null){
  23. try{
  24. fullPathString=newURL(filePath);
  25. }catch(MalformedURLExceptione){
  26. returnnull;
  27. }
  28. }
  29. if(fullPathString==null){
  30. returnnull;
  31. }
  32. try{
  33. fullPathString=FileLocator.toFileURL(fullPathString);
  34. }catch(IOExceptione){
  35. e.printStackTrace();
  36. }
  37. returnfullPathString.getPath();
  38. }
  39. publicstaticbooleanisReady(Bundlebundle){
  40. returnbundle!=null&&isReady(bundle.getState());
  41. }
  42. publicstaticbooleanisReady(intbundleState){
  43. return(bundleState&(Bundle.RESOLVED|Bundle.STARTING
  44. |Bundle.ACTIVE|Bundle.STOPPING))!=0;
  45. }
  46. publicstaticURLfind(Bundlebundle,Stringpath){
  47. if(bundle==null){
  48. returnnull;
  49. }
  50. returnFileLocator.find(bundle,newPath(path),null);
  51. }
  52. }

你可能感兴趣的:(Eclipse RCP 程序获取程序路径)