我正在尝试在 Raspberry Pi 模型 3 上运行 java 代码,从 PC eclipse 开发环境下载以使用 pi4j 库访问 I2C 总线上的 9DoF 设备。我收到以下错误:
java -classpath .:classes:/opt/pi4j/lib/'*' -jar /home/pi
/artifacts/RPITank-1.0-SNAPSHOT.jar Error: A JNI error has occurred, please check your installation and try again in 异常线程“主”java.lang.NoClassDefFoundError:com/pi4j/io/i2c/I2CFa
java.lang.Class.privateGetDeclaredMethods(Class.java:2701) 的 java.lang.Class.getDeclaredMethods0(Native Method) 的 ctory$UnsupportedBusNumberException java.lang.Class.privateGetMethodRecursive(Class.java:3048) 的 java.lang。 java.lang.Class.getMethod(Class.java:1784) 的 Class.getMethod0(Class.java:3018) 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