如何在eclispe rcp中从bundle中加载文件

时间:2018-06-11 15:10:50

标签: java eclipse-rcp

我正在eclipse包中加载一个属性文件,但我的代码是使用eclipse包中的InternalPlatform,这不是最好的方法,因为它是来自eclipse的受限制的包。如何从类路径加载此文件。

public Properties getProperties(String resourceName){
            Bundle bundle = InternalPlatform.getDefault().getBundle("sample-bundle");
            try {
                return PropertyFactory.getProperties(new URL("platform:/plugin/" + bundle.getSymbolicName() + "/" + resourceName));
            } catch (Exception e) {
                logger.error("Unable to load aegis config {}", e.getMessage(), e);
                return null;
            }



private static Properties getProperties(URL url) throws IOException {   
    Properties properties = new Properties();
    logger.info("Loading Properties  from {}", url);
    properties.load(url.openStream());
    return properties;
}

1 个答案:

答案 0 :(得分:0)

Platform类提供了一个官方方法来获取插件的包,其中包含ID:

Bundle bundle = Platform.getBundle("sample-bundle");

然后,您可以使用FileLocator类在捆绑包中查找资源:

IPath path = new Path("relative path of resource in bundle");

URL url = FileLocator.find(bundle, path, null);

注意:Platform插件中org.eclipse.core.runtime.Platformorg.eclipse.core.runtimePathorg.eclipse.core.runtime.Path