maven JavaFX样式在Raspberry PI 3中被破坏并显示错误,但在Windows和kali Linux中运行良好

knsnq2tg  于 2024-01-06  发布在  Maven
关注(0)|答案(2)|浏览(141)

我在windows中使用JavaFX,然后我转移到linux为raspberry pi制作GUI,因为raspberry pi也使用linux。所以错误概率应该较小。但是**当我在Kali Linux中运行jar文件时,它运行得很好。**x1c 0d1x

但是当我在Raspberry Pi中运行文件时,图形会被破坏。

我得到一些错误在终端乌藨子pi

jibon@jibon:~/Desktop $ java -jar LinuxApp1-1.0-SNAPSHOT.jar 
Nov 29, 2023 1:19:30 PM com.sun.javafx.application.PlatformImpl startup
WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @566b4ab9'
Loading library prism_es2 from resource failed: java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libprism_es2.so: /home/jibon/.openjfx/cache/17.0.6/libprism_es2.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libprism_es2.so: /home/jibon/.openjfx/cache/17.0.6/libprism_es2.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
    at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
    at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:388)
    at (...)
Loading library prism_sw from resource failed: java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libprism_sw.so: /home/jibon/.openjfx/cache/17.0.6/libprism_sw.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libprism_sw.so: /home/jibon/.openjfx/cache/17.0.6/libprism_sw.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
    at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
    at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:388)
    at (...)
Loading library glass from resource failed: java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libglass.so: /home/jibon/.openjfx/cache/17.0.6/libglass.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libglass.so: /home/jibon/.openjfx/cache/17.0.6/libglass.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
    at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
    at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:388)
    at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:232)
    at (...)
Loading library glassgtk3 from resource failed: java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libglassgtk3.so: /home/jibon/.openjfx/cache/17.0.6/libglassgtk3.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libglassgtk3.so: /home/jibon/.openjfx/cache/17.0.6/libglassgtk3.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
    at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
    at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:388)
    at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:232)
    at (...)
    at java.base/java.lang.Thread.run(Thread.java:840)
Loading library javafx_font from resource failed: java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libjavafx_font.so: /home/jibon/.openjfx/cache/17.0.6/libjavafx_font.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libjavafx_font.so: /home/jibon/.openjfx/cache/17.0.6/libjavafx_font.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
    at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
    at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:388)
    at (...)
Loading library javafx_font_freetype from resource failed: java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libjavafx_font_freetype.so: /home/jibon/.openjfx/cache/17.0.6/libjavafx_font_freetype.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libjavafx_font_freetype.so: /home/jibon/.openjfx/cache/17.0.6/libjavafx_font_freetype.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
    at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
    at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:388)
    at (...)
Loading library javafx_font_pango from resource failed: java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libjavafx_font_pango.so: /home/jibon/.openjfx/cache/17.0.6/libjavafx_font_pango.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libjavafx_font_pango.so: /home/jibon/.openjfx/cache/17.0.6/libjavafx_font_pango.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
    at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
    at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:388)
    at (...)

(java:1933): Gdk-WARNING **: 13:20:00.341: Native Windows wider or taller than 32767 pixels are not supported

(java:1933): Gdk-WARNING **: 13:20:00.352: Native Windows wider or taller than 32767 pixels are not supported
Loading library decora_sse from resource failed: java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libdecora_sse.so: /home/jibon/.openjfx/cache/17.0.6/libdecora_sse.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
java.lang.UnsatisfiedLinkError: /home/jibon/.openjfx/cache/17.0.6/libdecora_sse.so: /home/jibon/.openjfx/cache/17.0.6/libdecora_sse.so: cannot open shared object file: No such file or directory (Possible cause: can't load AMD 64 .so on a AARCH64 platform)
    at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
    at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:388)
    at  (...)

字符串
我在分享一些必要的文件。

