I'm making a small program to listen for the IR signal of my television remote's volume up/down, then rebroadcast to my A/V system, so I don't need two remotes.
To do this, I have java listening for the command (and eventually connecting to the internet to receive commands), but I need to use C to use a library for controlling the raspberry pi's GPIO fast enough to generate the IR signal. I've got both of these programs working independently, but I'm having troubles using JNI to call the C program from Java.
---For no background, start here---
When I run my java program normally, the connection between programs is made fine, but it cannot run the C portion of the code without being root.
However, when I run it as root, I get the "java.lang.UnsatisfiedLinkError" for the Sender library. Here are the commands I've been using for compiling/linking everything:
javac -cp /opt/pi4j/lib/*:/home/pi/Desktop/IRTranslator Translator.java
javah Translator
gcc -fPIC -Wall -pthread -o Sender -c Sender.c -lpigpio -lrt -I/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/
gcc Sender -shard -o libSender.so -lpigpio -lrt -Wl,-soname,Sender
java -cp /opt/pi4j/lib/*:. Translator
In my java code I have:
static { System.loadLibrary("Sender"); }
Again, all of this works fine without running my code as
sudo java -cp /opt/pi4j/lib/*:. Translator
Other (potentially) relevant information:
I've made symlinks for jni_md.h and jawt_md.h, which is why the -I portion of my compilation doesn't reference to the linux subdirectory as well.
I've exported LD_LIBRARY_PATH as "/home/pi/Desktop/IRTranslator", which is the location of all of my code, etc.
Thanks for reading over this, it got longer that I intended!
sudo
doesn't pass all of your environment variables to the command it runs (primarily for security reasons). So you'll need to pass your LD_LIBRARY_PATH explicitly
sudo LD_LIBRARY_PATH=$LD_LIBRARY_PATH java -cp /opt/pi4j/lib/*:. Translator