linux 64bit - jni linking error - java

I'm trying to get a working example of calling Java from C using JNI on ubuntu 64bit.
The code from: calling java function from c using jni I am compiling using:
gcc test.c -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/linux -c
gcc test.o -L/usr/lib/jvm/java-1.7.0-openjdk/amd64/jre/lib/amd64/server/ -ljvm -o jniTest
This doesnt generate any errors but if I try ldd jniTest I get the following:
linux-vdso.so.1 => (0x00007fffe55d5000)
libjvm.so => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f85f2928000)
/lib64/ld-linux-x86-64.so.2 (0x00007f85f2d0a000)
And if I try ./jniTest it gives this error:
test.o: In function `create_vm':
test.c:(.text+0x35): undefined reference to `JNI_CreateJavaVM'
collect2: error: ld returned 1 exit status

With the -L/usr/lib/jvm/java-1.7.0-openjdk/amd64/jre/lib/amd64/server/ you tell the linker where to find libjvm.so. At runtime you will need to do the same (http://linux.die.net/man/8/ld.so). And you'll also need the parent directory because other needed libs are in there. One way to do this is by specifying it on the command line: LD_LIBRARY_PATH=/usr/lib/jvm/java-1.7.0-openjdk/amd64/jre/lib/amd64/server:/usr/lib/jvm/java-1.7.0-openjdk/amd64/jre/lib/amd64 ./jniTest.
A way to get around the need to specify the path is to not link to libjvm.so at all but to find it at runtime and then use the functions in dlfcn.h (http://linux.die.net/man/3/dlopen, http://linux.die.net/man/3/dlsym) to use it.

Related

Jlibfprint cross compilation

I'm trying to cross-compile the JNI Bridge Jlibfprint using buildroot.
This is the source code.
Here is the .mk file:
JLIBFPRINT_VERSION = 775295f08174d11d39da9e327120b128ef270fc4
JLIBFPRINT_SITE = $(call github,eduardobogoni,jlibfprint,$(JLIBFPRINT_VERSION))
JLIBFPRINT_AUTORECONF = YES
JLIBFPRINT_SUBDIR = JlibFprint_jni
#JLIBFPRINT_CONF_OPTS = --enable-debug-log --enable-udev-rules=no --disable-debug-log
JLIBFPRINT_INSTALL_STAGING = YES
define PREPARE_FILES_AND_FIX_DIRECTORY
cd $(#D)/JlibFprint_jni && \
libtoolize
endef
JLIBFPRINT_PRE_CONFIGURE_HOOKS += PREPARE_FILES_AND_FIX_DIRECTORY
$(eval $(autotools-package))
Extract, Patch, Update, Configure, Autoreconfiguring, Patching libtool run without problem.
Then it failed at Building stage with this:
libfprint_jni_la-jlibfprint_JlibFprint.o
In file included from jlibfprint_Device.cpp:1:0:
jlibfprint_Device.h:5:10: fatal error: jni.h: No such file or directory
#include <jni.h>
^~~~~~~
compilation terminated.
Then I added, these includes to the /JlibFprint_jni/src/Makefile.am
ADD_INCLUDES=-I/usr/lib/jvm/java-11-openjdk-amd64/include -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux -I/home/mariano/buildroot/output/build/libfprint-0.7.0 -I/home/mariano/buildroot/output/build/libfprint-0.7.0/libfprint/nbis/include
And now it fails with:
arm-linux-gnueabihf-g++: ERROR: unsafe header/library path used in cross-compilation: '-I/usr/lib/jvm/java-11-openjdk-amd64/include'
arm-linux-gnueabihf-g++: ERROR: unsafe header/library path used in cross-compilation: '-I/usr/lib/jvm/java-11-openjdk-amd64/include'
libtool: compile: /home/mariano/buildroot/output/host/bin/arm-linux-gnueabihf-g++ -DPACKAGE_NAME=\"libfprint_jni\" -DPACKAGE_TARNAME=\"libfprint_jni\" -DPACKAGE_VERSION=\"1.0\" "-DPACKAGE_STRING=\"libfprint_jni 1.0\"" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"libfprint_jni\" -DVERSION=\"1.0\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_STRING=1 -DHAVE_IOSTREAM=1 -DHAVE_LIBFPRINT_FPRINT_H=1 -DHAVE_DLFCN_H=1 -DLT_OBJDIR=\".libs/\" -I. -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/lib/jvm/java-11-openjdk-amd64/include -I/usr/lib/jvm/java-11-openjdk-amd64/include/linux -I/home/mariano/buildroot/output/build/libfprint-0.7.0 -I/home/mariano/buildroot/output/build/libfprint-0.7.0/libfprint/nbis/include -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -Os -c jlibfprint_JlibFprint.cpp -fPIC -DPIC -o .libs/libfprint_jni_la-jlibfprint_JlibFprint.o
Makefile:496: recipe for target 'libfprint_jni_la-jlibfprint_Device.lo' failed
Jlibfprint requires a JDK, but Buildroot doesn't have a JDK package. Therefore, building Jlibfprint using Buildroot will never work.
I downloaded JDK for ARM 32 bits and addded the includes into that folder.
I removed the BR2_COMPILER_PARANOID_UNSAFE_PATH=n as Arnout said it was wrong. And it cross-compiled without issues.
I ran the Sample Run and it worked with an Upek fingerprint scanner.
I am working on this now. Buildroot includes openjdk, but does not install the jni header files, so when using staging as sysroot for cross compilation, things go wrong.
As far as I can tell the jni headers (jni.h and jni_md.h) serve as type translations only and can be included without problem for the target. I added them to the sysroot and compiling works without issues.

fatal error: jni.h: No such file or directory Netbeans IDE

I want to learn that how we can call a java function from C++,after following some links i came to know that we need to add following header file in C++ file
#include <jni.h>
but when I added this file in C++ file in Netbeans IDE I got following error
fatal error: jni.h: No such file or directory
How can this error be removed, I am just beginner in JNI.
I went through previous questions asked at stackoverflow but was not able to go through the Problem.
Here is my comile output:
"/C/MinGW/MSYS/1.0/bin./make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/c/Users/Simer/Documents/NetBeansProjects/JniDemo'
"/C/MinGW/MSYS/1.0/bin./make.exe" -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/jnidemo.exe
make.exe[2]: Entering directory `/c/Users/Simer/Documents/NetBeansProjects/JniDemo'
mkdir -p build/Debug/MinGW-Windows
rm -f "build/Debug/MinGW-Windows/main.o.d"
g++ -c -g -MMD -MP -MF "build/Debug/MinGW-Windows/main.o.d" -o build/Debug/MinGW-Windows/main.o main.cpp
main.cpp:9:17: fatal error: jni.h: No such file or directory
#include <jni.h>
^
compilation terminated.
make.exe[2]: *** [build/Debug/MinGW-Windows/main.o] Error 1
make.exe[2]: Leaving directory `/c/Users/Simer/Documents/NetBeansProjects/JniDemo'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/c/Users/Simer/Documents/NetBeansProjects/JniDemo'
make.exe": *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 2s)
You are compiling with MinGW without including Java headers, thats why it complains about not found
Try to add this option to your compilation command -I"c:/path/to/jdk/include" -I"c:/path/to/jdk/include/win32" -D__int64="long long".
The last option is to avoid the error caused because windows compiler uses the type __int64 and gcc uses long long.
Have you looked at the Netbeans JNI tutorial?
It goes through everything you should need to do in pretty good detail.

Compiling JNI Example

I am trying to use JNI to call a Java program from a C++ program. A supplier has put in place some phone apparatus and it only uses C++. I want to write a Screen Popper but don't want to do it in C++ so therefore want to pass across the event of the phone call coming in and the number to a Java routine to handle. I am using Netbeans as it makes the coding easier (usually!). I have not done C++ before which is why I want to escape to Java ASAP!
I am following instructions here: http://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html
Which seem to set out how to call a Java routine from C++ but I cannot get it to compile. I have got as far as I with lots of help from this site but now I'm stumped.
I have added the '-D__int64=long long' switch to the compiler options for the project and added the Java libraries to the Application include paths. I have reparsed the code using code assistance. But now I'm left with this error:
make[1]: Entering directory '/cygdrive/f/Javadev/Sample2'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin_4.x-Windows/sample2.exe
make[2]: Entering directory '/cygdrive/f/Javadev/Sample2'
mkdir -p build/Debug/Cygwin_4.x-Windows
rm -f build/Debug/Cygwin_4.x-Windows/main.o.d
g++.exe -D__int64=long long -c -g -I/cygdrive/C/Program\ Files\ \(x86\)/Java/jdk1.6.0_33/include/win32 -I/cygdrive/C/Program\ Files\ \(x86\)/Java/jdk1.6.0_33/include -MMD -MP -MF build/Debug/Cygwin_4.x-Windows/main.o.d -o build/Debug/Cygwin_4.x-Windows/main.o main.cpp
nbproject/Makefile-Debug.mk:65: recipe for target 'build/Debug/Cygwin_4.x-Windows/main.o' failed
make[2]: Leaving directory '/cygdrive/f/Javadev/Sample2'
nbproject/Makefile-Debug.mk:58: recipe for target '.build-conf' failed
make[1]: Leaving directory '/cygdrive/f/Javadev/Sample2'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
g++: error: long: No such file or directory
make[2]: *** [build/Debug/Cygwin_4.x-Windows/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
All source files are under F:\javadev, Sample2 is C++ and Sample3 is Java. Is there something else I need to include from Cygwin? It seems to say there is a problem with 'long' but that only really occurs in the switch and without it it fails even more catastrophically, saying that __int64 does not have a type.

undefined symbol: _ZTTN2cv4SURFE

I want to call C++ code from java using JNI. I already followed many tutorials on this, and all are working, when the C++ code is "simple". But the C++ code which I am going to call inludes the opencv library and uses various functions of it. And this is, where I get problems...
What I am doing is the following:
1.) g++ -fPIC -shared -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include/linux -o libHello.so Hello.cpp
which creates the .so file.
2.) I copy my created .so file to the directory where I use it, just as I did with the tutorials, and load it:
static {
System.loadLibrary("Hello");
}
But I get an UnSatisfiedLinkError with undefined symbol: _ZTTN2cv4SURFE
Most probably there is an error in the first step, such that it can not find my opencv library. But I don't know what the correct command would be.
ldd -d libHello.so produces:
linux-gate.so.1 => (0xb777d000)
libstdc++.so.6 => /usr/lib/i386-linux-gnu/libstdc++.so.6 (0xb765e000)
libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xb7640000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb7496000)
libm.so.6 => /lib/i386-linux-gnu/libm.so.6 (0xb746a000)
/lib/ld-linux.so.2 (0xb777e000)
undefined symbol: _ZTTN2cv4SURFE (./libHello.so)
undefined symbol: _ZTV11CvStatModel (./libHello.so)
undefined symbol: _ZTVN2cv24BackgroundSubtractorMOG2E (./libHello.so)
undefined symbol: _ZTVN2cv4SURFE (./libHello.so)
undefined symbol: _ZTV5CvSVM (./libHello.so)
edit:
For anyone else getting the same problem:
1.) As some people mentioned, I was not linking against the opencv library.
2.) But this was not enough. I was using this command, as it is also done in some tutorials:
g++ -shared -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include/linux -I/usr/local/include/opencv -L/usr/local/lib -lopencv_nonfree -lopencv_features2d -lopencv_core -lopencv_highgui Hello.cpp -o libHello.so
which caused the undefined symbol error when doing "ldd"
I changed the command to:
g++ Hello.cpp -shared -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include -I/usr/lib/jvm/java-1.7.0-openjdk-i386/include/linux -I/usr/local/include/opencv -L/usr/local/lib -lopencv_nonfree -lopencv_features2d -lopencv_core -lopencv_highgui -o libHello.so
i.e. I just wrote Hello.cpp directly after g++ instead at the end. And this solved the problem.
I don't know why this is working and the previos one didn't. But maybe someone can tell me.
You may need to specify a path to your additional libraries so the JVM can load them. Add an argument to Java:
-Djava.library.path=/path/to/opencv/libs:/other/paths/as/needed
This argument must come before your class name so that Java knows it is an argument for the JVM and not for your program.

How to build javafuse on Ubuntu?

I'm trying to implement the JavaFuse project but when I execute the make command I'm getting this error:
gcc -shared build/contextClass.o build/conninfoClass.o build/javafuse_jni.o build/statvfsClass.o build/fsClass.o build/utimebufClass.o build/fileinfoClass.o build/statClass.o -o build/libjavafuse.so
Error:
build/contextClass.o: file not recognized: File format not recognized
collect2: ld returned 1 exit status
make: * [libjavafuse.so] Error 1
I'm using Ubuntu.
Could anyone please suggest what is the problem?
I recommend fuse-jna, it's a project that I've used and it doesn't need to be compiled and also, it's alive and you may find it here fuse-jna. hope it'll help you

Categories

Resources