使用Java 8和JavaFX测试Apache Felix

3

我正在使用JavaFX 2.2和Java 8进行示例应用程序的开发。我创建了这个简单的Apache Felix激活器:

激活器:

import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class Activator implements BundleActivator
{

    Stage stage;

    @Override
    public void start(BundleContext context) throws Exception
    {

        Platform.runLater(new Runnable()
        {
            @Override
            public void run()
            {
                stage = new Stage();
                BorderPane pane = new BorderPane();
                Scene scene = new Scene(pane, 400, 200);
                pane.setCenter(new Label("This is a JavaFX Scene in a Stage"));
                stage.setScene(scene);
                stage.show();
            }
        });
        System.out.println("Main Module is loaded!");

    }

    @Override
    public void stop(BundleContext context) throws Exception
    {
        Platform.runLater(new Runnable()
        {
            @Override
            public void run()
            {
                stage.close();
            }
        });
        System.out.println("Main Module is unloaded!");
    }
}

POM

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>DX-57</groupId>
    <artifactId>DX-57_Main</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>bundle</packaging>

    <name>DX-57_Main OSGi Bundle</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.7</version>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Import-Package>*</Import-Package>
                        <Bundle-Activator>dx57.dx._main.Activator</Bundle-Activator>
                        <Export-Package>*</Export-Package>
                    </instructions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>build-for-felix</id>
            <dependencies>
                <dependency>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>org.apache.felix.main</artifactId>
                    <version>4.2.1</version>
                    <scope>provided</scope>
                </dependency>
                <!-- To include a shell:
                <dependency>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>org.apache.felix.gogo.shell</artifactId>
                    <version>0.10.0</version>
                </dependency>
                -->
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.7</version>
                        <executions>
                            <execution>
                                <id>compile</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <pathconvert property="plugins.jars" pathsep="${path.separator}">
                                            <path refid="maven.runtime.classpath"/>
                                            <map from="${project.build.directory}${file.separator}classes" to=""/>
                                        </pathconvert>
                                        <pathconvert pathsep=" " property="bundles">
                                            <path path="${plugins.jars}"/>
                                            <mapper>
                                                <chainedmapper>
                                                    <flattenmapper/>
                                                    <globmapper from="*" to="file:modules/*" casesensitive="no"/>
                                                </chainedmapper>
                                            </mapper>
                                        </pathconvert>
                                        <propertyfile file="${project.build.directory}/config.properties">
                                            <entry key="felix.auto.start" value="${bundles} file:modules/${project.build.finalName}.jar"/>
                                            <entry key="org.osgi.framework.bootdelegation" value="*"/>
                                        </propertyfile>
                                        <copy file="${maven.dependency.org.apache.felix.org.apache.felix.main.jar.path}" tofile="${project.build.directory}/felix.jar"/>
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>2.4</version>
                        <executions>
                            <execution>
                                <id>create-executable-jar</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                                <configuration>
                                    <descriptors>
                                        <descriptor>${basedir}/src/main/assembly/felix.xml</descriptor>
                                    </descriptors>
                                    <finalName>${project.build.finalName}</finalName>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>run-on-felix</id>
            <dependencies>
                <dependency>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>org.apache.felix.main</artifactId>
                    <version>4.2.1</version>
                    <scope>provided</scope>
                </dependency>
                <!-- org.apache.felix:org.apache.felix.gogo.shell:0.6.1 useless from Maven since stdin is swallowed -->
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.7</version>
                        <configuration>
                            <target>
                                <property name="vm.args" value=""/>
                                <pathconvert property="plugins.jars" pathsep="${path.separator}">
                                    <path refid="maven.runtime.classpath"/>
                                    <map from="${project.build.directory}${file.separator}classes" to=""/>
                                </pathconvert>
                                <makeurl property="urls" separator=" ">
                                    <path path="${plugins.jars}"/>
                                    <path location="${project.build.directory}/${project.build.finalName}.jar"/>
                                </makeurl>
                                <propertyfile file="${project.build.directory}/run.properties">
                                    <entry key="felix.auto.start" value="${urls}"/>
                                    <entry key="felix.auto.deploy.action" value="uninstall,install,update,start"/>
                                    <entry key="org.osgi.framework.storage" value="${project.build.directory}${file.separator}felix-cache"/>
                                    <entry key="org.osgi.framework.bootdelegation" value="*"/>
                                </propertyfile>
                                <makeurl property="run.properties.url" file="${project.build.directory}/run.properties"/>
                                <java fork="true" jar="${maven.dependency.org.apache.felix.org.apache.felix.main.jar.path}">
                                    <sysproperty key="felix.config.properties" value="${run.properties.url}"/>
                                    <jvmarg line="${vm.args}"/>
                                </java>
                            </target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