POM.XML

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>pro.jibon.apps</groupId>
    <artifactId>LinuxApp1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>LinuxApp1</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.9.2</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>17.0.6</version>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>17.0.6</version>
            <classifier>linux</classifier>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <executions>
                    <execution>
                        <!-- Default configuration for running with: mvn clean javafx:run -->
                        <id>default-cli</id>
                        <configuration>
                            <mainClass>pro.jibon.apps.linuxapp1/pro.jibon.apps.linuxapp1.HelloApplication</mainClass>
                            <launcher>app</launcher>
                            <jlinkZipName>app</jlinkZipName>
                            <jlinkImageName>app</jlinkImageName>
                            <noManPages>true</noManPages>
                            <stripDebug>true</stripDebug>
                            <noHeaderFiles>true</noHeaderFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>pro.jibon.apps.linuxapp1.Main</mainClass>
                                </transformer>
                            </transformers>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>pro.jibon.apps.linuxapp1.Main</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Main.java

package pro.jibon.apps.linuxapp1;

public class Main {
    public static void main(String[] args) {
        HelloApplication.main(args);
    }
}

HelloApplication.java

package pro.jibon.apps.linuxapp1;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCombination;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import java.io.IOException;

public class HelloApplication extends Application {
    @FXML
    private Button button1;
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), stage.getMaxWidth(), stage.getMaxHeight());
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.initStyle(StageStyle.UTILITY);
        stage.show();
        button1 = (Button) scene.lookup("#btn");
        button1.setText("Close");
        button1.setOnAction(mouseEvent -> {
            stage.close();
        });
        stage.setOnCloseRequest(windowEvent -> {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Wait");
            alert.setContentText("Are feeling okay by clicking me huh?");
            alert.setHeaderText("WTF yaar!");
//            alert.show();
            windowEvent.consume();
        });
        stage.setAlwaysOnTop(true);
        stage.setResizable(false);
        stage.setMaximized(true);
        stage.setResizable(false);
        stage.setFullScreen(true);
        stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
        stage.fullScreenProperty().addListener((observable, oldValue, newValue) -> {
            stage.setFullScreen(true);
        });
    }

    public static void main(String[] args) {
        launch();
    }
}

hello-view.xml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.layout.*?>

<VBox xmlns="http://javafx.com/javafx/11.0.14-internal" xmlns:fx="http://javafx.com/fxml/1">
    <padding>
        <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
    </padding>
    <ToolBar nodeOrientation="LEFT_TO_RIGHT" prefHeight="40.0" prefWidth="200.0">
        <items>
            <Button mnemonicParsing="false" text="Button" />
            <Button mnemonicParsing="false" text="Button" />
            <Button mnemonicParsing="false" text="Button" />
            <Button mnemonicParsing="false" text="Button" />
            <Button mnemonicParsing="false" text="Button" />
        </items>
    </ToolBar>

    <Label fx:id="welcomeText" />
    <Button fx:id="btn" alignment="CENTER" text="Hello!">
        <effect>
            <Blend />
        </effect>
    </Button>
</VBox>

module-info.java

module pro.jibon.apps.linuxapp1 {
    requires javafx.controls;
    requires javafx.fxml;

    opens pro.jibon.apps.linuxapp1 to javafx.fxml;
    exports pro.jibon.apps.linuxapp1;
}


请帮我摆脱这个:)提前感谢。

ergxz8rk

ergxz8rk1#

您需要为您选择的目标平台使用正确版本的执行软件。JavaFX(和JRE)包括仅在给定平台上运行的本地组件。
JavaFX Java(非原生)组件是模块化的,不同的Java模块针对不同的系统。例如,在JavaFX mac模块版本中有支持Mac系统菜单的Java组件,这些组件在其他版本中不存在。对于像Pi这样的嵌入式系统也是如此,如果它没有内置的原生窗口系统,那么JavaFX需要附带一个内置的窗口模拟器(就像它的单片眼镜系统一样),否则它根本无法正常工作。
下面是Maven中javafx-graphics模块的所有文件列表,最新的稳定版本(21.0.1):

它包括所有分类的依赖项。
对于Linux,您可以看到:

  • javafx-graphics-21.0.1-linux.jar
  • javafx-graphics-21.0.1-linux-aarch64.jar
  • javafx-graphics-21.0.1-linux-arm32-monocle.jar
  • javafx-graphics-21.0.1-linux-aarch64-monocle.jar

刚刚分类为“Linux”的是运行在x64(Intel和AMD)处理器上的linux。
一个分类为“linux-aarch 64”的是运行在ARM 64位处理器上的Linux。
一个分类为“linux-arm 32-monocle”的是32位ARM处理器,而“linux-aarch 64-monocle”是使用64位ARM处理器在Linux上运行并使用monocle
Monocle是JavaFX的Glass窗口组件在嵌入式系统中的实现。Monocle为没有底层窗口系统的简单嵌入式系统提供窗口功能和对本Map形的访问。
我没有在Pi上使用过JavaFX,也没有使用过Pi,也没有研究过它们有什么处理器和功能,但你可以这样做,然后在构建应用程序时选择适当的分类依赖项。
它将不仅仅是“linux”,因为这是为英特尔处理器和PI不使用英特尔处理器。这是(至少是一个原因)为什么你的应用程序在运行64位英特尔类型处理器的Linux系统上运行时工作,而不是在PI上。
您需要检查目标系统的处理器类型。如果Pi 3是32位,则需要arm 32版本的JavaFX(和JRE)。如果Pi 3是64位的,那么你需要一个aarch 64版本的JavaFX。如果Pi 3是64位的,并且运行一个内置的窗口系统如果Pi 3是64位的并且没有运行窗口系统,那么也许可以使用“linux-aarch 64-monocle”版本。
所以希望你能理解。你构建和打包应用程序的方式不适合你的目标系统。没有分类器的JavaFX Maven构建将默认使用构建它们的系统的JavaFX的分类版本。所以,如果你在Windows Intel处理器系统上构建,它将使用它作为Maven引入的JavaFX版本。但是你不是在Pi上构建你的软件,因此,你需要为Pi指定正确的分类器,用于构建你拥有的 * 所有 * JavaFX依赖项(不仅仅是javafx-graphics系统),可能还包括transitive JavaFX依赖项。
例如,也许如果针对JavaFX 21.0.1的linux-aarch 64-monocle(这只是一个猜测,我没有办法尝试):

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-controls</artifactId>
    <version>21.0.1</version>
    <classifier>linux-aarch64-monocle</classifier>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-fxml</artifactId>
    <version>21.0.1</version>
    <classifier>linux-aarch64-monocle</classifier>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-graphics</artifactId>
    <version>21.0.1</version>
    <classifier>linux-aarch64-monocle</classifier>
</dependency>
<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-base</artifactId>
    <version>21.0.1</version>
    <classifier>linux-aarch64-monocle</classifier>
</dependency>

字符串
这样的构建只能在Linux ARM 64位系统上工作(而不是其他),并且Linux系统需要安装JavaFX工作所需的库(例如用于JavaFX 21的glibc 3)。所以不要指望它也能在Intel Linux系统上工作。如果您将许多不同的分类依赖项捆绑为described here,但对于像Pi这样的受限设备来说,这似乎并不推荐。
我对Pi了解不多,但我认为它的最新版本比3要多,所以可能是最近版本的JavaFX已经放弃了对旧Pi系统的支持(我不知道)。
你在做什么(将JavaFX模块着色到一个jar中),是完全不受支持的。这并不意味着你不能让它工作,这只是意味着该软件不是以这种方式设计的(虽然它可能会工作)。这就是为什么你被警告:“警告:不支持的JavaFX配置”所以不要期望你正在做的事情会得到很多支持。如果你想要一个支持嵌入式系统的JavaFX发行版,Gluon提供了这些,你可以联系他们讨论,我不提供这些,也没有专业知识来提供建议。
着色破坏了模块性,所以你不应该为着色系统提供模块信息,所有东西都应该从类路径运行(即使JavaFX并不正式支持该运行时方法)。
我猜Maven Shade的首选方法是使用jlink提供从JavaFX SDK链接的模块和本机组件,或者使用GraalVM编译为本机二进制文件(对于Pi目标,这两种方法我都不能建议您)。

您可能会在Web上找到一些文章,其中详细介绍了如何让JavaFX在Pi上工作,但有些文章已经过时且不相关(或误导性的)最新JavaFX版本,有些可能会提供最新的信息和说明(我不知道在这里可以链接到哪些正确的文章)。或者,可以在包含JavaFX的Pi上安装JRE(可能有像阿苏尔或BellSoft这样的供应商提供这样的发行版--我没有检查)。
使用JavaFX进行Pi开发的一些资源(有些资源将过时,但可能仍包含一些相关信息,因此请注意以下任何过于字面的内容):

