老實說,我要放棄了,我已經嘗試了很多不同的可能性,現在已經好幾個星期了,差不多一個月了,多個問題。
我是一個新手程式員,尤其是 Java,但我對 Java 有很好的了解
我能夠創建一個maven專案沒問題,我對java本身的結構沒有問題,但我不完全理解pom.xml。
該檔案編譯得很好,但是當我用 啟動它時java -jar (filename),我得到以下輸出;
執行緒“main”中的例外 java.lang.NoClassDefFoundError: com/pi4j/Pi4J at com.pi.rasberri.Main.main(Main.java:16) 由:java.lang.ClassNotFoundException: com.pi4j.Pi4J at java .base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) 在 java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) 在 java.base/java.lang .ClassLoader.loadClass(ClassLoader.java:520) ... 1 more
這是我幾乎肢解的 pom;
(The long links) <modelVersion>4.0.0</modelVersion> <groupId>com.pi</groupId> <artifactId>Rasberri</artifactId> <version>3.6.1</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.32</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.32</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-core</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-plugin-raspberrypi</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-plugin-pigpio</artifactId> <version>2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId>maven-jar-plugin 3.1.0 true com.pi.rasberri.Main lib/ true true com.pi.rasberri.Main false true
還有我的代碼,即 com.pi.rasberri.Main
package com.pi.rasberri;
import com.pi4j.Pi4J;
import com.pi4j.io.gpio.digital.DigitalOutput;
import com.pi4j.io.gpio.digital.DigitalState;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
private static final int PIN_LED = 6;
public static void main(String[] args) throws Exception{
var pi4j = Pi4J.newAutoContext();
int x = 0;
var ledConfig = DigitalOutput.newConfigBuilder(pi4j)
.id("led")
.name("LED Flasher")
.address(PIN_LED)
.shutdown(DigitalState.LOW)
.initial(DigitalState.LOW)
.provider("pigpio-digital-output");
var led = pi4j.create(ledConfig);
while(x != 5){
led.high();
sleep(1000);
led.low();
sleep(500);
x ;
}
}
static void sleep(int z){
try {
Thread.sleep(z);
} catch (InterruptedException ex) {
System.out.println("Thread.sleep went fucky wucky");
}
}
}
我會很感激任何可以引導我到某個地方的東西,因為此時我很迷茫,當然,如果我找到答案,我會讓所有人都知道!提前致謝
**UPDATE**Thank you, tgdavies, the article you linked https://stackoverflow.com/a/574650/17644313 was the answer to that specific problem!**
Just in case anyone else has the same chain of issues as me;
But with that being said, i ran directly into another error;
[main] INFO com.pi4j.Pi4J - New auto context [main] INFO com.pi4j.Pi4J - New context builder [main] INFO com.pi4j.platform.impl.DefaultRuntimePlatforms - adding platform to managed platform map [id=raspberrypi; name=RaspberryPi Platform; priority=5; class=com.pi4j.plugin.raspberrypi.platform.RaspberryPiPlatform] Exception in thread "main" 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.pi.rasberri.Main.main(Main.java:27)
I cleaned the pom.xml a littlebit;
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-raspberrypi</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-pigpio</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<build>
<plugins> <!--Package all libraries classes into one runnable jar -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<mainClass>com.pi.rasberri.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
At this point, i know of atleast 2 possibilities,
- The code just needs to be run on the raspberry pi
- I have entered the classpaths incorrectly
But i honestly dont really know as to where to start troubleshooting I've tried a few things, one of which being to try another method of compiling the files, but the other method just resulted in a even longer string of errors.
What should i try next? Thanks in advance! And thanks to anyone who has suggested troubleshooting steps thus far!
Oh, and the target is to hopefully just create a singular jar file to execute on the raspberry pi
update
I tried to run it on the Raspberri pi, and it resulted in the same error so it's not that
uj5u.com熱心網友回復:
感謝 tgdavies、MadProgrammer 和 khmarbaise 的答案,修復方法是創建一個fat jar,它基本上是一個 jar 檔案,其中包含一個檔案中的所有依賴項,示例可以在原始問題/評論中找到
現在,至于另一個問題,問題在于這段代碼;
var ledConfig = DigitalOutput.newConfigBuilder(pi4j)
.id("led")
.name("LED Flasher")
.address(PIN_LED)
.shutdown(DigitalState.LOW)
.initial(DigitalState.LOW)
.provider("pigpio-digital-output");
var led = pi4j.create(ledConfig);
至于為什么,我仍在試圖弄清楚,但是當我弄清楚時,我會編輯這個答案,
導致第二個錯誤的原因,是因為目前pi4j 2.0不支持fat jars
關聯; https://github.com/Pi4J/pi4j-v2/issues/129
以防萬一有人想知道,這是最終的 POM.xml;
(自動創建的長鏈接)
<modelVersion>4.0.0</modelVersion>
<groupId>com.pi</groupId>
<artifactId>Rasberri</artifactId>
<version>3.6.1</version>
<packaging>jar</packaging>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.32</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.32</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-core</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-plugin-raspberrypi</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>com.pi4j</groupId> <artifactId>pi4j-plugin-pigpio</artifactId> <version>2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <!-- Fat jar; --> <artifactId>maven-assembly-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> <configuration> <archive> <manifest> <mainClass>com.pi.rasberri.Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins>
uj5u.com熱心網友回復:
我沒有 Raspberry Pi 來進行最終測驗,但我想我找到了一個解決方法。
使用這個答案,我設法2021-10-30-raspios-bullseye-armhf-lite.img在 Docker 中運行。我使用您的代碼構建了一個專案并復制了所有需要的檔案并嘗試啟動它(mypiapp我的 jar 包含您的主類):
java -cp mypiapp-0.0.1-SNAPSHOT.jar:lib/pi4j-core-2.1.1.jar:lib/pi4j-library-pigpio-2.1.1.jar:lib/pi4j-plugin-pigpio-2.1.1.jar:lib/pi4j-plugin-raspberrypi-2.1.1.jar:lib/slf4j-api-1.7.32.jar:lib/slf4j-simple-1.7.32.jar com.github.fwi.mypiapp.MyPiApp
然后給了我錯誤:
[main] ERROR com.pi4j.library.pigpio.util.NativeLibraryLoader - Unable to load [libpi4j-pigpio.so] using path: [/lib/armhf/libpi4j-pigpio.so]
java.lang.UnsatisfiedLinkError: /tmp/libpi4j-pigpio1770932771276400506.so: libpigpio.so.1: cannot open shared object file: No such file or directory
這是一個無賴。但至少沒有ProviderNotFoundException(我也可以使用“具有依賴項的胖罐子”進行復制)。長 java-command 似乎可以防止發生該例外。
我可以通過運行(在 Raspbian 中)來稍微改善這種情況
apt install pigpio
現在長java命令顯示:
[main] WARN com.pi4j.library.pigpio.impl.PiGpioNativeImpl - PIGPIO ERROR: PI_INIT_FAILED; pigpio initialisation failed
這是在 Docker 容器中運行時所預期的。但至少找到并加載了本機庫。
現在是可能解決“胖罐”問題的解決方法。我們將從 Spring 借用一些 Maven 設定和代碼。Spring 也可以構建 fat-jars 并且它有點復雜但也更好。pom 現在看起來像(在需要的地方用你的專案名稱更新):
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.1</version>
</parent>
<groupId>com.github.fwi</groupId>
<artifactId>mypiapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
<start.class>com.github.fwi.mypiapp.MyPiApp</start.class>
<pi4j.version>2.1.1</pi4j.version>
</properties>
<dependencies>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-raspberrypi</artifactId>
<version>${pi4j.version}</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-plugin-pigpio</artifactId>
<version>${pi4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${start.class}</mainClass>
<layout>ZIP</layout>
<executable>false</executable>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
只需運行mvn clean package即可構建 fat-jar。java -jar mypiapp-fat.jar在我的情況下運行它,它給出了與使用長 java 命令運行時完全相同的錯誤(即沒有ProviderNotFoundException)。所以我認為這可以在真正的 Raspberry-Pi 上作業(apt install pigpio盡管可能仍然需要)。
一些注意事項:
- Spring 的父級設定了一些很好的 Maven 默認值,并且還為
spring-boot-maven-plugin - Java 版本必須使用該
java.version屬性進行設定。 start.class使用包含您的main-method的類的名稱進行更新。pom.xml將拉入所有其他依賴項中的 3 個依賴項(依賴項的依賴項)- Spring 檔案參考:打包和嵌套 jars 啟動器選項。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/386408.html
上一篇:默認情況下使TextInputEditText成為焦點
下一篇:在子pom中使用依賴管理
