我正在尝试在Raspberry模型3上运行java代码,该模型是从PC eclipse开发环境下载的,通过pi4j库访问pi4j总线上的一个9 9DoF设备。我得到以下错误:
类:/ -classpath /pi4j/lib/‘*’-jar /home/pi /工件/RPITank-1.0-SNAPSHOT.jar错误: JNI错误发生,请检查您的安装,然后在线程"main“java.lang.NoClassDefFoundError: com/pi4j/io/i2c/I2CFa中再次尝试异常 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 )的java.lang.Class.getDeclaredMethods0(原生方法)异常在sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)引起的: java.lang.ClassNotFoundException: com.pi4j.io.i2c.I2CFactory$Unsuppor 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
发布于 2016-10-29 12:05:49
问题是,当项目jar被转移到Raspberry时,没有获得它与pi4j软件的运行时链接,RPi预装好了这个问题,多亏了natdan,通过关于github的问题解决了这个问题。
对项目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" />https://stackoverflow.com/questions/40287070
复制相似问题