JDK9 NetBeans Dev - compile error during JNI Tutorial - java

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.

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.

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

Android NDK: Eclipse parameter and javah

I spent hours and hours on it not moving an inch forward. I have redone the project several times and nothing else is left outside places. Hope someone here can shed some light pointing out some direction.
On Linux Eclipse IDE panel > Run > External Tools > External Tools Configurations:
Name: Dev javah
Tab [main]:
* location: /usr/java/jdk1.6.0_25/bin/javah
* working directory: ${workspace_loc:/Dev/bin}
* Arguments: ??
I have tried:
* Arguments: -d ${workspace_loc:/Dev/jni} com.dev.DevActivity ${project_classpath:Dev} com.pkgpub.DevActivity
which gives me:
1 error
javadoc: error - Illegal package name: "/home/user/dev/Dev/bin/classes"
and also tried:
* Arguments: -d ${workspace_loc:/Dev/jni} com.dev.DevActivity
That gives...
error: cannot access com.dev.DevActivity
class file for com.dev.DevActivity not found
javadoc: error - Class com.dev.DevActivity not found.
Error: No classes were specified on the command line. Try -help.
similarly to -
* Arguments: -d ${workspace_loc:/Dev/jni} com.pkgpub.DevActivity
That gives me:
error: cannot access com.pkgpub.DevActivity
class file for com.pkgpub.DevActivity not found
javadoc: error - Class com.pkgpub.DevActivity not found.
Error: No classes were specified on the command line. Try -help.
But when I run:
which javah
/usr/bin/which: no javah in (/usr/lib64/qt-3.3/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/user/android-sdks/tools:/home/user/android-sdks/platform-tools:/opt/android/android-ndk-r7:/home/user/.local/bin:/home/user/bin:/home/user/android-sdks/tools:/home/user/android-sdks/platform-tools:/opt/android/android-ndk-r7:/home/user/android-sdks/tools:/home/user/android-sdks/platform-tools:/opt/android/android-ndk-r7)
The question is... the project is not compiling because javah is not visible to Eclipse NDK, or due to parameter details that something is missing? And in such case, how to get it fixed?
Thanks.
try adding
-classpath bin/classes
as parameter to javah
Specially for Android:
use classes right after bin like project_location/bin/classes, this worked for me on Linux Fedora
javah -d /var/www/project/jni/ -classpath /var/www/project/bin/classes/ com.android.TestClass
#BlackBelt - thanks man for your suggestion!
Here is the parameter that worked, solving the javah side for the compiling process:
-d ${workspace_loc:/Dev/jni} com.pkgpub.Dev -classpath ${workspace_loc:/Dev/bin/classes/} com.pkgpub.Dev
Where pkgpub is the package name, and Dev is the project name. It is worth to mention that the project has the files: \src\Dev.java; \jni\Dev.c; \jni\Dev.h; \bin...\pkgpub\Dev.class.
Now it generates the file jni/com_pkgpub_Dev.h with the right content. Which is great!
However the ndk-build is still not completely linking, and does not generate the com_pkgpub_Dev.c file. So, from Project > Project Build - comes the message:
**** Build of configuration Build (GNU) for project Dev ****
ndk-build
make: *** No rule to make target `jni/com_pkgpub_Dev.c', needed by `obj/local/armeabi/objs/dev/com_pkgpub_Dev.o'. Stop.
**** Build Finished ****
From this message it sounds like some parameter should be missing from the file /jni/Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CFLAGS := -DHAVE_INTTYPES_H
LOCAL_MODULE := dev
LOCAL_SRC_FILES := com_pkgpub_Dev.c Dev.c
include $(BUILD_SHARED_LIBRARY)
To solve it, it is just necessary to create the missing file "jni/com_pkgpub_Dev.c". This file content should be manually made based on the automatic generated code contained in the file "jni/com_pkgpub_Dev.h".
Now, it works!

Categories

Resources