我正在尝试制作一个 OSGi 包,它是一个用 JavaFX 编写的 GUI。我的设置如下:
OS name: "linux", version: "3.8.0-25-generic", arch: "amd64", family: "unix"
Java version: 1.7.0_45, vendor: Oracle Corporation
Apache Maven 3.1.1 (0728685237757ffbf44136acec0402957f723d9a; 2013-09-17 10:22:22-0500)
我按照 JavaFX Maven 插件提供的说明将 JavaFX 运行时放在 classpath 上。我使用 apache felix 作为 OSGi 容器。所有这些一起工作得很好。(IE:我可以将 JavaFX GUI 创建为 OSGi 包,它可以工作!)
我遇到的问题是 JavaFX 的 MigLayout 库。我正在使用这些依赖项:
<dependency>
<groupId>com.miglayout</groupId>
<artifactId>miglayout-core</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>com.miglayout</groupId>
<artifactId>miglayout-javafx</artifactId>
<version>4.2</version>
</dependency>
这些都不是 OSGi 包,对它们的依赖会导致应用程序中发生运行时错误,特别是:
javafx.fxml.LoadException: java.lang.ClassNotFoundException: org.tbee.javafx.scene.layout.fxml.MigPane from bundle 7 (client)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2489)
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2333)
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2301)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2154)
at client.Gui.start(Gui.scala:22)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:89)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: org.tbee.javafx.scene.layout.fxml.MigPane from bundle 7 (client)
at akka.osgi.impl.BundleDelegatingClassLoader.loadClass(BundleDelegatingClassLoader.scala:49)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2557)
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2546)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2487)
... 14 more
Caused by: java.lang.ClassNotFoundException: org.tbee.javafx.scene.layout.fxml.MigPane not found by com.typesafe.akka.osgi [1]
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1532)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at akka.osgi.impl.BundleDelegatingClassLoader.loadClass(BundleDelegatingClassLoader.scala:46)
... 18 more
这是我尝试加载的示例 fxml 文件 login.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.control.Button?>
<?import org.tbee.javafx.scene.layout.fxml.MigPane?>
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="client.view.LoginView" fx:id="pane">
<children>
<Button text="Login" fx:id="loginButton" onAction="#login"/>
</children>
</StackPane>
请注意,如果我在 OSGi 容器之外运行此应用程序,则页面加载正常。但在 OSGi 容器中,它会因上述堆栈跟踪而失败。此外,如果我注释掉 MigPane 的导入,它也会在 OSGi 容器中正常加载。所以下面的例子在 OSGi 容器中运行良好:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.control.Button?>
<!--<?import org.tbee.javafx.scene.layout.fxml.MigPane?>-->
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="client.view.LoginView" fx:id="pane">
<children>
<Button text="Login" fx:id="loginButton" onAction="#login"/>
</children>
</StackPane>
我尝试通过使用wrap
Peter Kriens 的 bnd 工具上的命令更改 MigLayout jar 文件来解决此问题,以便它们将在清单文件中包含必要的 OSGi 元数据。
命令:
// The original jar (the one specified in the aforementioned maven
// dependency - which is not an OSGi bundle) is contained in the
// wrap subdirectory.
$ java -jar bnd-2.1.0.jar wrap miglayout-core-4.2.jar wrap/miglayout-core-4.2.jar
$ java -jar bnd-2.1.0.jar wrap miglayout-javafx-4.2.jar wrap/miglayout-javafx-4.2.jar
miglayout-core-4.2.jar MANIFEST.MF:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Bnd-LastModified: 1384015623755
Build-Jdk: 1.6.0_29
Built-By: Mike
Bundle-ManifestVersion: 2
Bundle-Name: miglayout.core
Bundle-SymbolicName: miglayout.core
Bundle-Version: 0
Created-By: 1.7.0_45 (Oracle Corporation)
Export-Package: net.miginfocom.layout
Originally-Created-By: Apache Maven
Tool: Bnd-2.1.0.20130426-122245
miglayout-javafx-4.2.jar MANIFEST.MF:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Bnd-LastModified: 1384015600603
Build-Jdk: 1.6.0_29
Built-By: Mike
Bundle-ManifestVersion: 2
Bundle-Name: miglayout.javafx
Bundle-SymbolicName: miglayout.javafx
Bundle-Version: 0
Created-By: 1.7.0_45 (Oracle Corporation)
Export-Package: org.tbee.javafx.scene.layout;uses:="javafx.scene,javafx.
scene.layout,net.miginfocom.layout",org.tbee.javafx.scene.layout.fxml;u
ses:="javafx.beans,javafx.scene,net.miginfocom.layout,org.tbee.javafx.s
cene.layout"
Import-Package: javafx.beans;resolution:=optional,javafx.collections;res
olution:=optional,javafx.geometry;resolution:=optional,javafx.scene;res
olution:=optional,javafx.scene.control;resolution:=optional,javafx.scen
e.layout;resolution:=optional,javafx.scene.paint;resolution:=optional,j
avafx.scene.shape;resolution:=optional,javafx.stage;resolution:=optiona
l,net.miginfocom.layout;resolution:=optional
Originally-Created-By: Apache Maven
Tool: Bnd-2.1.0.20130426-122245
但是将生成的“捆绑包”放入 OSGi 容器并不能解决问题。我犯了同样的错误。
我不知道还能尝试什么,所以我可以使用 OSGi 容器中的 MigLayout 库。
我需要做什么才能在 OSGi 容器中使用 MigLayout?
编辑
这是加载 FXML 的代码。它是用 Scala 编写的。
package client
import _root_.javafx.application.Application
import _root_.javafx.stage.Stage
import _root_.javafx.scene.{Parent, Scene}
import _root_.javafx.fxml.FXMLLoader
import client.view.{Login, Screen}
class Gui extends Application {
def start(stage: Stage) {
val initialScreen: Screen = Login
stage.setTitle("GUI")
val loader = new FXMLLoader
loader.setLocation(initialScreen.url)
try {
val root: Parent = loader.load(initialScreen.inputStream).asInstanceOf[Parent]
val scene: Scene = new Scene(root, 800, 600)
scene.getStylesheets.add("/fxml/styles/styles.css")
stage.setScene(scene)
stage.show()
} catch {
case t: Throwable => t.printStackTrace()
}
}
override def stop() {
System.out.println("Stopping JavaFX Application")
Container.shutdown()
}
}
此外,这里是为客户端定义的 Import-Packages。构建实际上是使用 scala 和 osgi 插件通过 gradle 完成的。
build.gradle 的片段:
def importPackages =
' akka.actor' +
', akka.actor.dungeon' +
', akka.event' +
', akka.osgi' +
', javafx.application' +
', javafx.beans' +
', javafx.collections' +
', javafx.fxml' +
', javafx.geometry' +
', javafx.scene' +
', javafx.scene.control' +
', javafx.scene.image' +
', javafx.scene.layout' +
', javafx.scene.paint' +
', javafx.scene.shape' +
', javafx.stage' +
', net.miginfocom.layout' +
', org.osgi.framework' +
', org.tbee.javafx.scene.layout' +
', org.tbee.javafx.scene.layout.fxml' +
', scala' +
', scala.collection' +
', scala.reflect' +
', scala.runtime'
jar {
manifest {
name = "client (OSGi bundle)"
instruction 'Bundle-Vendor', 'Company'
instruction 'Bundle-Description', 'Client (OSGi bundle)'
instruction 'Private-Package', 'client'
instruction 'Bundle-Activator', 'client.ClientActivator'
instruction 'Import-Package', importPackages
}
}
编辑 2
我按照tomsontom的建议修改了加载fxml的代码来设置类加载器。这是更新的代码:
来自 build.gradle 的片段:
def importPackages =
' akka.actor' +
', akka.actor.dungeon' +
', akka.event' +
', akka.osgi' +
', javafx.application' +
', javafx.beans' +
', javafx.collections' +
', javafx.fxml' +
', javafx.geometry' +
', javafx.scene' +
', javafx.scene.control' +
', javafx.scene.image' +
', javafx.scene.layout' +
', javafx.scene.paint' +
', javafx.scene.shape' +
', javafx.stage' +
', net.miginfocom.layout' +
', org.osgi.framework' +
', org.tbee.javafx.scene.layout' +
', org.tbee.javafx.scene.layout.fxml' +
', scala' +
', scala.collection' +
', scala.reflect' +
', scala.runtime'
fxml加载代码:
def start(stage: Stage) {
val initialScreen: Screen = Login
stage.setTitle("GUI")
val loader = new FXMLLoader
loader.setClassLoader(getClass.getClassLoader)
loader.setLocation(initialScreen.url)
try {
val root: Parent = loader.load(initialScreen.inputStream).asInstanceOf[Parent]
val scene: Scene = new Scene(root, 800, 600)
scene.getStylesheets.add("/fxml/styles/styles.css")
stage.setScene(scene)
stage.show()
} catch {
case t: Throwable => t.printStackTrace()
}
}
但是请注意,现在我在 OSGi 容器中运行应用程序时收到了类似但不同的错误消息:
javafx.fxml.LoadException: java.lang.ClassNotFoundException: org.tbee.javafx.scene.layout.fxml.MigPane
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2489)
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2333)
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2301)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2154)
at client.Gui.start(Gui.scala:23)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:89)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ClassNotFoundException: org.tbee.javafx.scene.layout.fxml.MigPane
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.apache.felix.framework.ExtensionManager$ExtensionManagerWiring.getClassByDelegation(ExtensionManager.java:873)
at org.apache.felix.framework.BundleWiringImpl.searchImports(BundleWiringImpl.java:1553)
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1484)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2557)
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2546)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2487)
... 14 more
它仍在生成 ClassNotFoundException。但是这一次,堆栈跟踪不包括与跟踪来自哪个包有关的任何内容。这是一个微妙的区别,我不知道这意味着什么。有任何想法吗?
编辑 3
在使用 bnd 命令行实用程序后,我修改了 OP 以包含为两个 MigLayout 依赖项生成的 MANIFEST.MF 文件。
编辑 4
client.jar MANIFEST.MF(请注意,我的包实际上是 xxclient 而不仅仅是客户端。但是我已经从包名称中删除了 SO 的 xx。这就是此 MANIFEST.MF 文件中行间距差异的原因):
Manifest-Version: 1.0
Export-Package: client;version="1.0.0.SNAPSHOT";uses:=
"akka.actor,akka.osgi,javafx.application,javafx.stage,org.osgi.framew
ork,scala.reflect",client.controller;version="1.0.0.S
NAPSHOT";uses:="akka.actor,akka.event,scala,scala.reflect,scala.runti
me",client.message;version="1.0.0.SNAPSHOT";uses:="sc
ala,scala.collection,scala.reflect",client.model;vers
ion="1.0.0.SNAPSHOT";uses:="scala.reflect",client.vie
w;version="1.0.0.SNAPSHOT";uses:="scala,scala.collection,scala.reflec
t"
Private-Package: client
Tool: Bnd-2.1.0.20130426-122213
Bundle-Name: client (OSGi bundle)
Created-By: 1.7.0_45 (Oracle Corporation)
Bundle-Vendor: Company
Bundle-Version: 1.0.0.SNAPSHOT
Bnd-LastModified: 1384035615000
Bundle-ManifestVersion: 2
Bundle-Activator: client.ClientActivator
Bundle-Description: Client (OSGi bundle)
Bundle-SymbolicName: client
Import-Package: akka.actor;version="[2.2,3)",akka.event;version="[2.2,
3)",akka.osgi;version="[2.2,3)",javafx.application,javafx.collections
,javafx.fxml,javafx.scene,javafx.stage,org.osgi.framework;version="[1
.7,2)",scala;version="[2.10,3)",scala.collection;version="[2.10,3)",s
cala.reflect;version="[2.10,3)",scala.runtime;version="[2.10,3)",net.
miginfocom.layout,org.tbee.javafx.scene.layout.fxml,org.tbee.javafx.s
cene.layout,javafx.scene.layout,javafx.scene.image,javafx.scene.contr
ol,javafx.scene.shape,javafx.scene.paint,javafx.geometry,javafx.beans
,akka.actor.dungeon;version="[2.2,3)"
编辑 5
我在 GitHub 上创建了一个项目,您可以下载该项目以查看问题。正如当前签入的那样,如果您按照文件 README.txt 中的说明进行签出,该项目应该会在您签出后立即运行。
如果您想查看我在使用 MigLayout 库时遇到的问题,请client/src/main/resources/fxml/login.fxml
通过取消注释 MigPane 的导入来编辑文件。重建项目,将新创建的 client-1.0.0-SNAPSHOT.jar 复制到app/bundle/
目录,清除 felix-cache 目录,$ rm -rf app/felix-cache/
然后app
使用命令从目录中重新启动 felix 启动器$ java -jar bin/felix.jar
。