Java 如何在一个OSGi包中使用**CSS**资源文件,而不是在另一个OSGi包中使用
我的项目目录结构(在Eclipse中): 我想在类Java 如何在一个OSGi包中使用**CSS**资源文件,而不是在另一个OSGi包中使用,java,css,resources,osgi,bndtools,Java,Css,Resources,Osgi,Bndtools,我的项目目录结构(在Eclipse中): 我想在类StyleImpl.java中的另一个OSGi包MyProjectContainingCSS中包含的CSS文件style.CSS中使用 比如: public class StyleImpl { public static void main(String[] args) { css = this.getClass().getResource("/com/me/myapp/style.css").toExternalForm
StyleImpl.java
中的另一个OSGi包MyProjectContainingCSS
中包含的CSS文件style.CSS中使用
比如:
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();
}
Bundle Bundle=FrameworkUtil.getBundle(this.getClass());
枚举资源=bundle.getResources(“/com/me/myapp/style.css”);
if(资源!=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();
}
Bundle Bundle=FrameworkUtil.getBundle(this.getClass());
Bundle[]bArray=Bundle.getBundleContext().getBundles();
Bundle cssBundle=null;
用于(捆绑包b:bArray){
if(b.getSymbolicName().equals(“com.me.myapp”)){
cssBundle=b;
打破
}
}
枚举资源=cssBundle.getResources(“/com/me/myapp/style.css”);
if(资源!=null){
URL myCSS=resources.nextElement();
}
在CSS捆绑包中创建一个类,将InputStream公开到CSS文件,并将该类导入另一个捆绑包。一个捆绑包如何知道在另一个捆绑包中会找到具有特定名称的特定资源?这种假设意味着您违反了模块封装。试着找到一个不需要对其他模块进行假设的解决方案。为什么不描述一下您试图解决的真正问题呢?非常感谢@Marcos Zolnowski,我已经将您的代码放在我的一个名为MainScreen的类中,但我无法使用方法FrameworkUtil.getBundle
。它获得一个错误突出显示,警告类型FrameworkUtil的方法getBundle(Class)未定义。FrameworkUtil是org.osgi.framework包中的一个类。同样的包裹。你使用什么版本的OSGi?我对OSGi完全陌生。我不知道如何检查OSGi版本。我在EclipseMars.2版本(4.5.2)中使用BND工具没有问题,因为系统没有告诉您。你能告诉我org.osgi.framework
package的版本吗?bndtools的一些版本存在泛型问题。但我现在不确定。您可以尝试强制转换:FrameworkUtil.getBundle((Class)myClass)
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();
}