我的元素目录结构(在 Eclipse 中):
MyProjectContainingCSS/
src/ --> "source directory" on Eclipse's classpath/buildpath
com.me.myapp
style.css
MyProjectInheritingCSS/
src/ --> "source directory" on Eclipse's classpath/buildpath
com.me.myapp
StyleImpl.java
我想在另一个 OSGi 包 MyProjectContainingCSS
中的 OSGi 包 MyProjectContainingCSS
中包含的 CSS 文件 style.css 中使用类 StyleImpl.java
,
类似于:
public class StyleImpl {
public static void main(String[] args) {
css = this.getClass().getResource("/com/me/myapp/style.css").toExternalForm();
scene.getStylesheets().add(css);
}
}
如何在一个 OSGi 包中使用来自另一个 OSGi 包的 CSS 资源文件?
提前谢谢大家。
更新
bnd.bnd 文件
Bundle-Version: 0.0.0.${tstamp}
-buildpath: \
../cnf/plugins/org.apache.felix.dependencymanager.annotation-3.2.0.jar;version=file,\
org.apache.felix.dependencymanager,\
osgi.core,\
launcher;version=latest,\
libs/commons-io-2.4.jar;version=file
Private-Package: ui.impl
Export-Package: ui
Import-Package: *
运行描述符
-runfw: org.apache.felix.framework;version='[4,5)'
-runee: JavaSE-1.8
-runsystemcapabilities: ${native_capability}
-resolve.effective: active;skip:="osgi.service"
-runbundles: \
org.apache.felix.dependencymanager,\
org.apache.felix.dependencymanager.runtime,\
org.apache.felix.dependencymanager.shell,\
org.apache.felix.metatype,\
org.apache.felix.eventadmin,\
org.apache.felix.configadmin,\
org.apache.felix.log,\
org.apache.felix.gogo.command,\
org.apache.felix.gogo.runtime,\
org.apache.felix.gogo.shell,\
launcher;version=latest,\
ui;version=latest,\
mainscreen;version=latest
-runsystempackages: javafx.application,javafx.scene,javafx.stage,javafx.scene.layout,javafx.event,javafx.collections,javafx.scene.control,javafx.scene.paint,javafx.scene.shape
最佳答案
要从您自己的包中获取文件,您可以这样做:
Bundle bundle = FrameworkUtil.getBundle(this.getClass());
Enumeration<URL> resources = bundle.getResources("/com/me/myapp/style.css");
if (resources != null) {
URL myCSS = resources.nextElement();
}
如果您可以找到其他 OSGi 包对象,您也可以这样做。我会尝试这样的事情:
Bundle bundle = FrameworkUtil.getBundle(this.getClass());
Bundle[] bArray = bundle.getBundleContext().getBundles();
Bundle cssBundle = null;
for (Bundle b : bArray){
if(b.getSymbolicName().equals("com.me.myapp")) {
cssBundle = b;
break;
}
}
Enumeration<URL> resources = cssBundle.getResources("/com/me/myapp/style.css");
if (resources != null) {
URL myCSS = resources.nextElement();
}
关于java - 我怎样才能在一个 OSGi 包中使用来自另一个 OSGi 包的 **CSS** 资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36377502/