【问题标题】:raspberry pi + maven + jdk1.8 + javafx树莓派 + maven + jdk1.8 + javafx
【发布时间】:2013-11-14 16:24:17
【问题描述】:

我正在开发一个必须在树莓派上运行的 JavaFx 应用程序。 我按照所有步骤在 Raspberry 上安装 jdk1.8,下载了 maven 3.1.1 并将项目复制到我的树莓上。

当我跑步时

mvn clean package

构建成功,但是当我运行命令时

java -cp app.jar my.main.class

如此处所述 (https://wiki.openjdk.java.net/display/OpenJFX/OpenJFX+on+the+Raspberry+Pi#OpenJFXontheRaspberryPi-RunningaJDKEarlyAccessbuildontheRaspberryPi)

我收到此错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
    at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NoClassDefFoundError: javafx/scene/media/Media
    at com.ilgala.dmp.components.Playlist.createPlayer(Playlist.java:78)
    at com.ilgala.dmp.components.Playlist.initPlaylist(Playlist.java:55)
    at com.ilgala.dmp.components.Playlist.<init>(Playlist.java:44)
    at com.ilgala.dmp.videoPlayer.VideoPlayer.<init>(VideoPlayer.java:28)
    at com.ilgala.dmp.App.start(App.java:44)
    at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
    at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:331)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:297)
    at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:294)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:294)
    at com.sun.glass.ui.lens.LensApplication$RunnableEvent.dispatch(LensApplication.java:182)
    at com.sun.glass.ui.lens.LensApplication._runLoop(LensApplication.java:861)
    at com.sun.glass.ui.lens.LensApplication.access$1700(LensApplication.java:58)
    at com.sun.glass.ui.lens.LensApplication$4.run(LensApplication.java:914)
    ... 1 more
Caused by: java.lang.ClassNotFoundException: javafx.scene.media.Media
    at java.net.URLClassLoader$1.run(URLClassLoader.java:359)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:348)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:347)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 16 more
Exception running application my.app

谁能帮帮我?我已将 jfxrt 包含在我的 pom.xml

就是这样:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.group.Id/groupId>
    <artifactId>my.artifact.Id</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>
    <name>App</name>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.ilgala.dmp.App</mainClass>
                            <classpathPrefix>dependency-jars/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeGroupIds>com.oracle</includeGroupIds>
                            <includeArtifactIds>javafx</includeArtifactIds>
                            <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>javafx</artifactId>
            <version>2.2</version>
            <scope>system</scope>
            <systemPath>/path/to/jfxrt.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

【问题讨论】:

  • 您真的不应该将 Java 8 用于实际项目。无论如何,您在 pom 中将您的项目指定为 Java 7。为什么要使用 Java 8 作为运行时环境?
  • 嗯,我按照我在 Oracle JavaFx 页面上找到的指南进行操作,这就是我使用该指南的原因。树莓已经有JDK1.7.0_40 我应该用那个吗?
  • 我无法想象他们为什么会推荐早期访问版本。我会使用 Java 7 进行生产性使用。虽然这可能与您的问题无关。

标签: java maven maven-3 raspberry-pi


【解决方案1】:

你应该直接看http://zenjava.com/javafx/maven/

Javafx maven 插件:https://github.com/zonski/javafx-maven-plugin

它是一个用于构建 javafx 应用程序的好插件,您可以使用它的命令将 jfxrt.jar 链接到 maven 构建:

mvn com.zenjava:javafx-maven-plugin:2.0:fix-classpath

命令说明如下:http://zenjava.com/javafx/maven/fix-classpath.html

我也引用:

如果你不想使用 fix-classpath Maven 命令或者有 运行时出现问题。您可以尝试手动移动 JAR 文件 自己从 lib 目录到 ext 目录。没有了 比这更需要魔法。

【讨论】:

  • 我在使用 zenjava 插件时遇到此错误,在树莓上使用 maven 3.0.5 运行它:无法执行目标 com.zenjava:javafx-maven-plugin:2.0:fix-classpath (default- cli)在项目standalone-pom上:执行目标com.zenjava:javafx-maven-plugin:2.0:fix-classpath的默认cli失败:插件com.zenjava:javafx-maven-plugin:2.0或其依赖项之一无法已解决:在指定路径/​​usr/lib/jvm/jdk-7-oracle-armhf/jre/../lib/ant-javafx.jar 找不到工件javafx-packager:javafx-packager:jar:1.7跨度>
  • @IIGala 这是一个错误,获取 2.0-SNAPSHOT 来修复它:github.com/zonski/javafx-maven-plugin/issues/27
  • 完成,jvm 文件夹上的 lib 文件夹丢失,然后插件工作。现在覆盆子似乎可以满足我的需要,谢谢大家!