Compiling JNI Example - java

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.

Related

How to fix an error : 'Could not load library 'toscomm': no toscomm in java.library.path'?

I am getting familiar with Tiny OS enviroment, currently I am at lesson 4 "Mote-PC serial communication and SerialForwarder"
and I am stuck with the part where I have to listen to serial port for any incoming data. I am trying to implement a simple example where I need to use TestSerial script to listen for any incoming messages however when I run
java TestSerial -comm serial#/dev/ttyUSB0:iris
my output looks like that:
Error loading the TinyOS JNI libraries the conventional way!
Attempting to load library 'toscomm'
Could not load library 'toscomm': no toscomm in java.library.path
----------
java.lang.UnsatisfiedLinkError: no toscomm in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1865)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1119)
at net.tinyos.util.TOSLibraryLoader.load(TOSLibraryLoader.java:85)
at net.tinyos.comm.TOSCommJNI.<clinit>(TOSCommJNI.java:14)
at net.tinyos.comm.NativeSerial.getTOSCommMap(NativeSerial.java:207)
at net.tinyos.comm.TOSSerial.<init>(TOSSerial.java:277)
at net.tinyos.packet.SerialByteSource.openStreams(SerialByteSource.java:67)
at net.tinyos.packet.StreamByteSource.open(StreamByteSource.java:61)
at net.tinyos.packet.Packetizer.openSource(Packetizer.java:149)
at net.tinyos.packet.AbstractSource.open(AbstractSource.java:78)
at net.tinyos.packet.Packetizer.open(Packetizer.java:142)
at net.tinyos.packet.PhoenixSource.run(PhoenixSource.java:178)
----------
In order to load the library 'toscomm' Java tries to locate the file 'libtoscomm.so' in one of the following paths:
/usr/java/packages/lib/i386:/lib:/usr/lib
The operating system is 'Linux' (i386)
Trying to locate the file 'linux_x86_toscomm.lib' in the classpath
Temporary file created: '/tmp/toscomm3780740783398787473.lib'
Library copied successfully. Let's load it.
Library loaded successfully
serial#/dev/ttyUSB0:57600: resynchronising
Sending packet 0
Sending packet 1
instead of having
Sending packet 1
Received packet sequence number 4
Sending packet 2
Received packet sequence number 5
as it described in tutorial
I am concerned about the warnings I get when I run this script, particularly this one
Attempting to load library 'toscomm'
Could not load library 'toscomm': no toscomm in java.library.path
According to tutorial, this error can be resolved by running
sudo tos-install-jni,
however when I type this, I get the error that tos command not found.
I tried to go to tinyos-2.x/tools/tinyos/java. directory and typed sudo make
and sudo make install, nevertheless that did not work either giving me the following error:
make[1]: Entering directory '/home/wsn/tinyos-main/tools'
/bin/sh ./config.status --recheck
running CONFIG_SHELL=/bin/sh /bin/sh ./configure --no-create --no-recursion
checking for a BSD-compatible install... /bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking whether gcc understands -c and -o together... yes
checking for style of include used by make... GNU
checking dependency style of gcc... gcc3
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking dependency style of g++... gcc3
checking for ranlib... ranlib
checking for a Python3 interpreter with version >= 3.0... python3
checking for python3... /bin/python3
checking for python3 version...
checking for python3 platform... linux
checking for python3 script directory... ${prefix}/lib/python3.4/site-packages
checking for python3 extension module directory... /lib/python/site-packages
checking for a Python interpreter with version >= 2.5... python2
checking for python2... /bin/python2
checking for python2 version... 2.7
checking for python2 platform... linux2
checking for python2 script directory... ${prefix}/lib/python2.7/site-packages
checking for python2 extension module directory... (cached) /lib/python/site-packages
checking for perl... /bin/perl
checking for python2... /bin/python2
checking for python3... /bin/python3
checking for python2.7-config... /bin/python2.7-config
checking for dot... no
configure: I can't find dot. dot is required for documentation.
checking for nescc... /bin/nescc
checking for cygwin... no
checking for JDK location... configure: error: java not found
Makefile:377: recipe for target 'config.status' failed
make[1]: *** [config.status] Error 1
make[1]: Leaving directory '/home/wsn/tinyos-main/tools'
Makefile:374: recipe for target '../../config.status' failed
make: *** [../../config.status] Error 2
When I try to run the same command for BaseStation script, the program gives the same warning and just gets frozen at this point:
serial#/dev/ttyUSB0:57600: resynchronising
So I don't even see any packets sent or received!
I am not sure what is the problem here, please help.
I am using Virtual Machine with Fedora OS
I am trying to run the program on mib520 platform and I use iris motes
my java version
openjdk version "1.8.0_31"
OpenJDK Runtime Environment (build 1.8.0_31-b13)
OpenJDK Server VM (build 25.31-b07, mixed mode)
I do not think your issue is with toscomm, as long as the library ends up being loaded successfully you should not be getting any issues. I always used to get the same exception that you are seeing being thrown over there everytime I used the serial but never bothered to fix it as it didn't really hamper the operation of the serial port.
In the end, I got rid of the exception by copying libtoscomm from the {TOS_ROOT}/tools/tinyos/java/serial/ directory to one of the directories that TinyOS is looking for libraries in. In your case you would put a copy of the libtoscomm file in one of the directories /usr/java/packages/lib/i386 , /lib or /usr/lib.
You can try doing that and seeing if that resolves the exception issue for you.
What I suspect might be causing your issue is that you do not have TestSerialAppC loaded onto you mote and connected to the serial port. Have you made sure the mote is connected to the serial port?
I loaded ran the application on a mote and got the output below, so the issue is not with TestSerialAppC
serial#/dev/tty.usbserial-FTYRYAHT:115200: resynchronising
Sending packet 0
Received packet sequence number 3
Sending packet 1
Received packet sequence number 4
Sending packet 2

