我正在尝试在Raspberry Pi模型3上运行java代码,从PC eclipse开发环境下载,使用pi4j库访问I2C总线上的9DoF设备。我收到以下错误:
java -classpath。:classes:/ opt / pi4j / lib /' *' -jar / home / pi
/artifacts/RPITank-1.0-SNAPSHOT.jar错误:发生了JNI错误, 请检查您的安装并再次尝试线程中的异常 "主" java.lang.NoClassDefFoundError:com / pi4j / io / i2c / I2CFa
ctory $ UnsupportedBusNumberException at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.privateGetMethodRecursive(Class.java:3048) 在java.lang.Class.getMethod0(Class.java:3018) 在java.lang.Class.getMethod(Class.java:1784) at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544 ) 在sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526) 引起:java.lang.ClassNotFoundException: com.pi4j.io.i2c.I2CFactory $ Unsuppor
tedBusNumberException at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ......还有7个
这是代码
package main;
import java.io.IOException;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CFactory;
import devices.I2C.Pi4jI2CDevice;
import devices.sensorImplementations.MPU9250.MPU9250;
public class MPU9250Test {
public static void main(String[] args)
{
I2CBus bus = null;
System.out.println("Attempt to get Bus 1");
try {
final GpioController gpio = GpioFactory.getInstance();
bus = I2CFactory.getInstance(I2CBus.BUS_1);
System.out.println("Got Bus, create devices");
MPU9250 mpu9250 = new MPU9250(
new Pi4jI2CDevice(bus.getDevice(0x68)), // MPU9250 I2C device
new Pi4jI2CDevice(bus.getDevice(0x0C)), // ak8963 I2C
100, // sample rate
100); // sample size
Thread sensor = new Thread(mpu9250);
sensor.start();
Thread.sleep(10000);
sensor.interrupt();
for(int i = mpu9250.getAccelerometerReadingCount() -1; i>0; i--)
{
System.out.print("G: " + mpu9250.getRotationalAcceleration(i).toString());
System.out.print(" A: " + mpu9250.getAcceleration(i).toString());
System.out.println(" M: " + mpu9250.getGaussianData(i).toString());
}
} catch (I2CFactory.UnsupportedBusNumberException | InterruptedException | IOException e) {
e.printStackTrace();
}
}
}
我已经使用I2Cdetect -y 1检查了设备在总线1上是否可见,这显示了一个地址为0x68和0x76的设备。
我不知道这是执行环境或代码的问题,欢迎任何帮助。
进一步的实验表明,删除异常处理程序并不是编译时需要的选项。此处描述了异常类 http://pi4j.com/apidocs/com/pi4j/io/i2c/I2CFactory.UnsupportedBusNumberException.html
答案 0 :(得分:1)
问题在于,当转移到Raspberry Pi时,项目jar没有获得预先安装在RPi上的pi4j软件的运行时链接,问题是通过an issue on github here解决的,这归功于natdan。
对项目pom.xml所做的更改如下:
将运行时依赖项添加到依赖项
<dependencies>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-native</artifactId>
<version>1.2-SNAPSHOT</version>
<classifier>raspberrypi-dynamic</classifier>
<type>so</type>
</dependency>
经过进一步的实验,结果发现毕竟不需要这种依赖,所以请忽略上面的部分。
然后将类路径作为清单条目添加到maven jar插件配置中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!--<classpathPrefix>${pi.pi4j.Directory}/</classpathPrefix>-->
<mainClass>${pi.main.class}</mainClass>
</manifest>
<manifestEntries>
<!-- Add the pi4j in runtime. -->
<Class-Path>${pi.pi4j.Directory}/pi4j-core.jar</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
最后,类路径已从antrun部分的
中的java命令中删除 <!-- run the JAR file on the Raspberry Pi -->
<sshexec host="${pi.host}" port="${pi.port}" username="${pi.user}"
password="${pi.password}" trust="true" failonerror="false"
verbose="true"
command="java -jar ${pi.deployDirectory}/${project.build.finalName}.jar" />