错误:

[rcbandit@Laptop felix-framework_JavaFX]$ /opt/jdk1.8.0/bin/java -jar bin/felix.jar
ERROR: Bundle DX-57.Main [1] Error starting file:/home/rcbandit/Desktop/test/felix-framework_JavaFX/bundle/DX-57_Main-1.0-SNAPSHOT.jar (org.osgi.framework.BundleException: Activator start error in bundle DX-57.Main [1].)
java.lang.ClassCastException: dx57.dx._main.Activator cannot be cast to org.osgi.framework.BundleActivator
    at org.apache.felix.framework.Felix.createBundleActivator(Felix.java:4336)
    at org.apache.felix.framework.Felix.activateBundle(Felix.java:2141)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)
    at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1291)
    at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:304)
    at java.lang.Thread.run(Thread.java:724)
____________________________
Welcome to Apache Felix Gogo

g! 

我已经将外部包添加到config.properties文件中:

org.osgi.framework.system.packages.extra=javafx.application, \
com.sun.browser.plugin, \
com.sun.deploy.uitoolkit.impl.fx, \
com.sun.deploy.uitoolkit.impl.fx.ui, \
com.sun.deploy.uitoolkit.impl.fx.ui.resources, \
com.sun.deploy.uitoolkit.impl.fx.ui.resources.image, \
com.sun.glass.events, \
com.sun.glass.ui, \
com.sun.glass.ui.delegate, \
com.sun.glass.ui.gtk, \
com.sun.glass.ui.mac, \
com.sun.glass.ui.win, \
com.sun.glass.ui.x11, \
com.sun.glass.utils, \
com.sun.javafx, \
com.sun.javafx.animation, \
com.sun.javafx.animation.transition, \
com.sun.javafx.applet, \
com.sun.javafx.application, \
com.sun.javafx.beans, \
com.sun.javafx.beans.annotations, \
com.sun.javafx.beans.event, \
com.sun.javafx.binding, \
com.sun.javafx.charts, \
com.sun.javafx.collections, \
com.sun.javafx.collections.annotations, \
com.sun.javafx.collections.transformation, \
com.sun.javafx.css, \
com.sun.javafx.css.converters, \
com.sun.javafx.css.parser, \
com.sun.javafx.cursor, \
com.sun.javafx.effect, \
com.sun.javafx.embed, \
com.sun.javafx.event, \
com.sun.javafx.font, \
com.sun.javafx.fxml, \
com.sun.javafx.fxml.builder, \
com.sun.javafx.fxml.expression, \
com.sun.javafx.geom, \
com.sun.javafx.geom.transform, \
com.sun.javafx.iio, \
com.sun.javafx.iio.bmp, \
com.sun.javafx.iio.common, \
com.sun.javafx.iio.gif, \
com.sun.javafx.iio.jpeg, \
com.sun.javafx.iio.png, \
com.sun.javafx.image, \
com.sun.javafx.image.impl, \
com.sun.javafx.jmx, \
com.sun.javafx.logging, \
com.sun.javafx.menu, \
com.sun.javafx.perf, \
com.sun.javafx.property, \
com.sun.javafx.property.adapter, \
com.sun.javafx.robot, \
com.sun.javafx.robot.impl, \
com.sun.javafx.runtime, \
com.sun.javafx.runtime.async, \
com.sun.javafx.runtime.eula, \
com.sun.javafx.scene, \
com.sun.javafx.scene.control, \
com.sun.javafx.scene.control.behavior, \
com.sun.javafx.scene.control.skin, \
com.sun.javafx.scene.control.skin.caspian, \
com.sun.javafx.scene.control.skin.resources, \
com.sun.javafx.scene.input, \
com.sun.javafx.scene.layout.region, \
com.sun.javafx.scene.paint, \
com.sun.javafx.scene.shape, \
com.sun.javafx.scene.text, \
com.sun.javafx.scene.transform, \
com.sun.javafx.scene.traversal, \
com.sun.javafx.scene.web, \
com.sun.javafx.scene.web.behavior, \
com.sun.javafx.scene.web.skin, \
com.sun.javafx.sg, \
com.sun.javafx.sg.prism, \
com.sun.javafx.stage, \
com.sun.javafx.tk, \
com.sun.javafx.tk.desktop, \
com.sun.javafx.tk.quantum, \
com.sun.javafx.util, \
com.sun.media.jfxmedia, \
com.sun.media.jfxmedia.control, \
com.sun.media.jfxmedia.effects, \
com.sun.media.jfxmedia.events, \
com.sun.media.jfxmedia.locator, \
com.sun.media.jfxmedia.logging, \
com.sun.media.jfxmedia.track, \
com.sun.media.jfxmediaimpl, \
com.sun.media.jfxmediaimpl.platform, \
com.sun.media.jfxmediaimpl.platform.gstreamer, \
com.sun.media.jfxmediaimpl.platform.java, \
com.sun.media.jfxmediaimpl.platform.osx, \
com.sun.openpisces, \
com.sun.prism, \
com.sun.prism.camera, \
com.sun.prism.d3d, \
com.sun.prism.d3d.hlsl, \
com.sun.prism.image, \
com.sun.prism.impl, \
com.sun.prism.impl.packrect, \
com.sun.prism.impl.paint, \
com.sun.prism.impl.ps, \
com.sun.prism.impl.shape, \
com.sun.prism.j2d, \
com.sun.prism.j2d.paint, \
com.sun.prism.paint, \
com.sun.prism.ps, \
com.sun.prism.render, \
com.sun.prism.shader, \
com.sun.prism.shape, \
com.sun.prism.tkal, \
com.sun.prism.util.tess, \
com.sun.prism.util.tess.impl.tess, \
com.sun.scenario, \
com.sun.scenario.animation, \
com.sun.scenario.animation.shared, \
com.sun.scenario.effect, \
com.sun.scenario.effect.impl, \
com.sun.scenario.effect.impl.hw, \
com.sun.scenario.effect.impl.hw.d3d, \
com.sun.scenario.effect.impl.hw.d3d.hlsl, \
com.sun.scenario.effect.impl.prism, \
com.sun.scenario.effect.impl.prism.ps, \
com.sun.scenario.effect.impl.prism.sw, \
com.sun.scenario.effect.impl.state, \
com.sun.scenario.effect.impl.sw, \
com.sun.scenario.effect.impl.sw.java, \
com.sun.scenario.effect.impl.sw.sse, \
com.sun.scenario.effect.light, \
com.sun.t2k, \
com.sun.webpane.perf, \
com.sun.webpane.platform, \
com.sun.webpane.platform.event, \
com.sun.webpane.platform.graphics, \
com.sun.webpane.sg, \
com.sun.webpane.sg.prism, \
com.sun.webpane.sg.prism.resources, \
com.sun.webpane.sg.prism.theme, \
com.sun.webpane.sg.theme, \
com.sun.webpane.webkit, \
com.sun.webpane.webkit.dom, \
com.sun.webpane.webkit.network, \
com.sun.webpane.webkit.network.about, \
com.sun.webpane.webkit.network.data, \
com.sun.webpane.webkit.unicode, \
javafx.animation, \
javafx.beans, \
javafx.beans.binding, \
javafx.beans.property, \
javafx.beans.property.adapter, \
javafx.beans.value, \
javafx.collections, \
javafx.concurrent, \
javafx.embed.swing, \
javafx.embed.swt, \
javafx.event, \
javafx.fxml, \
javafx.geometry, \
javafx.scene, \
javafx.scene.canvas, \
javafx.scene.chart, \
javafx.scene.control, \
javafx.scene.control.cell, \
javafx.scene.effect, \
javafx.scene.image, \
javafx.scene.input, \
javafx.scene.layout, \
javafx.scene.media, \
javafx.scene.paint, \
javafx.scene.shape, \
javafx.scene.text, \
javafx.scene.transform, \
javafx.scene.web, \
javafx.stage, \
javafx.util, \
javafx.util.converter, \
org.osgi.framework.wiring, \
netscape.javascript