rJava: non-zero exit status

I have been trying to install rJava using the following commands:
install.packages('rJava',,'http://www.rforge.net/')
or
install.packages('rJava', type = 'source', INSTALL_opts='--merge-multiarch')
but each time I fail due to this error:
WARNING: JRI could NOT be built Set IGNORE=1 if you want to build
rJava anyway. ERROR: configuration failed for package 'rJava'. I have uninstalled and re-installed both version of java on my machine but still no help!
Does anyone has a solution ?
the full description is as below:
install for i386
installing source package 'rJava' ...
** package 'rJava' successfully unpacked and MD5 sums checked Generate Windows-specific files (src/jvm-w32) ... make: Entering directory
'/Users/ahkay/AppData/Local/Temp/RtmpKalUob/R.INSTALL192609e43b16/rJava/src/jvm-w32'
C:/Rtools/mingw_32/bin/dlltool --as C:/Rtools/mingw_32/bin/as
--input-def jvm.def --kill-at --dllname jvm.dll --output-lib libjvm.dll.a C:/Rtools/mingw_32/bin/gcc -O2 -c -o findjava.o
findjava.c C:/Rtools/mingw_32/bin/gcc -s -o findjava.exe findjava.o
make: Leaving directory
'/Users/ahkay/AppData/Local/Temp/RtmpKalUob/R.INSTALL192609e43b16/rJava/src/jvm-w32'
Find Java... ./configure.win: line 15: [: too many arguments
JAVA_HOME=C:/Program Files/Java/jre1.8.0_202
=== Building JRI === configure.win: line 21: [: too many arguments JAVA_HOME=C:/Program Files/Java/jre1.8.0_202
R_HOME=C:/PROGRA~1/R/R-35~1.2 Creating Makefiles ... Configuration
done. make -C src JRI.jar make[1]: Entering directory
'/Users/ahkay/AppData/Local/Temp/RtmpKalUob/R.INSTALL192609e43b16/rJava/jri/src'
C:/Program Files/Java/jre1.8.0_202/bin/javac -d . ../RList.java
../RBool.java ../RVector.java ../RMainLoopCallbacks.java
../RConsoleOutputStream.java ../Mutex.java ../Rengine.java
../REXP.java ../RFactor.java ../package-info.java make[1]: C:/Program:
Command not found make[1]: * [Makefile.all:41:
org/rosuda/JRI/Rengine.class] Error 127 make[1]: Leaving directory
'/Users/ahkay/AppData/Local/Temp/RtmpKalUob/R.INSTALL192609e43b16/rJava/jri/src'
make: * [Makefile.all:19: src/JRI.jar] Error 2
**** WARNING: JRI could NOT be built Set IGNORE=1 if you want to build rJava anyway. ERROR: configuration failed for package 'rJava'
removing 'C:/Users/ahkay/Documents/R/win-library/3.5/rJava' In R CMD INSTALL In R CMD INSTALL Warning in install.packages : installation
of package ‘rJava’ had non-zero exit status
I have experienced same issue and following approach worked for me in my Mac OS 10.13.6, you can apply minor OS related changes if using in other OS.
OS:
Installing rJava Manually
# curl -#ROL https://www.rforge.net/rJava/snapshot/rJava_0.9-8.tar.gz
# R CMD INSTALL rJava_0.9-8.tar.gz
# su -
Location you have installed R software, you might have different location.
# cd /Library/Frameworks/R.framework/Versions/3.5/Resources/lib
I found that different Java used by R, and forcing by softlink:
#ln -s /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home/lib/server/libjvm.dylib libjvm.dylib
Remember, you need to put your JAVA_HOME in the following location in R Studio:
>Sys.setenv(JAVA_HOME = "/Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home")
Verifying rJava working in R Studio:
>require(rJava)

JDK9 NetBeans Dev - compile error during JNI Tutorial

I have searched the entire web throughly for almost two days now and couldn't come up with a solution to my problem. I hope you guys can help me.
This might be a little specific but I am sure sooner or later others will experience the same problems.
I am currently using NetBeans Dev (Build 201803200002) with Java JDK 9.0.4 on a Windows 10, 64-bit system.
I was working through this JNI tutorial:
https://cnd.netbeans.org/docs/jni/beginning-jni-win.html
Almost at the end, below figure 13, you are supposed to build the project HelloWorldNative (in my case it is called JNITestNative).
But I always get the following error message:
cd 'D:\NetBeans\projects\JNITestNative'
C:\Programme\cygwin\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/d/NetBeans/projects/JNITestNative'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/JNITestNative.dll
make[2]: Entering directory '/cygdrive/d/NetBeans/projects/JNITestNative'
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/JNITest.o.d"
gcc -mno-cygwin -Wl,--add-stdcall-alias shared -m32 -c -g -I/cygdrive/C/Program\ Files/Java/jdk-9.0.4/include -I/cygdrive/C/Program\ Files/Java/jdk-9.0.4/include/win32 -MMD -MP -MF "build/Debug/Cygwin-Windows/JNITest.o.d" -o build/Debug/Cygwin-Windows/JNITest.o JNITest.c
gcc: error: shared: No such file or directory
gcc: error: unrecognized command line option '-mno-cygwin'; did you mean '-mno-clwb'?
nbproject/Makefile-Debug.mk:66: recipe for target 'build/Debug/Cygwin-Windows/JNITest.o' failed
make[2]: *** [build/Debug/Cygwin-Windows/JNITest.o] Error 1
make[2]: Leaving directory '/cygdrive/d/NetBeans/projects/JNITestNative'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/cygdrive/d/NetBeans/projects/JNITestNative'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
NetBeans seems to be missing a file which I am expecting it to generate in the first place - JNITest.o
I checked the directories ...jdk-9.0.4/include, ...jdk9.0.4/include/win32 and ...Debug/Cygwin-Windows.
All of them exist, but the files JNITest.o.d or JNITest.o do not.
I tried to replace "cygwin" by "clwb" as suggested in the second error message, but the first error still occurs.
According to some sources in the internet I also removed the whole part "-mno-cygwin" from the additional options but the first error still occurs.
According to some other sources in the internet I added my ...cygwin/bin directory to the include directories, but this did not improve anything.
I have absolutely no idea how to solve this or how to search for other potential solutions anymore.
I hope anyone here can help.
The correct answer was stated by Jorn Vernee in his comment.
A '-' was missing before the argument "shared".
Thank you for that.
For anyone else who might be experiencing this problem, let me sum up how I got from here to a successful build.
According to this and this I added three code lines for the definition of data type __int64, so now my JNITest.c looks like this:
#ifdef __CYGWIN__
#define __int64 long long
#endif
#include <jni.h>
#include <stdio.h>
#include "JNITest.h"
JNIEXPORT void JNICALL Java_com_plaettner_jnitest_JNITest_nativePrint
(JNIEnv *env, jobject obj)
{
printf("\nHello World from C\n");
}
Next I added the packages "mingw64-i686-gcc-core: GCC for Win32" and "mingw64-i686-gcc-g++: GCC for Win32" to my Cygwin.
In NetBeans options - C/C++ - Build Tools I then changed the C and C++ Compiler from gcc/g++ to i686-w64-mingw32-gcc/g++ respectively.
In the project properties in the categories Build - C Compiler/C++ Compiler I changed the additional options from "-mno-cygwin -Wl,--add-stdcall-alias -shared -m32" to "-Wl,--add-stdcall-alias -shared" and added my ...cygwin/bin directory to the include directories.
This leads to a successful build already. Anyway I added "-m32" to the additional options again and changed c/c++ Compiler back to gcc/g++ to keep compliance to the tutorial.
Now this leads to a successful build as well, although it didn't before.
Removing the ...cygwin/bin directory from the include directories leads to a failed build again and the whole process has to be repeated.
At least for me it worked this way.

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 Android JNI for OpenCV on Mac OSX

Bear with me - this is a long description, but I wanted to include all details. I'm trying use android NDK and JNI for building OpenCV Android apps.
I'm following this tutorial to get everything installed:
http://www.stanford.edu/class/ee368/Android/Tutorial-2-OpenCV-for-Android-Setup-Macintosh-API8.pdf
The following installed just fine:
/usr/bin/g++
/usr/local/bin/swig
/usr/bin/cmake
/usr/bin/make
/Developer/pcre-8.32/
Contents of my .bashrc file:
export NDK=/Users/Me/Code/Android/android-ndk-r4-crystax
export SDK=/Users/Me/Code/Android/adt-bundle-mac-x86_64-20130219/
export OPCV=/Users/Me/Code/OpenCV/opencv
export PATH=$NDK:$SDK/tools:$SDK/platform-tools:$PATH
export NDK_ROOT=$NDK
Making OpenCV libraries went just fine.
Due to complaint in using make, I had to make a small edit in /Users/Me/Code/OpenCV/opencv/android/android-jni/local.env.mk which looked like so:
#ANDROID_NDK_ROOT=$(HOME)/android-ndk-r4-crystax
ANDROID_NDK_ROOT=/Users/Me/Code/Android/android-ndk-r4-crystax
Then upon trying again to make in the android-jni, directory, disaster struck:
/Users/Me/Code/Android/android-ndk-r4-crystax/ndk-build OPENCV_CONFIG=../build/android-opencv.mk \
PROJECT_PATH= ARM_TARGETS="armeabi armeabi-v7a" V=
Gdbserver : [arm-eabi-4.4.0] /Users/Me/Code/OpenCV/opencv/android/android-jni/libs/armeabi/gdbserver
Gdbsetup : /Users/Me/Code/OpenCV/opencv/android/android-jni/libs/armeabi/gdb.setup
Gdbsetup : + source directory /Users/Me/Code/OpenCV/opencv/android/android-jni/jni
Gdbserver : [arm-eabi-4.4.0] /Users/Me/Code/OpenCV/opencv/android/android-jni/libs/armeabi-v7a/gdbserver
Gdbsetup : /Users/Me/Code/OpenCV/opencv/android/android-jni/libs/armeabi-v7a/gdb.setup
Gdbsetup : + source directory /Users/Me/Code/OpenCV/opencv/android/android-jni/jni
SharedLibrary : libandroid-opencv.so
/Users/Me/Code/Android/android-ndk-r4-crystax/build/prebuilt/darwin-x86/arm-eabi-4.4.0/bin/../lib/gcc/arm-eabi/4.4.0/../../../../arm-eabi/bin/ld: cannot find -lopencv_calib3d
collect2: ld returned 1 exit status
make[1]: *** [/Users/Me/Code/OpenCV/opencv/android/android-jni/obj/local/armeabi/libandroid-opencv.so] Error 1
make: *** [libs/armeabi-v7a/libandroid-opencv.so] Error 2
I thought perhaps this was a pkg-config error, but that appears to be set up as well, though in my old install of OpenCV. Below is the output of pkg-config --cflags --libs opencv:
-I/usr/include/opencv /usr/lib/libopencv_calib3d.dylib /usr/lib/libopencv_contrib.dylib /usr/lib/libopencv_core.dylib /usr/lib/libopencv_features2d.dylib /usr/lib/libopencv_flann.dylib /usr/lib/libopencv_gpu.dylib /usr/lib/libopencv_highgui.dylib /usr/lib/libopencv_imgproc.dylib /usr/lib/libopencv_legacy.dylib /usr/lib/libopencv_ml.dylib /usr/lib/libopencv_nonfree.dylib /usr/lib/libopencv_objdetect.dylib /usr/lib/libopencv_photo.dylib /usr/lib/libopencv_stitching.dylib /usr/lib/libopencv_ts.dylib /usr/lib/libopencv_video.dylib /usr/lib/libopencv_videostab.dylib
The install built by this particular tutorial has placed an opencv.pc file at this location:
/Users/Me/Code/OpenCV/opencv/build/unix-install
BUT Running the below and trying make again doesn't fix the problem:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/Users/Me/Code/OpenCV/opencv/build/unix-install
I can't figure out what is wrong. I've been battling with this problem for about a month on and off, and finally needed some outside opinions. Any ideas?
Error message says
cannot find -lopencv_calib3d collect2: ld returned 1 exit status
It means linker can't find opencv_calib3d library. It may have name libopencv_calib3d.a or libopencv_calib3d.so. So you need find one of them and add path to this file to your LD_LIBRARY_PATH env variable.
I mean if let's say folder /Users/Me/Code/OpenCV/opencv/lib contains libopencv_calib3d.so then you need execute something like following before make:
export LD_LIBRARY_PATH=/Users/Me/Code/OpenCV/opencv/lib:$LD_LIBRARY_PATH
Try the official OpenCV4Android tutorials at http://docs.opencv.org/trunk/doc/tutorials/introduction/android_binary_package/android_dev_intro.html

Categories

Resources