Java Native Interface - java

I am quite new to JNI. Now I have a Question that, I have my own simple library in C. i have created both .so and .a library for my c library.
Generated libmymath.so Via following Command
gcc -shared -fPIC -o libmymath.so addnsub.o mulndiv.o
JNI.java
public class JNIPart
{
public native int addJNI(int a, int b);
public static void main(String args[])
{
JNIPart ja = new JNIPart();
int answer = ja.addJNI(10, 20);
System.out.println("Answer = "+ answer);
}
static
{
System.loadLibrary("mymath");
}
}
JNI.c
#include "JNIPart.h"
#include "stdio.h"
JNIEXPORT jint JNICALL Java_JNIPart_addJNI(JNIEnv *env, jobject thisObj, jint a, jint b)
{
return add(a, b);
}
JNI.h
#include "jni.h"
#include "addnsub.h"
#include "mulndiv.h"
JNIEXPORT jint JNICALL Java_JNIPart_addJNI(JNIEnv *env, jobject thisObj, jint a, jint b);
Note :- Function add() is part of my library.
Now When i am trying to compile JNI.c, It Works Fine with Following Command
gcc -c JNI.c -I="/home/axit/jdk1.7.0_67/include/"
Path of JDK include is for jni.h
Then I am Trying to Create the Shared Library Which should go to the System.loadLibrary() of Java
gcc -shared -fPIC -o libJNI JNI.o
As Mentioned earlier that, libJNI library will be go to the System.loadLibrary() of JNI.java
Question is :-
1) My libJNI Library Should Contain Both my own libmymath.so and JNI.o. Am i Right ?
2) When I am Trying to Generate the .so which is Combination of libmymath.so and JNI.o. It is giving error to as following
Command :-
gcc -shared -fPIC -o libJNI JNI.o libmymath.so
libJNI :- Library that should go in JNI.java, System.loadLibrary()
JNI.o :- Compiled JNI.c
libmymath.so :- my library that contains basic add, sub, mul, and division function
/usr/bin/ld: JNIPart.o: relocation R_X86_64_PC32 against undefined symbol `add' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: ld returned 1 exit status
If helper want more information, i'll be more specific and any help will be appreciated
Help me.
Thanks,
Axit Soni

Q: My libJNI Library Should Contain Both my own libmymath.so and JNI.o. Am i Right?
It sounds like your JNI.o calls code in libmymath.so. So yes, you need to specify libmylibmath.so in your link command, and you also need to make sure it's available at runtime.
Q: When I am Trying to Generate the .so which is Combination of libmymath.so and JNI.o. It is giving error undefined symbol 'add' can not be used when making a shared object; recompile with -fPIC
I would run "nm" against libmymath.so and make sure you actually have an "add". Perhaps you inadvertantly compiled one or more functions with C++, and the name is mangled?
It wouldn't hurt to do what the error says, and make sure everything is consistently built with "-fPIC".

Related

java.lang.UnsatisfiedLinkError even though libraries and classes have method

I am using Python and Py4J to test JNI code. But when I call the JNI code I get the following error:
py4j.protocol.Py4JJavaError: An error occurred while calling o37.createInstance.
: java.lang.UnsatisfiedLinkError: com.mgr_api_JNI.createInstance(Lcom/mgr_api_types$EDisplayType;Ljava/lang/String;Lcom/mgr_api_types$ECommType;Ljava/lang/String;)V
at com.mgr_api_JNI.createInstance(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
at py4j.Gateway.invoke(Gateway.java:282)
at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
at py4j.commands.CallCommand.execute(CallCommand.java:79)
at py4j.GatewayConnection.run(GatewayConnection.java:238)
at java.base/java.lang.Thread.run(Thread.java:834)
I have looked at these links link 1, link 2, link 3, link 4, link 5, and link 6, plus others, but none of them solve my problem.
Code
mgr_api_JNI.java:
package com;
import com.mgr_api_types.*;
public class mgr_api_JNI
{
static
{
try
{
System.loadLibrary("Mngr"); // Use "-Djava.library.path=[path to library]" option to load this library
}
catch (UnsatisfiedLinkError e)
{
System.err.println("Native code library 'Mngr' failed to load.\n" + e);
}
}
public native void createInstance(com.mgr_api_types.EDisplayType displayType,
String displaySerialNumber,
com.mgr_api_types.ECommType commType,
String portName);
}
testsJNI.java:
import com.*;
import py4j.GatewayServer;
public class testsJNI
{
public static void main(String args[])
{
testsJNI testApp = new testsJNI();
// Py4J server
GatewayServer server = new GatewayServer(testApp);
server.turnLoggingOff();
server.start();
}
}
com_mgr_api_JNI.h (created by using javac -h on mgr_api_JNI.java):
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_mgr_api_JNI */
#ifndef _Included_com_mgr_api_JNI
#define _Included_com_mgr_api_JNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_mgr_api_JNI
* Method: createInstance
* Signature: (Lcom/mgr_api_types/EDisplayType;Ljava/lang/String;Lcom/mgr_api_types/ECommType;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_com_mgr_1api_1JNI_createInstance
(JNIEnv *, jobject, jobject, jstring, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
com_mgr_api_JNI.cpp:
#include "com_mgr_api_JNI.h"
static manager::CManagerApi* manager = NULL;
JNIEXPORT void JNICALL Java_com_mgr_1api_1JNI_createInstance(
JNIEnv *env,
jobject thisObj,
jobject displayType,
jstring displaySerialNumber,
jobject commType,
jstring portName)
{
manager::EDisplayType dType = convertObjectToDisplayType(env, displayType);
const char* serialNumber = env->GetStringUTFChars(displaySerialNumber, 0);
manager::ECommType comm = convertObjectToCommType(env, commType);
const char* port = env->GetStringUTFChars(portName, 0);
char buf[100];
sprintf(buf,"%s",port);
std::string portStr = buf;
sprintf(buf,"%s",serialNumber);
std::string serialNumStr = buf;
if (manager == NULL)
{
manager = manager::CManagerApi::get();
manager->initialize(dType, serialNumStr, comm, portStr);
}
// Release memory
env->ReleaseStringUTFChars(displaySerialNumber, serialNumber);
env->ReleaseStringUTFChars(portName, port);
}
Command line execution of Java code:
java -cp /mnt/c/Workspace/library/java/:.:/home/fred/.local/share/py4j/py4j0.10.7.jar -Djava.library.path=/mnt/c/Workspace/build/library/ testsJNI
Doing a -XshowSettings:properties shows the following properties:
awt.toolkit = sun.awt.X11.XToolkit
file.encoding = UTF-8
file.separator = /
java.awt.graphicsenv = sun.awt.X11GraphicsEnvironment
java.awt.printerjob = sun.print.PSPrinterJob
java.class.path = /mnt/c/Workspace/library/java/
.
/home/fred/.local/share/py4j/py4j0.10.7.jar
java.class.version = 55.0
java.home = /usr/lib/jvm/java-11-openjdk-amd64
java.io.tmpdir = /tmp
java.library.path = /mnt/c/Workspace/build/library/
Attempts to solve the problem
Making sure I have a valid native library
Doing an ls on the java.library.path /mnt/c/Workspace/build/library/ shows the library libMngr.so.
If I delete libMngr.so from that location and then try to run Java complains it can't find the library.
Doing the nm command on libMngr.so shows the following:
000000000021f269 T Java_com_mgr_1api_1JNI_createInstance
So Java can find the native library and it has the symbol of the function.
Doing the objdump command:
$objdump -f build/library/libMngr.so
build/library/libMngr.so: file format elf64-x86-64
architecture: i386:x86-64, flags 0x00000150:
HAS_SYMS, DYNAMIC, D_PAGED
start address 0x000000000018aee0
Shows that the native library is 64 bit and according to the -XshowSettings:properties I am using 64 bit Java.
I have even put a print statement right before System.loadLibrary("Mngr"); in mgr_api_JNI.java to make sure that the native library is loaded only once.
Update
I have regenerated the header file from mgr_api_JNI.java and copied the function declaration to the .cpp file to make sure the function name is correct. But I still get the same error.
Making sure I have valid Java classes
If I do an ls on the java.class.path /mnt/c/Workspace/library/java/ I find all of the Java classes from compiling mgr_api_JNI.java.
If I delete the classes and try to run Java, then Java complains it can't find the classes.
Doing a grep -r createInstance on the java.class.path /mnt/c/Workspace/library/java/ returns:
Binary file com/mgr_api_JNI.class matches
com/mgr_api_JNI.java: public native void createInstance(com.mgr_api_types.EDisplayType displayType,
So Java can find the the compiled Java class and it has the createInstance method in it.
Questions
It appears to me that Java can find all of the needed classes and the native library, but I am still getting the UnsatisfiedLinkError error.
Why am I still getting this error?
What do I need to do to help Java find/recognize the createInstance method?
This is your error
You have library file: /mnt/c/Workspace/build/library/libMgr.so but you load this one in your code: System.loadLibrary("Mngr"); - you have a typo
You can make sure that in case of correct name it works as expected
When you "leave" initial JVM, -Djava.library.path is no longer a valid place to provide info to other JVM. I don't know the details of py4j.GatewayConnection, but it's good to make sure you don't run another JVM instance or, that you don't use JNI there.
I suggest following tests:
set the LD_LIBRARY_PATH to location where your lib is
make sure you can access library in the code with only public class mgr_api_JNI (without Python bridge engine)
if there is a chance that you second JVM is loading native code (and can't find it), try to use _JAVA_OPTIONS=-Djava.library.path=[path to library]. This way, all the JVMs will "see" the location - but you should do that just for testing purposes
It definitely looks like issue with loading the lib from another process. If I reduce your code to something like this:
.
|-- Makefile
|-- README.md
|-- c
| `-- com_mgr_api_JNI.cc
|-- java
| `-- com
| |-- mgr_api_JNI.java
| `-- mgr_api_types
| |-- ECommType.java
| `-- EDisplayType.java
|-- lib
`-- target
and the code itself
com_mgr_api_JNI.cc
#include <iostream>
#include "jni.h"
#include "com_mgr_api_JNI.h"
using namespace std;
JNIEXPORT void JNICALL Java_com_mgr_1api_1JNI_createInstance
(JNIEnv *env, jobject cls, jobject a, jstring b, jobject c, jstring d) {
cout << "Hello";
}
java/com/mgr_api_JNI.java
package com;
import com.mgr_api_types.*;
public class mgr_api_JNI
{
static {
try {
System.loadLibrary("Mngr"); // Use "-Djava.library.path=[path to library]" option to load this library
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library 'Mngr' failed to load.\n" + e);
}
}
public native void createInstance( com.mgr_api_types.EDisplayType displayType,
String displaySerialNumber,
com.mgr_api_types.ECommType commType,
String portName);
public static void main(String [] arg) {
mgr_api_JNI obj = new mgr_api_JNI();
obj.createInstance(new com.mgr_api_types.EDisplayType(), "", new com.mgr_api_types.ECommType(), "");
}
}
java/com/mgr_api_types/ECommType.java
package com.mgr_api_types;
public class ECommType { }
cat java/com/mgr_api_types/EDisplayType.java
package com.mgr_api_types;
public class EDisplayType { }
and I build the code
Makefile.common
ARCH=$(shell uname -s | tr '[:upper:]' '[:lower:]')
ifeq ($(ARCH),darwin)
EXT=dylib
else
EXT=so
endif
Makefile
include Makefile.common
all: compilejava compilec
compilec:
c++ -std=c++11 -g -shared -fpic -I${JAVA_HOME}/include -I${JAVA_HOME}/include/$(ARCH) c/com_mgr_api_JNI.cc -o lib/libMngr.$(EXT)
compilejava:
$(JAVA_HOME)/bin/javac -h c -d target -cp target java/com/mgr_api_types/EDisplayType.java
$(JAVA_HOME)/bin/javac -h c -d target -cp target java/com/mgr_api_types/ECommType.java
$(JAVA_HOME)/bin/javac -h c -d target -cp target java/com/mgr_api_JNI.java
test:
$(JAVA_HOME)/bin/java -Djava.library.path=$(LD_LIBRARY_PATH):./lib -cp target com.mgr_api_JNI
clean:
-rm -rfv target/*
-rm c/*.h
-rm -rf lib/*
it works as expected
make test
/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/bin/java -Djava.library.path=:./lib -cp target com.mgr_api_JNI
Hello
I think you have a situation where you class is called from either another instance of JVM, or from something called via JNI, or from another process and your -Djava.library.path is gone.
Also, try to use System.load("full/path/of/library.so") to make sure you can access the library from JVM.
The problem occurs when the runtime can not load the corresponding native function in a shared library.
So, in these cases it's always good to regenerate the header file to see if the function name is correct.
In this case your function name looks suspicious
Java_com_1mgr_1api_1JNI_createInstance
The generated name uses the _1 to encode a literal underscore in the Java class/function name. So, based on the class name mgr_api_JNI the name should be
Java_com_mgr_1api_1JNI_createInstance
instead. i.e. the first _1 is just a plain underscore _, and this is also what I see when generating a header file for a class with that name.

How to solve "fatal: relocations remain against allocatable but non-writable sections" while using Java native interface?

I'm trying to call a C function inside a Java code.
I have this hava code.
public class JavaToC {
public native void helloC();
static {
System.loadLibrary("HelloWorld");
}
public static void main(String[] args) {
new JavaToC().helloC();
}
}
. I compiled it and then created header file. Then make the following HelloWorld.c file.
#include <stdio.h>
#include <jni.h>
#include "JavaToC.h"
JNIEXPORT void JNICALL Java_JavaToC_helloC(JNIEnv *env, jobject javaobj)
{
printf("Hello World: From C");
return;
}
I tried compiling this using "gcc -o libHelloWorld.so -shared -I/usr/java/include -I/usr/java/include/solaris HelloWorld.c -lc", but it gives the following result.
Text relocation remains referenced
against symbol offset in file
.rodata (section) 0x9 /var/tmp//cc.GaGrd.o
printf 0xe /var/tmp//cc.GaGrd.o
ld: fatal: relocations remain against allocatable but non-writable sections
collect2: ld returned 1 exit status
I'm working on Solaris 11, how can I solve this?
I cannot test this on a Solaris machine at the moment, but from http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/SPARC-Options.html
-mimpure-text suppresses the “relocations remain against allocatable but non-writable sections” linker error message. However, the
necessary relocations will trigger copy-on-write, and the shared
object is not actually shared across processes. Instead of using
-mimpure-text, you should compile all source code with -fpic or -fPIC.
the solution seems to be to add the -fpic option to generate position-independent code.

java c++ jni unsatisfied link error

I'm quite a newbie to Java JNI. I tried a helloCPP program but ran into problems. here's my code :
helloCPP.java;
public class helloCPP {
public native void hellocpp ();
static {
System.loadLibrary("helloCPP");
}
public static void main (String [] args) {
helloCPP hcpp=new helloCPP ();
hcpp.hellocpp ();
System.exit (0);
}
}
Then here's my hellocpp.cpp out of which I made helloCPP.dll ;
#include <iostream>
#include "helloCPP.h"
using namespace std ;
JNIEXPORT void JNICALL Java_helloCPP_hellocpp. (JNIEnv *env, jobject obj) {
cout <<"hello java, I'm c++\n";
}
int main (){};
I successfully built the helloCPP.dll. But when I try to run java helloCPP, I get this error:
Exception in thread "main"
java.lang.UnsatisfiedLinkError: helloCPP.hellocpp () V
at helloCPP.hellocpp(Native Method)
at helloCPP.main (helloCPP.java :8)
I'm doing all these stuff on a Windows 8 x86 operating system.
These are the commands I wrote:
javac helloCPP.java
javah helloCPP
g++ -c hellocpp.cpp
g++ -o helloCPP.dll hellocpp.cpp
This one generated the error:
java HelloCPP
Thanks in advance.
Most likely, the Java run-time cannot locate the DLL file. You have run the Java application with additional arguments (assuming the DLL file is in the current directory):
java -Djava.library.path=. HelloCPP
Update:
EJP has a good point. I think your link command isn't correct. The second g++ command should be something like:
g++ -o helloCPP.dll -shared hellocpp.o
There are two changes: hellocpp.o instead of hellocpp.cpp and more importantly the option -shared to indicate that you want to create a shared library and not an executable.
The exact options depend on your platform. As you haven't specified it, I can't tell you for sure.

Java: Invoking native method giving "Exception in thread "main" java.lang.UnsatisfiedLinkError"

I am trying to call a simple native method in Java from C++, to achieve this I do the following:
Create a simple class as shown below:
public class HelloNative{
public native void printString(String str);
static{
System.out.println("Current Directory is: " + System.getProperty("user.dir"));
System.load(System.getProperty("user.dir") + "/libhellonative.so");
}
public static void main(String[] args){
System.out.println("Calling Native Libraray (libhellonative.so) method printString");
new HelloNative().printString("Message from Java to C");
}
}
Create .h file for the native method using javah -jni which create the following declaration:
JNIEXPORT void JNICALL Java_HelloNative_printString(JNIEnv *, jobject, jstring);
Implement the native function in .cpp file as:
JNIEXPORT void JNICALL Java_HelloNative_printString(JNIEnv* jni_env, jobject java_obj, jstring msg){
printf("inside native method\n");
jboolean iscopy;
const char *message = (jni_env)->GetStringUTFChars( msg, &iscopy);
printf("%s", message);
}
And finally create the .so file using:
g++ HelloNative.cpp -o libhellonative.so -shared -Wl,-soname,libhellonative.so -static -lc -I /usr/lib/jvm/java-6-sun-1.6.0.26/include -I /usr/lib/jvm/java-6-sun-1.6.0.26/include/linux
But when I compile and run the .java file it's giving me Runtime Exception:
Current Directory is: /home/gmuhammad/Projects/test
Calling Native Libraray (libhellonative.so) method printString
Exception in thread "main" java.lang.UnsatisfiedLinkError: HelloNative.printString(Ljava/lang/String;)V
at HelloNative.printString(Native Method)
at HelloNative.main(HelloNative.java:16)
Ok, I got this to work. It has nothing to do with loading the library, but with actually calling the method from that library.
I created .java, .h and .cpp files by copy/paste from your question and ran them (I had to add #include <jni.h> in the .cpp file) - and got exactly the same error as you did.
Then I edited the .cpp file to include the generated .h file. Also, as maba indicated in his answer, you need to call (jni_env)->ReleaseStringUTFChars(msg, message); to release the object. My full .cpp file now looks like this:
#include "HelloNative.h"
JNIEXPORT void JNICALL Java_HelloNative_printString(JNIEnv* jni_env, jobject java_obj, jstring msg){
printf("inside native method\n");
jboolean iscopy;
const char *message = (jni_env)->GetStringUTFChars( msg, &iscopy);
printf("%s", message);
(jni_env)->ReleaseStringUTFChars(msg, message);
}
I re-compiled the library, ran the java code - and voila! everything works. This way, it works regardless of which way you load the library, with load or loadLibrary.
Give it a try.
I recommend to load the library slightly differently.
Place your libmynative.so into whatever directory you like.
Add that directory to LD_LIBRARY_PATH variable.
In your java code, change System.load call to be System.loadLibrary("mynative") (note the lack of full path, prefix lib and extension .so)
Run your java code.
Have a look here for more details: http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html
It is totally OK to use the System.load() but you will have to be sure that your shared libraries really are where you say they should be.
Current Directory is: /home/gmuhammad/Projects/test
Your code is trying to access the shared library in that directory. Are you sure that the libhellonative.so is there?
Then you can also use the System.mapLibraryName("hellonative") to get the name of the shared library for the current platform. That makes it more platform independent. The call will give you libhellonative.so on linux and hellonative.dll on windows.
Also you must call (jni_env)->GetReleaseUTFChars(msg, message); to release the objects.

JNI error while calling a C subroutine

I want to call a C subroutine from Java. I'm using JNI. I have created the .java, .c, and .h files, and compiled a DLL. All the files are in the same folder. But when I run the program, it shows an unsatisfiedlinkError. Where am I going wrong...?
As I am learning JNI, the source code I used is the one from: http://www.ibm.com/developerworks/java/tutorials/j-jni/section2.html and things I have already tried:
Create a dll using Code::Blocks(ide) and GCC as the compiler
Create dll using GCC from command line (Ref. http://sig9.com/node/35 )
I am using Win7 32 bit, and I guess all the methods above generate 32-bit DLLs
All the solutions I found for creating a DLL (shared library) are for MS VC/VCPP and I don't have that on my machine right now.
Where is the problem? DLL files are being generated without any exception, but when I run the Java code, it throws the exception.
PS: If there is any theoretical examples that explains how JNI works and what actually it does, then kindly share the link...
The message or exception being thrown:
c:\myjava1>java Sample1
Exception in thread "main" java.lang.UnsatisfiedLinkError: Sample1.intMethod(I)I
at Sample1.intMethod(Native Method)
at Sample1.main(Sample1.java:11)
At the end of the day after creating dll file lots of times,I am pretty sure that there is possibly no issue with it ,something is wrong with the path ...I have changed the loadlibrary method with load method ,but still no luck,.....
as suggested by a MOD:
I have been discussing about this question on the post:JNI error while calling a C subroutine ,I am posting all the codes here as the comments have limited characters... Sample1.c
#include "jni.h"
#include"Sample_Sample1.h"
JNIEXPORT jint JNICALL Java_Sample_Sample1_test(JNIEnv *env, jobject obj){
return(1);
}
void main(){}
Sample1.java
package Sample;
public class Sample1
{
public native int test();
static{
System.loadLibrary("Sample1");
}
public static void main(String[] args)
{
Sample1 sample = new Sample1();
System.out.println(sample.test());
}
}
Sample_Sample1.h(generated using javah -jni command)
/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
/* Header for class Sample_Sample1 */
#ifndef _Included_Sample_Sample1
#define _Included_Sample_Sample1
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Sample_Sample1
* Method: test
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_Sample_Sample1_test
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
Can anyone guide me where the issue is?In earlier post I mentioned that the code I am using is the one from tutorials ,but to simplyfy the things I have changed the codes ...While using **java Sample.Sample1" i am getting:
c:\myjava1>java Sample.Sample1
Exception in thread "main" java.lang.UnsatisfiedLinkError: Sample.Sample1.test()I
at Sample.Sample1.test(Native Method)
at Sample.Sample1.main(Sample1.java:12)
You need to have your library explicitly set on your path.
It may be the case that the flags you are using there aren't quite right. Try this:
gcc -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at \
-I[Java_HOME]/include -I[Java_HOME]/include/win32 \
-shared -o Sample1.dll Sample1.c
From MinGW GCC site.
Have you checked your DLL by calling it from a stub C++ application? Pay particular attention to the exact name (including capitalisation) of the method(s) you are calling.
The Javadoc for UnsatisfiedLinkError says "Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native." That probably means either that the name is misspelled or your DLL is not where the JVM is expecting to find it.
Finally solved the problem using System.load() method, System.loadLibrary() Still doesn't work for me...it keeps on giving the same exception, And the I think the issue was with the .dll Thanks to all of you who supported and responded...
After spending 2hrs and analysing the code. Problem is with .dll/.so compilation. Below command works for me:
g++ -dynamiclib HelloJni.cpp -I /usr/include/ -I /usr/lib/jvm/java-1.8.0-openjdk-amd64/include/ -I /usr/lib/jvm/java-1.8.0-openjdk-amd64/include/linux/ -shared -o libHelloJni.so
or
g++ -dynamiclib HelloJni.cpp -I /usr/include/ -I $JAVA_HOME/include/ -I $JAVA_HOME/include/linux/ -shared -o libHelloJni.so
Above command is for linux. In case of Windows, change
[Java_HOME]/include/linux/ ---> [Java_HOME]/include/win/
Mac:
$Java_HOME/include/linux/ ---> $Java_HOME/include/darwin/

Categories