使用 Java 8 和 JavaFX 测试 Apache Felix

Posted

技术标签:

【中文标题】使用 Java 8 和 JavaFX 测试 Apache Felix【英文标题】:Test Apache Felix with Java 8 and JavaFX 【发布时间】:2013-05-21 03:28:16 【问题描述】:

我正在研究 JavaFX 2.2 和 Java 8 的示例。我创建了这个简单的 Apache Felix Activator:

激活器:

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.separatorclasses" 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.separatorclasses" 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.separatorfelix-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

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

更新

删除&lt;Export-Package&gt;*&lt;/Export-Package&gt; 后出现此错误:

[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! 

【问题讨论】:

“使用 JavaFX 2.2 和 Java 8”,我猜你的意思是“使用 JavaFX 8 和 Java SE 8”? 【参考方案1】:

JavaFX 应用程序依赖于来自 JavaFX API 的包,例如 javafx.application 和其他几个。因为看起来你已经使用 Maven Bundle Plugin 构建了你的包,所以你的包已经声明了这些依赖项。这是一件好事。

在 Java 8 中,javafx.* 包由基本 JRE 提供。然而,OSGi 不会自动从 JRE 导出 每个 包,这仅仅是因为所有 JRE 都有一堆普通应用程序代码不应该访问的非标准包(例如 com.sun.* 等)。因此,OSGi 仅提供由相关 JCP 规范为您正在使用的 Java 版本定义的包。比如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”的包??没有这样的包。您是否明确添加了此依赖项?请提供有关您所做操作的更多信息。 @PeterPenzov 你在看这些回复吗??您添加了无效的依赖项。请显示捆绑包的 pom。 @PeterPenzov 看起来你贴错了。捆绑包 DX-57.testr 是失败的包。 @PeterPenzov 我认为这不是一个好主意......现在答案与问题无关。那好吧。无论如何,最新的问题是你说过&lt;Export-Package&gt;*&lt;/Export-Package&gt;。请删除该行。 删除&lt;Export-Package&gt;*&lt;/Export-Package&gt;后再次更新帖子【参考方案2】:

我最近发布了Drombler FX 的第一个早期访问版本,这是一个基于 OSGi 和 Maven(POM 优先;默认使用 Apache Felix)的 JavaFX 模块化富客户端平台。

您可以在此处阅读更多信息:http://puces-blog.blogspot.ch/2012/12/drombler-fx-building-modular-javafx.html

入门:http://wiki.drombler.org/GettingStarted

我最近尝试使用 Java SE 8 的预发布版本运行它,并且到目前为止它工作正常,虽然我还没有指定 Java SE 8 的系统包,所以我不确定是否一切正常/如果您可以访问所有内容。

【讨论】:

以上是关于使用 Java 8 和 JavaFX 测试 Apache Felix的主要内容,如果未能解决你的问题,请参考以下文章

JavaFX 8 的基本 JUnit 测试

JavaFX 8 WebEngine:如何从 javascript 获取 console.log() 到 java 中的 System.out?

JavaFX FXML API 版本警告

从另一个类(java、javafx)调用对象

为什么OpenJDK 8在使用Docker时没有拔出正确的JavaFX库?

Javafx 8 替换 textarea 中的文本并保持格式