简体   繁体   English

无法从Java类调用静态库函数

[英]Unable to call static library function from Java class

Say I have a static C++ lib, DataPacker.lib and is linked to a shared dynamic link library called Command.dll. 假设我有一个静态C ++库DataPacker.lib,并链接到一个名为Command.dll的共享动态链接库。

The static library exposes following function, 静态库提供以下功能,

//Datapacker.h
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT void JNICALL Java__Observer_sendToClient
(JNIEnv *, jclass, jobject);
#ifdef __cplusplus
}
#endif

//Datapacker.cpp
JNIEXPORT void JNICALL Java__Observer_sendToClient
(JNIEnv *, jclass, jobject)
{
//Print
}

Now I tried to call above function from java class as follows, 现在,我尝试从java类中调用上述函数,如下所示:

//Observer.Java
class Observer
{
static{
System.loadLibrary("Command");
}
public static void main(String[] args) {
try{
sendToClient();
} 
catch (Throwable t){
System.err.println("Native code library failed to load.\n" +  t.getMessage());
}
}
private static native void sendToClient();
}

But am getting following exception from java, 但是我正在从Java获取以下异常,

"Native code library failed to load.Observer.sendToClient()V" “本机代码库无法加载。Observer.sendToClient()V”

The call works fine when it is moved from DataPacker.lib to command.lib. 从DataPacker.lib移到command.lib时,该调用工作正常。 Please help. 请帮忙。

c++ linker didnt add DataPacker.lib code into Command.dll. C ++链接器未将DataPacker.lib代码添加到Command.dll。 if you want to call function you must write proxy function in the Command.dll for DataPacker.lib(global export function for java calls) for example dll code: #include #ifdef _ cplusplus extern "C" { #endif JNIEXPORT void JNICALL Java _Observer_sendToClient (JNIEnv *, jclass, jobject); 如果要调用函数,则必须在Command.dll中为DataPacker.lib(java调用的全局导出函数)编写代理函数,例如dll代码:#include #ifdef _ cplusplus extern“ C” {#endif JNIEXPORT void JNICALL Java _Observer_sendToClient(JNIEnv *,jclass,jobject); #ifdef __cplusplus } #endif #ifdef __cplusplus} #endif

    //DatapackerDLL.cpp
    JNIEXPORT void JNICALL Java__Observer_sendToClient
    (JNIEnv *, jclass, jobject)
    {
     DataPacker::sendToClient();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM