0

问题总结

当我尝试在桌面上编写代码并将其远程部署到我的 Pi Zero W 时,我遇到了类路径错误。

背景

Pi4J v2.0在此处输入链接描述库提供了一个 Maven Archetype 来帮助开发和在远程 PC 上开发的说明。

环境

IDE: Intellij Ultimate 
Development PC is Windows 10
Java 11.0.2
Maven 3.8.3

Rasp Pi Zero W
Open jdk java 11.0.8
Maven 3.8.3

迄今为止采取的步骤

在开发 PC 上,我已经安装了 Archetype:

INSTALL ARCHETYPE
cd pi4j-maven-archetype-master
mvn install
...
BUILD SUCCESS

CREATE PROJECT
mkdir pitest2
mvn archetype:generate -DarchetypeCatalog=local
....
answer questions per instructions
BUILD SUCCESS

然后我在 Intellij 中打开项目并使用 maven 刷新它并将 raspberry.properties 编辑为我的 pi IP 地址并登录。

在构建和部署 Archetype 附带的示例代码时,说明状态:

The project declares in the pom.xml the following maven/ant goals that you can execute with the command shown :

mvn clean : delete all compiled files from local and remote project
mvn install : builds the project, uploads the required jars to the remote target RPI board
mvn antrun:run@exec : runs the program on the remote target
mvn antrun:run@debug : runs a remote debugging session on the target RPI.

该命令mvn install有效,并且文件被复制到 Pi。

当我尝试mvn antrun:run@exec命令结果时:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  16.818 s
[INFO] Finished at: 2021-11-16T20:15:56+13:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:3.0.0:run (exec) on project pitest2: An Ant BuildException has occured: The following error occurred while executing this line:
[ERROR] C:\Users\RM\IdeaProjects\pitest2\antrun\build.xml:166: The following error occurred while executing this line:
[ERROR] C:\Users\RM\IdeaProjects\pitest2\antrun\build.xml:123: Remote command failed with exit status 1
[ERROR] around Ant part ...<ant antfile="antrun/build.xml" target="run-remote" />... @ 9:59 in C:\Users\RM\IdeaProjects\pitest2\target\antrun\build-main.xml
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

采取的其他步骤

部署的输出显示,pitest.jar 已复制到 Pi 及其依赖项。

pi@raspberrypi: ls
pi4j-core.jar
pi4j-library-pigpio.jar
pi4j-plugin-pigpio.jar
pi4j-plugin-raspberrypi.jar
pitest2.jar
pitest2-jar-with-dependencies.jar
slf4j-api.jar
slf4j-simple.jar

其他直接在 pi 上执行的尝试:

pi@raspberrypi:~/IdeaProjects/pitest2/dist/lib $ java -cp . pitest2.jar
Error: Could not find or load main class pitest2.jar
Caused by: java.lang.ClassNotFoundException: pitest2.jar
pi@raspberrypi:~/IdeaProjects/pitest2/dist/lib $ java -cp . pitest2.jar com.geekhelp.Main
Error: Could not find or load main class pitest2.jar
Caused by: java.lang.ClassNotFoundException: pitest2.jar
pi@raspberrypi:~/IdeaProjects/pitest2/dist/lib $ java -cp ./ pitest2.jar com.geekhelp.Main
Error: Could not find or load main class pitest2.jar
Caused by: java.lang.ClassNotFoundException: pitest2.jar
pi@raspberrypi:~/IdeaProjects/pitest2/dist/lib $ java -cp pitest2.jar com.geekhelp.Main
Exception in thread "main" java.lang.NoClassDefFoundError: com/pi4j/util/Console
        at com.geekhelp.Main.<clinit>(Main.java:24)
Caused by: java.lang.ClassNotFoundException: com.pi4j.util.Console
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 1 more
pi@raspberrypi:~/IdeaProjects/pitest2/dist/lib $

/dist 下似乎是一个胖罐子

pi@raspberrypi:~/IdeaProjects/pitest2/dist $ java -cp pitest2.jar com.geekhelp.Main
21:01:58 [main] INFO com.pi4j.util.Console - ---------------------------
21:01:58 [main] INFO com.pi4j.util.Console - |  Hello Rasbian world !  |
21:01:58 [main] INFO com.pi4j.util.Console - ---------------------------
21:01:58 [main] INFO com.pi4j.Pi4J - New auto context
21:01:58 [main] INFO com.pi4j.Pi4J - New context builder
...
...    
21:02:02 [main] DEBUG com.pi4j.platform.impl.DefaultRuntimePlatforms - platforms loaded [1]
21:02:02 [main] DEBUG com.pi4j.runtime.impl.DefaultRuntime - Pi4J context/runtime successfully initialized.'
21:02:02 [main] DEBUG com.pi4j.context.impl.DefaultContext - Pi4J runtime context successfully created & initialized.'
21:02:02 [main] DEBUG com.pi4j.context.impl.DefaultContextBuilder - Pi4J successfully created and initialized a new runtime 'Context'.'
21:02:02 [main] INFO com.pi4j.util.Console - --------------------
21:02:02 [main] INFO com.pi4j.util.Console - |  Pi4J PLATFORMS  |
21:02:02 [main] INFO com.pi4j.util.Console - --------------------
21:02:02 [main] INFO com.pi4j.util.Console -
PLATFORMS: [1] "Pi4J Runtime Platforms" <com.pi4j.platform.impl.DefaultPlatforms>
└─PLATFORM: "RaspberryPi Platform" {raspberrypi} <com.pi4j.plugin.raspberrypi.platform.RaspberryPiPlatform> {Pi4J Platform for the RaspberryPi series of products.}
21:02:03 [main] INFO com.pi4j.util.Console -
com.pi4j.provider.exception.ProviderNotFoundException: Pi4J provider [pigpio-digital-output] could not be found.  Please include this 'provider' JAR in the classpath.
        at com.pi4j.provider.impl.DefaultRuntimeProviders.get(DefaultRuntimeProviders.java:238)
        at com.pi4j.provider.impl.DefaultProviders.get(DefaultProviders.java:147)
        at com.pi4j.provider.Providers.get(Providers.java:253)
        at com.pi4j.context.Context.create(Context.java:316)
        at com.pi4j.internal.IOCreator.create(IOCreator.java:58)
        at com.pi4j.internal.IOCreator.create(IOCreator.java:96)
        at com.pi4j.internal.IOCreator.create(IOCreator.java:176)
        at com.geekhelp.Main.run(Main.java:63)
        at com.geekhelp.Main.main(Main.java:34)
21:02:03 [main] DEBUG com.pi4j.platform.impl.DefaultRuntimePlatforms - removed platform from managed platform map [id=raspberrypi; name=RaspberryPi Platform; class=com.pi4j.plugin.raspberrypi.platform.RaspberryPiPlatform]

一位同事从 GitHub 下载了我的项目(与 Archetype 没有变化)并在 Rasp Pi 4(不是零 W)上成功运行它。

问:为什么代码看不到依赖项?什么时候跑?

4

1 回答 1

1

我设法修复它。

Pi Zero W 的问题在于,常用的 Java JDK 的默认版本给出了不受支持的 ARM 错误。所以我按照这里的说明安装了 Zulu 版本:

https://webtechie.be/post/2020-08-27-azul-zulu-java-11-and-gluon-javafx-11-on-armv6-raspberry-pi/

从我的桌面 IDE 进行远程执行时,rasperberry.properties 文件的值指向 /usr/lib/jvm/default-java 下的 default-java 文件夹,但我安装的 Java JDK 的 zulu 版本不会创建此文件夹。

我的解决方法是更改​​以下行rasperberry.properties

#target.remote.jre=/usr/lib/jvm/default-javam #COMMENT OUT THIS LINE
target.remote.jre=/usr/lib/jvm/zulu11.41.75-ca-jdk11.0.8-linux_aarch32hf #PUT YOUR JAVA PATH HERE
于 2021-11-16T20:21:51.040 回答