0

我的项目目录结构(在 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 包中的 OSGi 包中包含的 CSS 文件style.css中使用,MyProjectContainingCSSMyProjectContainingCSSStyleImpl.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
4

1 回答 1

0

要从您自己的包中获取文件,您可以执行以下操作:

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();
    }
于 2016-04-03T17:55:37.777 回答