Eclipse RCP 程序获取程序路径

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

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

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

  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. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值