BAEL-4165 - Custom DLL Load - Fixing the "java.lang.UnsatisfiedLinkError" Error (#14393)
* first draft * review 1 * review 1 - test setup
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
||||
#include "com_baeldung_unsatisfiedlink_JniUnsatisfiedLink.h"
|
||||
#include <iostream>
|
||||
|
||||
/*
|
||||
* Class: com_baeldung_unsatisfiedlink_JniUnsatisfiedLink
|
||||
* Method: test
|
||||
* Signature: ()Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jstring JNICALL Java_com_baeldung_unsatisfiedlink_JniUnsatisfiedLink_test (JNIEnv* env, jobject thisObject) {
|
||||
std::string test = "Test OK";
|
||||
std::cout << test << std::endl;
|
||||
return env->NewStringUTF(test.c_str());
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class com_baeldung_unsatisfiedlink_JniUnsatisfiedLink */
|
||||
|
||||
#ifndef _Included_com_baeldung_unsatisfiedlink_JniUnsatisfiedLink
|
||||
#define _Included_com_baeldung_unsatisfiedlink_JniUnsatisfiedLink
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: com_baeldung_unsatisfiedlink_JniUnsatisfiedLink
|
||||
* Method: test
|
||||
* Signature: ()V
|
||||
*/
|
||||
JNIEXPORT jstring JNICALL Java_com_baeldung_unsatisfiedlink_JniUnsatisfiedLink_test
|
||||
(JNIEnv *, jobject);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
#!/bin/bash
|
||||
# Create the header with javac -h . ClassName.java
|
||||
# Remember to set your JAVA_HOME env var
|
||||
# Don't forget to set java.library.path to point to the folder where you have the lib you're loading.
|
||||
MYSELF="$(readlink -f "$0")"
|
||||
MYDIR="${MYSELF%/*}"
|
||||
|
||||
cd "$MYDIR"
|
||||
class_name=com_baeldung_unsatisfiedlink_JniUnsatisfiedLink
|
||||
lib_name=test
|
||||
|
||||
g++ -c -fPIC -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/linux" ${class_name}.cpp -o ${class_name}.o
|
||||
g++ -shared -fPIC -o lib${lib_name}.so ${class_name}.o -lc
|
||||
|
||||
# 32-bit version
|
||||
g++ -m32 -c -fPIC -I"${JAVA_HOME}/include" -I"${JAVA_HOME}/include/linux" ${class_name}.cpp -o ${class_name}32.o
|
||||
g++ -m32 -shared -fPIC -o lib${lib_name}32.so ${class_name}32.o -lc
|
||||
|
||||
# dummy version
|
||||
touch ${class_name}-dummy.o
|
||||
|
||||
ls lib*
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.unsatisfiedlink;
|
||||
|
||||
public class JniUnsatisfiedLink {
|
||||
|
||||
public static final String LIB_NAME = "test";
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.loadLibrary(LIB_NAME);
|
||||
new JniUnsatisfiedLink().test();
|
||||
}
|
||||
|
||||
public native String test();
|
||||
|
||||
public native String nonexistentDllMethod();
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
grant {
|
||||
permission java.lang.RuntimePermission "loadLibrary.test";
|
||||
};
|
||||
Reference in New Issue
Block a user