你知道如何解决这个问题吗?

更新

移除 <Export-Package>*</Export-Package> 后,我收到了以下错误:

[rcbandit@Laptop felix-framework_JavaFX]$ /opt/jdk1.8.0/bin/java -jar bin/felix.jar
ERROR: Bundle DX-57.Main [1] Error starting file:/home/rcbandit/Desktop/test/felix-framework_JavaFX/bundle/DX-57_Main-1.0-SNAPSHOT.jar (org.osgi.framework.BundleException: Activator start error in bundle DX-57.Main [1].)
java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:201)
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:196)
    at javafx.application.Platform.runLater(Platform.java:52)
    at dx57.dx._main.Activator.start(Activator.java:20)
    at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:645)
    at org.apache.felix.framework.Felix.activateBundle(Felix.java:2146)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2064)
    at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1291)
    at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:304)
    at java.lang.Thread.run(Thread.java:724)
____________________________
Welcome to Apache Felix Gogo

g! 

1
“使用JavaFX 2.2和Java 8”,我猜你的意思是“使用JavaFX 8和Java SE 8”? - Puce
2个回答

10

JavaFX应用程序依赖于来自JavaFX API的软件包,例如javafx.application等多个软件包。由于看起来您已经使用Maven Bundle插件构建了您的捆绑包,因此您的捆绑包已经声明了这些依赖项。这是一件好事。

