java - NoClassDefFound错误: com/pi4j/io/I2CFactory$UnsupportedBusNumberException

标签 java raspberry-pi3 i2c pi4j

我正在尝试在 Raspberry Pi model 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 Exception in thread "main" 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) at java.lang.Class.getMethod0(Class.java:3018) at java.lang.Class.getMethod(Class.java:1784) at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544 ) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526) Caused by: 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 more

这是代码

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

最佳答案

问题是项目 jar 传输到 Raspberry Pi 时没有获取到 RPi 上预安装的 pi4j 软件的运行时链接,问题已通过 an issue on github here 解决。感谢纳丹。

对项目 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>

经过进一步的实验,发现这个依赖关系并不是必需的,所以忽略上面的部分。

然后将类路径作为 list 条目添加到 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" />

关于java - NoClassDefFound错误: com/pi4j/io/I2CFactory$UnsupportedBusNumberException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40287070/

相关文章:

java - 使用 hashmap 和 arraylist 查找数字的因子

java - jnlp-文件同时用于 Java Webstart 和 Java Applet

c - i2c_msg.buf 的大小可以有多大?

c++ - 这段代码是如何工作的,它叫什么

Arduino 作为具有多个 i2c 地址的从机

java - 无法在java中生成64位长值

java - 错误 404 : css page can't load in JSP

python - 使用python和Opencv进行多处理或多线程以检测人脸

camera - 树莓派 3B+ : Camera V1. 3 不工作

python - 如何使用python将传感器数据输出保存到excel中