如何在执行java.io.file或FileInputStream时引用OSGi包中包含的文件
我正在使用AquetBND工具集创建一个OSGi包,并打包了一些相关的“资源”文件。这包括我创建的资源目录中的*.css文件和*.xsd文件 我已将以下内容包括在bundle.bnd文件中:如何在执行java.io.file或FileInputStream时引用OSGi包中包含的文件,java,classpath,osgi,bundle,Java,Classpath,Osgi,Bundle,我正在使用AquetBND工具集创建一个OSGi包,并打包了一些相关的“资源”文件。这包括我创建的资源目录中的*.css文件和*.xsd文件 我已将以下内容包括在bundle.bnd文件中: Include-Resource: resources/=resources/ 当我进行构建时,生成的*.jar文件在jar包文件的顶部目录的resources目录中有*.css和*.xsd文件 但是,在实际代码中,我很难将其作为类路径的一部分: 我尝试了以下方法: new File("resource
Include-Resource: resources/=resources/
当我进行构建时,生成的*.jar文件在jar包文件的顶部目录的resources目录中有*.css和*.xsd文件
但是,在实际代码中,我很难将其作为类路径的一部分:
我尝试了以下方法:
new File("resources/example.css");
URL cssFile = this.getClass().getResource("/resources/example.css");
我也尝试过:
URL cssFile = this.getClass().getResource("resources/example.css");
try
{
file = new File(cssFile.toURI()));
}
catch(Exception e)
{
e.printStackTrace();
}
我要么收到NullPointException错误,要么找不到文件IOException错误(取决于我使用的是哪一个)。在调试配置模式下的EclipseEquinox和ApacheFelix(我们在部署中使用的)中运行时,我都会遇到这个错误。注意,我试图在BundleActivator之外的Java类中实现这一点
我是否需要始终参考BundleActivator的上下文,例如
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
@Override
public void start(BundleContext context) throws Exception
{
/* Service to host the Bundle interface. */
ServletContainerService service = new ServletContainerService();
service.addServlet(new ServletContainer(new AxisServlet(), true));
this.serverReg = context.registerService(ServletContainerService.class.getName(), service, null);
cssFile = new File(context.getClass.getResource("resource/example.css"));
}
我认为上面的方法会起作用,但这意味着我必须通过cssFile引用,它看起来并不优雅
是否有任何方法可以引用bundle/.jar文件中任何给定Java类中bundle jar文件中包含的“resources”目录的路径?如果它涉及BundleContext,有没有任何方法可以在任何Java类中引用它
任何帮助都将不胜感激
我已经看过了,但是看起来你需要文本 我可能已经找到了一个可能的解决方案: 看起来Vogella对此有一些示例代码:
URL url;
try {
url = new URL("platform:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt");
InputStream inputStream = url.openConnection().getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
如果不是插件,或者我使用的是不同的OSGi环境,比如EquinoxEclipse和ApacheFelix,有人知道这条路径是否相同吗?
e、 g.url=newurl(“平台:/plugin/de.vogella.rcp.plugin.filereader/files/test.txt”)代码>具有一个返回Url的方法,并记录为:
返回此捆绑包中指定路径处的项的URL。此捆绑包的类装入器不用于搜索条目。仅在该捆绑包的内容中搜索条目。
指定的路径始终相对于此束的根,并且可以以“/”开头。路径值“/”表示此捆绑包的根。如果使用Eclipse/Equinox进行构建,则可以通过调用BundleActivator中包含的BundleContext从外部获取捆绑包的位置,方法如下:
Bundle yourBundle = Platform.getBundle("bundleSymbolicName");
Path relativePathToBundle = new Path("/relativePath");
FileLocator.openStream(yourBundle, relativePathToBundle, false);
平台和FileLocator类来自org.eclipse.core.runtime插件,因此,如果您依赖于该插件,则上述类应允许您访问资源。我认为答案应该如下所示:
new File("resources/example.css");
URL cssFile = this.getClass().getResource("/resources/example.css");
即,确保路径中有一条前导斜线。它没有前导斜杠,这会使它与调用的类“相对”
或者,应采用以下方法:
this.getClass().getClassLoader().getResource("resources/example.css");
在BundleActivator的上下文之外是否还有其他方法可以访问它?我猜您指的是这样做:public void start(BundleContext){Bundle Bundle=context.getBundle();InputStream in=Bundle.getEntry(“/resource/example.css”).openStream();}。然而,我正在寻找一个解决方案(如果有)来访问BundleActivator之外的它。我看了一下,但仍然感到困惑。您是否找到了从Bundle id获取Bundle对象的方法?我可以获取条目,但问题是getEntry()只提供相对路径,而不提供完整(根)路径捆绑包的路径:。我试图避免使用org.eclipse.core.runtime插件。对于我们的部署,我们决定使用ApacheFelix(除非有合并此插件的方法)。如果我在Eclipse运行时环境下运行它,上面的方法会起作用。我选择了此解决方案,但在尝试导入所有目录的值时遇到问题,例如,我使用BluePrint CSS,并且在.getClass().getClassLoader().getResource(“resources/CSS”)方面遇到问题,因为不仅有许多文件,而且还有子目录。我最终将文件“复制”到一个临时位置,但这会导致一些性能问题。