在Java 8中,javafx.*软件包由基本JRE提供。但是,OSGi并没有自动导出JRE中的每个软件包,仅因为所有JRE都有一堆非标准的软件包(例如com.sun.*等),正常的应用程序代码不应该访问它们。因此,OSGi仅提供与您正在使用的Java版本的相关JCP规范定义的软件包。例如,javax.swingorg.w3c.dom等软件包。

由于JavaFX不是标准化的,因此没有针对JavaFX的JCP规范,并且OSGi不导出javafx.*软件包。但是,您可以在启动OSGi时通过设置以下配置属性来配置OSGi为您执行此操作:

org.osgi.framework.system.packages.extra=javafx.application,...

NB 我已经展示了如何将javafx.application软件包添加到您的运行时。您可能需要添加更多,即来自JavaFX API的所有软件包。我不够熟悉JavaFX以列出这些软件包,但是您应该很容易就能找到它们。


我看了你的更新。这没有意义。你试图导入一个名为"osgi.wiring.package"的包??根本没有这样的包。你是显式地添加了这个依赖吗?请展示更多关于你所做的事情的信息。 - Neil Bartlett
@PeterPenzov 你在看这些回复吗?你添加了一个无效的依赖项。请展示该捆绑包的pom文件。 - Neil Bartlett
@PeterPenzov 看起来你粘贴错了。DX-57.testr 组合包失败了。 - Neil Bartlett
@PeterPenzov 我认为那不是一个好主意...现在答案与问题无关了。不过没关系。最新的问题是你说了<Export-Package> * </Export-Package>。请删除这一行。 - Neil Bartlett
我在删除<Export-Package>*</Export-Package>后再次更新了帖子。 - Peter Penzov
@PeterPenzov,这不是OSGi的问题,而是JavaFX的问题。我想你需要找出如何正确初始化JavaFX运行时。请停止在此stackoverflow页面上多次提问。只需标记问题已回答,并在新问题中发布您的最新问题即可。 - Neil Bartlett

6
我最近发布了Drombler FX的第一个早期访问版本,这是一个基于OSGi和Maven(以POM为先)的JavaFX模块化Rich Client平台,默认使用Apache Felix。您可以在这里阅读更多信息: http://puces-blog.blogspot.ch/2012/12/drombler-fx-building-modular-javafx.html. 开始使用请参考文档:http://wiki.drombler.org/GettingStarted。我最近尝试使用Java SE 8的预发布版运行它,并且到目前为止它可以工作,虽然我还没有指定Java SE 8的系统包,所以我不确定是否一切正常/您是否可以访问所有内容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接