I am trying to make a native list in c++ and use it in java, I am fairly sure everything is declared right but I am getting a link error
Java Exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Java_List.init_list(Ljava/lang/Object;)J
at Java_List.init_list(Native Method)
at Java_List.main(Java_List.java:13)
Java source
public class Java_List
{
static
{
System.loadLibrary("JAVA_JNI_FTC");
}
public native long init_list(Object a);
public static void main(String[] args)
{
Java_List list = new Java_List();
System.out.println(list.init_list(list));
}
}
header file
#include <jni.h>
#include "List.h"
#include <new>
#ifndef _Included_JAVA_LIST
#define _Included_JAVA_LIST
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloJNI
* Method: sayHello
* Signature: ()V
*/
JNIEXPORT jlong JNICALL Java_Java_List_init_list(JNIEnv *, jobject, jobject);
JNIEXPORT jobject JNICALL Java_Java_List_list_get(JNIEnv *env, jobject thisObj, jint index, jlong list);
#ifdef __cplusplus
}
#endif
typedef struct Java_List Java_List;
struct Java_List
{
void *list;
jclass type;
};
void init_Java_List(Java_List *jList, jclass type);
jobject java_list_get(Java_List *jList, int index);
#endif
C ++ source
#include <jni.h>
#include "Java_List.h"
#include "pch.h"
extern "C"
{
JNIEXPORT jlong JNICALL Java_Java_List_init_list(JNIEnv *env, jobject thisObj, jobject classType)
{
jclass type = env->GetObjectClass(classType);
Java_List *list = (Java_List *)malloc(sizeof(Java_List));
init_Java_List(list, type);
return (jlong)list;
}
JNIEXPORT jobject JNICALL Java_Java_List_list_get(JNIEnv *env, jobject thisObj, jint index, jlong list)
{
return java_list_get((Java_List *)list, (int)index);
}
}
/* jni api*/
void init_Java_List(Java_List *jList, jclass type)
{
jList->list = malloc(sizeof(List<jobject>));
new (jList->list) List<jobject>();
jList->type = type;
}
jobject java_list_get(Java_List *jList, int index)
{
List<jobject> *list = (List<jobject> *) jList->list;
return *(list->get(index));
}
I am using visual studio for this project, the project builds multiple files not just the dll, I dont know if this the issue. Files
Your Java class name and method name contain underscores, which goes against the naming convention.
If you insist on keeping them, you need to change the name of your C++ function to Java_Java_1List_init_1list
. Note the 1
s before List
and list
, which tells the linker to interpret the preceeding underscore as a literal underscore character instead of a naming separator.