使用Java 8和JavaFX测试Apache Felix [英] Test Apache Felix with Java 8 and JavaFX

查看:142
本文介绍了使用Java 8和JavaFX测试Apache Felix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on example with JavaFX 2.2 and Java 8. I created this simple Apache Felix Activator:

激活器:

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

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>

错误:

Error:

[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中添加了外部包:

I added external packages into the file 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

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

Any idea how to fix the problem?

更新

UPDATE

删除< Export-Package> *< / Export-Package> 后,我收到此错误:

After removing <Export-Package>*</Export-Package> I get this error:

[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应用程序依赖于JavaFX API中的包,例如如 javafx.application 和其他几个。由于您看起来已经使用Maven Bundle Plugin构建了捆绑包,因此您的捆绑包已经声明了这些依赖项。这是一件好事。

JavaFX applications depend on packages from the JavaFX APIs, such as javafx.application and several others. Since it looks like you have built your bundle with Maven Bundle Plugin, your bundle already has these dependencies declared. This is a good thing.

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

In Java 8 the javafx.* packages are provided by the base JRE. However OSGi does not automatically export every package from the JRE, simply because all JREs have a bunch of non-standard packages (e.g. com.sun.* etc) that normal application code should not have access to. Therefore OSGi only makes available the packages that are defined by the relevant JCP Specification for the version of Java that you are using. For example packages such as javax.swing, org.w3c.dom, etc.

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

Since JavaFX is not a standard, there is no JCP Specification for JavaFX, and OSGi does not export the javafx.* packages. However you can configure OSGi to do this for you by setting the following configuration property when you launch OSGi:

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

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

NB I have shown how to add the javafx.application package to your runtime. It is likely you will need to add several more, i.e. all of the packages from the JavaFX API. I am not sufficiently familar with JavaFX to list these, but they should be easy enough for you to find.

这篇关于使用Java 8和JavaFX测试Apache Felix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