我的建议是,要么找到并遵循一个值得信赖的指南,详细说明你需要做什么才能让你的软件在你的目标系统上工作,如果你找不到一个,那么就从像Gluon这样的人那里寻求专业帮助。

jk9hmnmh

jk9hmnmh2#

经过长时间的Web研究,我找到了自己的解决方案,并希望与大家一步一步地分享。

  • 在主机上安装默认JDK
  • 在主机上安装最新的intellij
  • 在Raspi上安装OpenJFX
  • 在Raspi上安装默认JDK
  • 然后在Raspi上安装WiringPi
  • 在Raspi上安装maven
  • 在主机中使用Maven和默认JDK创建项目
  • 在pom.xml上,添加shade插件以返回包含依赖项的jar文件

pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>pro.jibon.apps.linuxapp1.Main</mainClass>
                    </transformer>
                </transformers>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>

字符串

  • 然后用maven在项目中添加pi 4j

pom.xml

<dependency>
    <groupId>com.pi4j</groupId>
    <artifactId>pi4j-core</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>com.pi4j</groupId>
    <artifactId>pi4j-device</artifactId>
    <version>1.3</version>
</dependency>
<dependency>
    <groupId>com.pi4j</groupId>
    <artifactId>pi4j-library-pigpio</artifactId>
    <version>2.2.1</version>
</dependency>
<dependency>
    <groupId>com.pi4j</groupId>
    <artifactId>pi4j-gpio-extension</artifactId>
    <version>1.3</version>
</dependency>

  • 如果尚未添加,则添加openjfx javafx-graphics

pom.xml

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-graphics</artifactId>
    <version>17.0.6</version>
</dependency>

  • 创建一个伪Main类以避免扩展错误

Main.java

package pro.jibon.apps.linuxapp1;
public class Main {
    public static void main(String[] args) {
        HelloApplication.main(args);
    }
}

  • 使用这两个函数来检查它是否为rapi

HelloApplication.java

public static boolean isLinux() {
    return (getOsName().toLowerCase().contains("linux"));
}
public static boolean isRasPi() {
    return (isLinux() && Files.exists(Paths.get("/opt/pigpio"), LinkOption.NOFOLLOW_LINKS));
}

  • mvn clean用于清除目标和库
  • mvn安装以获取目标文件夹中的着色jar文件

Rememer将在Raspi建立mvn
最后,您现在可以导入

import com.pi4j.io.gpio.*;
import static com.pi4j.system.SystemInfo.getOsName;


并享受JavaFX与GPIO引脚控制在Raspberry Pi。
打开和关闭13号引脚上LED的JavaFX示例(Pi 4j 2)

ledBtn = (Button) scene.lookup("#ledPower");
GpioController gpio = null;
GpioPinDigitalOutput ledPin;
int ledPinNumber = 2; //16
if (isRasPi()){
    try{
        gpio = GpioFactory.getInstance();
        ledPin = gpio.provisionDigitalOutputPin(RaspiPin.getPinByAddress(ledPinNumber), PinState.LOW);
    }catch (Exception e){
        ledPin = null;
    }
}else {
    ledPin = null;
}

GpioPinDigitalOutput finalLedPin = ledPin;
ledBtn.setOnAction(mouseEvent->{
    ledBtn.setText("Wait.");
    if (finalLedPin != null){
        ledBtn.setText("Wait..");
        try{
            if (finalLedPin.getState() == PinState.LOW){
                ledBtn.setText("Turning LED ON");
                finalLedPin.high();
            }else{
                ledBtn.setText("Turning LED OFF");
                finalLedPin.low();
            }
        }catch (Exception e){
            //AlertBox(e.getMessage());
        }
    }else{
        ledBtn.setText("Unsupported");
    }
});


记住清理GPIO管脚

stage.setOnCloseRequest(windowEvent -> {
    if (isRasPi()) {
        try {
            GpioController gpio2 = GpioFactory.getInstance();
            gpio2.shutdown();
        }catch (Exception e){

        }
    }
    //windowEvent.consume();
});


谢谢大家。

相关问题