简体   繁体   中英

How to compile and run Java program with JNI GPIO native library in Raspberry pi

I would like to inform you that one of employer has created a java based program which communicates via I2C communication with Raspberry pi.

He has created some of port methods(open,write,close) in c++ and he used JNI library to make a bridge with his java based program.So in this case we are able to communicate with embedded card via java using c++ native library(JNI) bridge.

So our main program is written in Java and bridged with c++(JNI) methods to communicate with GPIO.

We are using Raspberry pi to control this embedded card and java works on Raspberry.

he has created main java class and a library which is written in c++.We are able to add this library to java main program using Netbeans ide.

Recently, this projects was assigned to me and I improved this project and added user interface using javafx.

In this case my java(include javafx) program is working well on raspberry pi as long as c++ Jnı library commands are disabled.

I couldn't set to run these c++ JNI codes and my java program together , When I enable to these c++ JNI related commands in my java program .

Netbeans build well all together but it gives me an error when I run these codes in Raspberry or PC

java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) Caused by: java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: /puttolightfx/RaspberryPINativeCLibrary/dist/Debug/GNU -Linux-x86/libRaspberryPINativeCLibrary.so at java.lang.Runtime.load0(Runtime.java:806) at java.lang.System.load(System.java:1086) at base.RaspberryPINativeJavaLibraryBaseClass.LoadLibrary(RaspberryPINativeJavaLibraryBaseClass.java:30) at base.RaspberryPINativeJavaLibraryBaseClass.(RaspberryPINativeJavaLibraryBaseClass.java:19) at puttolightfx.SignalLamp.(SignalLamp.java:186) at puttolightfx.PutToLightFX.main(PutToLightFX.java:149) ... 11 more

My question is;

1)How can I compile these c++ JNI related codes and run in Raspberry ?

Basically I am using scriptshell to run this java and javafx based program.But it is not working when c++ JNI is enabled.

which looks like this ;

/usr/lib/jvm/jdk-8-oracle-arm-vfp-hflt/jre/bin/java
-Dfile.encoding=UTF-8 -Djava.ext.dirs=/opt/armv6hf-sdk/rt/lib/ext
-Djava.awt.headless=true
-jar /home/root/NetBeansProjects//PutTo/dist/PutTo.jar

您可以使用此库http://pi4j.com,它支持I2C通信。

Based on this part of the stacktrace.

java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library:
/puttolightfx/RaspberryPINativeCLibrary/dist/Debug/GNU-Linux-x86/libRaspberryPINativeCLibrary.so

You should check if the library is at this location.

What does ldd libRaspberryPINativeCLibrary.so return? Is there maybe a dynamic linked library missed?

From the stacktrace:

/puttolightfx/RaspberryPINativeCLibrary/dist/Debug/GNU-Linux-x86/libRaspberryPINativeCLibrary.so

from your code in the comment

/projects /puttolightfx/RaspberryPINativeCLibrary/dist/Debug/GNU-Li‌​nux-x86/libRaspberryPINativeCLibrary.so

Do you checked already the difference in the path?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM