如何将JavaLX的MigLayout转换为OSGi包,以便它可以在OSGi容器中运行?

时间:2013-11-09 18:00:21

标签: javafx osgi apache-felix equinox miglayout

我正在尝试制作一个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插件to put the JavaFX runtime on the 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 command on Peter Kriens' bnd tool来更改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

但是将结果“bundle”放在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 (注意我的软件包实际上是x.x.client而不仅仅是客户端。但是我已经从包的名称中删除了x.x.这就是这个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

我创建了一个project on GitHub,您可以下载以查看问题。如前所述,如果您按照README.txt文件中的说明检查它,该项目应立即运行。

如果您想查看我在使用MigLayout库时遇到的问题,请通过取消注释MigPane的导入来编辑文件client/src/main/resources/fxml/login.fxml。重建项目,将新创建的client-1.0.0-SNAPSHOT.jar复制到app/bundle/目录,用$ rm -rf app/felix-cache/清除felix-cache目录,然后从{{1}中重新启动felix启动器使用命令app

的目录

2 个答案:

答案 0 :(得分:1)

我99%肯定fxml你的问题是你没有设置正确的类加载器。请显示您的fxml加载代码

答案 1 :(得分:0)

您还需要在UI包中包含相应的Import-Package语句。创建这些内容的最简单方法是在UI包的pom中使用maven bundle plugin

您使用bundle插件检查生成的Manifest以确保存在所有必要的Import-Package语句。