Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Persistent UnsatisfiedLinkError while running Java invoking native .so on Linux, within Eclipse(JNI test setup with Eclipse fails on Linux)
Persistent UnsatisfiedLinkError while running Java invoking native .so on Linux, within Eclipse [message #1773712] Tue, 03 October 2017 10:37
herman budde is currently offline herman buddeFriend
Messages: 1
Registered: October 2017
Junior Member
I'm trying to get a small/sample Java application, which invokes native C++ code using JNI calls, running on Linux.

I use Eclipse to build, configure the environment and run the application.

I have divided the "overall" project in 2 separate Eclipse projects: 1 Java project and 1 C++ project, containing the native code.

Eclipse project view

The Java part consists of: 1: a "main" class, from which an "adapter" class is invoked to load the .so library and call the native interface/C++ methods 2: an "adapter" class, containing the native method declarations

public class Java_Main_For_So_Kickoff {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello from Java main!");

Native_Cpp_Adapter adapter = new Native_Cpp_Adapter();

adapter.locSetProperty();
adapter.locLoadLib();

adapter.sayHello();

adapter.test_Kickoff_So_For_Print();

}

}


public class Native_Cpp_Adapter {
public native void test_Kickoff_So_For_Print();

public void locSetProperty() {
"/home/adminuser/workspace_Unit_Test_Java_Cpp/Unit_Test_Cpp/Debug");
String libPathProp = System.getProperty("java.library.path");

System.out.println("lib path set:" + libPathProp);
}

public void locLoadLib() {
System.setProperty("java.library.path",
"//home//adminuser//workspace_Unit_Test_Java_Cpp//Unit_Test_Cpp//Debug//");
String libPathProp = System.getProperty("java.library.path");
String soLibName = "libUnit_Test_Cpp.so";
String soLibWithPath = libPathProp.concat(soLibName);

System.out.println("lib path set for loading:" + libPathProp);
System.load(soLibWithPath);

System.out.println("library loaded");
}

public void sayHello() {
System.out.println("Hello from JNI Adapter (Java part)");
}
}
The C++ part consists of: 1: a *.h file, generated with javah, containing the native method definition 2: a *.cpp file, containing the C++ implementation of the native methods

Both very rudimentary, only meant to sanity-test the JNI environment setup. Added this, omitted that in the original post for this issue.

.h file: Native_Cpp_adapter.h

/*
* Native_Cpp_Adapter.h
*
* Created on: Aug 23, 2017
* Author: adminuser
*/

#ifndef SRC_NATIVE_CPP_ADAPTER_H_
#define SRC_NATIVE_CPP_ADAPTER_H_

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Native_Cpp_Adapter */

#ifndef _Included_Native_Cpp_Adapter
#define _Included_Native_Cpp_Adapter
#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT void JNICALL Java_Native_Cpp_Adapter_test_Kickoff_So_For_Print
(JNIEnv *, jobject);


#ifdef __cplusplus
}
#endif
#endif





#endif /* SRC_NATIVE_CPP_ADAPTER_H_ */
.cpp file: Native_Cpp_Adapter.cpp

#include "jni.h"
#include <iostream>

using namespace std;

JNIEXPORT void JNICALL Java_Native_Cpp_Adapter_test_Kickoff_So_For_Print
(JNIEnv *, jobject)
{
cout << "Correct kickoff of Native JNI method nr. 1";
return;
}
From the C++ project an .so is built, including jni.h and jni_md.h. for the Java project, the resulting .so is included as external library in the Java build path and added as VM argument "java.library.path" in the run configuration (for the applicable Java project/main class).

First, the .so is built from the C++ project: this results in an .so binary in the C++ project folder within the common workspace. The the Java build is performed. Thereafter the Java program is run invoking the project's main class.

Result: the .so appears to be loaded, but thereafter the process is aborted with an (very persistent) "UnsatisfiedLinkError".

For what I understand, this error is thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native.

This sounds like the native method definition (C++ side) is not compliant with the native method declaration on the Java-side. But the C++ definitions are as far as I see/know entirely compliant with the native method declaration in Java, and with the applicable native method naming conventions. For me, it appears that all building/configuration options are exhausted - does anyone have a suggestion what might be the cause, and to solve this?

Can it have something to do with the Eclipse environment (settings), and possible interference with other (Linux) setting?
Previous Topic:cannot set breakpoints
Next Topic:Launch Failed. Binaries not found.
Goto Forum:
  


Current Time: Sat Apr 27 01:56:24 GMT 2024

Powered by FUDForum. Page generated in 0.02575 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top