一、Rcp程序获取程序安装路径
- Stringpath=null;
- Locationlocation=Platform.getInstallLocation();
- if(location!=null){
- URLurl=location.getURL();
- path=url.getPath();
- }
二、获取Plug-in中的资源的绝对路径
- importjava.io.IOException;
- importjava.net.MalformedURLException;
- importjava.net.URL;
- importorg.eclipse.core.runtime.FileLocator;
- importorg.eclipse.core.runtime.Path;
- importorg.eclipse.core.runtime.Platform;
- importorg.osgi.framework.Bundle;
- publicclassTestGetFullPath{
- //filePath为资源的相对路径
- publicstaticStringgetFullPath(StringpluginId,StringfilePath){
- if(pluginId==null||filePath==null){
- thrownewIllegalArgumentException();
- }
- //ifthebundleisnotreadythenthereisnofile
- Bundlebundle=Platform.getBundle(pluginId);
- if(!isReady(bundle)){
- returnnull;
- }
- //lookforthefile(thiswillcheckboththepluginandfragment
- //folders
- URLfullPathString=find(bundle,filePath);
- if(fullPathString==null){
- try{
- fullPathString=newURL(filePath);
- }catch(MalformedURLExceptione){
- returnnull;
- }
- }
- if(fullPathString==null){
- returnnull;
- }
- try{
- fullPathString=FileLocator.toFileURL(fullPathString);
- }catch(IOExceptione){
- e.printStackTrace();
- }
- returnfullPathString.getPath();
- }
- publicstaticbooleanisReady(Bundlebundle){
- returnbundle!=null&&isReady(bundle.getState());
- }
- publicstaticbooleanisReady(intbundleState){
- return(bundleState&(Bundle.RESOLVED|Bundle.STARTING
- |Bundle.ACTIVE|Bundle.STOPPING))!=0;
- }
- publicstaticURLfind(Bundlebundle,Stringpath){
- if(bundle==null){
- returnnull;
- }
- returnFileLocator.find(bundle,newPath(path),null);
- }
- }