I'm learning JNI and JNA, and I'm facing the problem stated in the title while trying the examples of JNA
This is my pom and test code
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.13.0</version>
</dependency>
public interface Clibrary extends Library {
Clibrary INSTANCE = Native.load( "TestJNA", Clibrary.class);
int hello();
int basicTest(int a, float b, String pChar);
void arrayTest(String pStr, byte[] puStr);
void pointerTest(IntByReference pInt, FloatByReference pFloat);
void mallocTest(Memory pString);
void doublePointTest(PointerByReference ppInt, PointerByReference ppStr);
void freePt(Pointer pt);
}
public class Test {
public static void main(String[] args) {
System.setProperty("jna.library.path", "E:\\javaProject\\xxxxxxxx\\jniLearn\\src\\main\\resources\\lib");
Clibrary.INSTANCE.hello();
}
}
`TestJNA.dll` is written and compiled by myself.Here is the class structure, using Visual Studio 2019.The platform is x64
Here is the full stack exception.
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'hello': The specified program could not be found.
at com.sun.jna.Function.<init>(Function.java:252)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:620)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:596)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:582)
at com.sun.jna.Library$Handler.invoke(Library.java:248)
at com.sun.proxy.$Proxy0.hello(Unknown Source)
at org.szh.jna.Test.main(Test.java:15)
I tried to use the absolute path
Clibrary INSTANCE = Native.load("E:\\javaProject\\xxxxxxxx\\jniLearn\\src\\main\\resources\\libTestJNA",Library.class);
but the same problem still occurs.
this is TestJNA.cpp code.
#include "pch.h"
#include "TestJNA.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/**
No parameter test
*/
int hello()
{
printf("Hello world\n");
return 0;
}
/*
Basic data type test
*/
int basicTest(int a, float b)
{
printf("a=%d\n", a);
printf("b=%f\n", b);
return (int)(a + b);
}
/*
Pointer and array tests
*/
void arrayTest(char* pStr, unsigned char* puStr)
{
int i = 0;
printf("%s\n", pStr);
for (i = 0; i < 10; i++) {
printf("%c ", puStr[i]);
}
}
/*
Basic data type pointer test
*/
void pointerTest(int* pInt, float* pFloat)
{
*pInt = 10;
*pFloat = 12.34;
}
/*
Dynamic memory test
*/
void mallocTest(char* pszStr)
{
strcpy_s(pszStr, 50, "Happay Children's Day!");
}
/*
secondary pointer
*/
void doublePointTest(int** ppInt, char** ppStr)
{
printf("before int:%d\n", **ppInt);
**ppInt = 10086;
*ppStr = (char*)malloc(10 * sizeof(char));
strcpy_s(*ppStr, 50, "Happy National Day!");
}
/*
freed
*/
void freePoint(void* pt) {
if (pt != NULL) {
free(pt);
}
}
int main()
{
hello();
system("pause");
return 0;
}