1

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

enter image description here

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;
}
HuaJFrame
  • 72
  • 7
  • 1
    IIRC, the naive approach for matching names like this only works for C functions, and C++ applies some form of name-mangling. You may want to check what https://github.com/nativelibs4java/JNAerator generates (though it hasn't been maintained for a long time). – Mark Rotteveel Aug 03 '23 at 09:03
  • 1
    It may help if you add the source of TestJNA.cpp – tgdavies Aug 03 '23 at 09:05
  • @tgdavies Thanks for the reply, the code of TestJNA.cpp has been added to this entry. – HuaJFrame Aug 03 '23 at 09:14
  • Thanks, I will try it. @MarkRotteveel – HuaJFrame Aug 03 '23 at 09:22

0 Answers0