JNI,Caused by: java.lang.UnsatisfiedLinkError: com.tencent.wxapp.core.jni.JNI.call(Ljava/util/List;)[B

huangapple go评论102阅读模式
英文:

JNI,Caused by: java.lang.UnsatisfiedLinkError: com.tencent.wxapp.core.jni.JNI.call(Ljava/util/List;)[B

问题

以下是您要翻译的内容:

I try to call cpp in Java. After packaging into Jar, such an error occurs after running.
I can run these codes in the Linux system, but I regret reporting the error after packaging them into a jar.

How should I modify my program? Is there a naming problem?

com.tencent.oceanus.jar.program.ProgramInvocationException: org.apache.flink.client.program.ProgramInvocationException: The program caused an error:
at com.tencent.oceanus.jar.CliFrontend.executeProgram(CliFrontend.java:248)
at com.tencent.oceanus.jar.CliFrontend.getJobGraph(CliFrontend.java:168)
at com.tencent.oceanus.server.jobs.JobManager.compile(JobManager.java:257)
at com.tencent.oceanus.server.jobs.transitions.StartTransitionCallback$StartThread.start(StartTransitionCallback.java:236)
at com.tencent.oceanus.server.jobs.transitions.StartTransitionCallback$StartThread.run(StartTransitionCallback.java:142)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.flink.client.program.ProgramInvocationException: The program caused an error:
at org.apache.flink.client.program.OptimizerPlanEnvironment.getOptimizedPlan(OptimizerPlanEnvironment.java:93)
at com.tencent.oceanus.jar.program.ClusterClient.getOptimizedPlan(ClusterClient.java:244)
at com.tencent.oceanus.jar.program.ClusterClient.run(ClusterClient.java:316)
at com.tencent.oceanus.jar.program.ClusterClient.run(ClusterClient.java:285)
at com.tencent.oceanus.jar.CliFrontend.executeProgram(CliFrontend.java:242)
... 9 more
Caused by: java.lang.UnsatisfiedLinkError: com.tencent.wxapp.core.jni.JNI.call(List;)[B
at com.tencent.wxapp.core.jni.JNI.call(Native Method)
at com.tencent.wxapp.core.jni.JNITest.main(JNITest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.tencent.oceanus.jar.program.CustomPackagedProgram.callMainMethod(CustomPackagedProgram.java:314)
at com.tencent.oceanus.jar.program.CustomPackagedProgram.invokeInteractiveModeForExecution(CustomPackagedProgram.java:213)
at org.apache.flink.client.program.OptimizerPlanEnvironment.getOptimizedPlan(OptimizerPlanEnvironment.java:83)
... 13 more

This is my JNI.java

package com.tencent.wxapp.core.jni;

import java.io.*;
import java.util.List;

public class JNI{
public native byte[] call(List jList);
static{
InputStream is = JNI.class.getResourceAsStream("/JNIdll.so");
File file = null;
try {
file = File.createTempFile("lib", ".so");
} catch (IOException e) {
e.printStackTrace();
}
OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] buffer = new byte[1024];
int length = 0;
while (true) {
try {
if (!((length = is.read(buffer)) != -1)) break;
} catch (IOException e) {
e.printStackTrace();
}
try {
os.write(buffer, 0, length);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}

  1. System.load(file.getAbsolutePath());
  2. file.deleteOnExit();
  3. }

}

This is my JNI.cpp

//JNIdll.cpp文件

#include<stdio.h>
#include
#include
#include "hyperloglog.hpp"
#include
#include
#include
#include "JNI.h"

using namespace hll;
using namespace std;

JNIEXPORT jbyteArray JNICALL Java_JNI_call
(JNIEnv env, jobject obj,jobject jList){
//实现代码
HyperLogLog hll(10);
// retrieve the java.util.List interface class
jclass cList = env->FindClass("java/util/List");
// retrieve the toArray method and invoke it
jmethodID mToArray = env->GetMethodID(cList, "toArray", "()[Ljava/lang/Object;");
jobjectArray array = (jobjectArray)env->CallObjectMethod(jList, mToArray);
// now create the string array
string
sArray = new string[env->GetArrayLength(array)];

  1. for(int i=0;i<env->GetArrayLength(array);i++) {
  2. // retrieve the chars of the entry strings and assign them to the array!
  3. jstring strObj = (jstring)env->GetObjectArrayElement(array, i);
  4. const char * chr = env->GetStringUTFChars(strObj, NULL);
  5. sArray[i].append(chr);
  6. env->ReleaseStringUTFChars(strObj, chr);
  7. }
  8. // just print the array to std::cout
  9. for(int i=0;i<env->GetArrayLength(array);i++) {
  10. //print value
  11. cout << sArray[i] << endl;
  12. const char* p = sArray[i].data();
  13. hll.add(p,10);
  14. }
  15. //print estimate value
  16. cout<<hll.estimate()<<endl;
  17. //输出到二进制流
  18. std::ostringstream sstream;
  19. hll.dump(sstream);
  20. const std::string nativeString = sstream.str(); //转换成字符串
  21. jbyteArray arr = env->NewByteArray(nativeString.length());
  22. env->SetByteArrayRegion(arr,0,nativeString.length(),(jbyte*)nativeString.c_str()); //存入到二进制array传回Java
  23. return arr;

}

This is my JNI.h

/* DO NOT EDIT THIS FILE - it is machine generated /
#include <jni.h>
/
Header for class JNI */

#ifndef _Included_JNI
#define _Included_JNI
#ifdef __cplusplus
extern "C" {
#endif
/*

  • Class: JNI
  • Method: call
  • Signature: (Ljava/util/ArrayList;)[B
    */
    JNIEXPORT jbyteArray JNICALL Java_JNI_call
    (JNIEnv *, jobject, jobject);

#ifdef __cplusplus
}
#endif
#endif

英文:

I try to call cpp in Java. After packaging into Jar, such an error occurs after running.
I can run these codes in the Linux system, but I regret reporting the error after packaging them into a jar.

How should I modify my program? Is there a naming problem?

  1. com.tencent.oceanus.jar.program.ProgramInvocationException: org.apache.flink.client.program.ProgramInvocationException: The program caused an error:
  2. at com.tencent.oceanus.jar.CliFrontend.executeProgram(CliFrontend.java:248)
  3. at com.tencent.oceanus.jar.CliFrontend.getJobGraph(CliFrontend.java:168)
  4. at com.tencent.oceanus.server.jobs.JobManager.compile(JobManager.java:257)
  5. at com.tencent.oceanus.server.jobs.transitions.StartTransitionCallback$StartThread.start(StartTransitionCallback.java:236)
  6. at com.tencent.oceanus.server.jobs.transitions.StartTransitionCallback$StartThread.run(StartTransitionCallback.java:142)
  7. at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  8. at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  9. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  10. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  11. at java.lang.Thread.run(Thread.java:748)
  12. Caused by: org.apache.flink.client.program.ProgramInvocationException: The program caused an error:
  13. at org.apache.flink.client.program.OptimizerPlanEnvironment.getOptimizedPlan(OptimizerPlanEnvironment.java:93)
  14. at com.tencent.oceanus.jar.program.ClusterClient.getOptimizedPlan(ClusterClient.java:244)
  15. at com.tencent.oceanus.jar.program.ClusterClient.run(ClusterClient.java:316)
  16. at com.tencent.oceanus.jar.program.ClusterClient.run(ClusterClient.java:285)
  17. at com.tencent.oceanus.jar.CliFrontend.executeProgram(CliFrontend.java:242)
  18. ... 9 more
  19. Caused by: java.lang.UnsatisfiedLinkError: com.tencent.wxapp.core.jni.JNI.call(Ljava/util/List;)[B
  20. at com.tencent.wxapp.core.jni.JNI.call(Native Method)
  21. at com.tencent.wxapp.core.jni.JNITest.main(JNITest.java:30)
  22. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  23. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  24. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  25. at java.lang.reflect.Method.invoke(Method.java:498)
  26. at com.tencent.oceanus.jar.program.CustomPackagedProgram.callMainMethod(CustomPackagedProgram.java:314)
  27. at com.tencent.oceanus.jar.program.CustomPackagedProgram.invokeInteractiveModeForExecution(CustomPackagedProgram.java:213)
  28. at org.apache.flink.client.program.OptimizerPlanEnvironment.getOptimizedPlan(OptimizerPlanEnvironment.java:83)
  29. ... 13 more

This is my JNI.java

  1. package com.tencent.wxapp.core.jni;
  2. import java.io.*;
  3. import java.util.List;
  4. public class JNI{
  5. public native byte[] call(List&lt;String&gt; jList);
  6. static{
  7. InputStream is = JNI.class.getResourceAsStream(&quot;/JNIdll.so&quot;);
  8. File file = null;
  9. try {
  10. file = File.createTempFile(&quot;lib&quot;, &quot;.so&quot;);
  11. } catch (IOException e) {
  12. e.printStackTrace();
  13. }
  14. OutputStream os = null;
  15. try {
  16. os = new FileOutputStream(file);
  17. } catch (FileNotFoundException e) {
  18. e.printStackTrace();
  19. }
  20. byte[] buffer = new byte[1024];
  21. int length = 0;
  22. while (true) {
  23. try {
  24. if (!((length = is.read(buffer)) != -1)) break;
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. try {
  29. os.write(buffer, 0, length);
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. try {
  35. is.close();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. try {
  40. os.close();
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. System.load(file.getAbsolutePath());
  45. file.deleteOnExit();
  46. }
  47. }

This is my JNI.cpp

  1. //JNIdll.cpp文件
  2. #include&lt;stdio.h&gt;
  3. #include&lt;iostream&gt;
  4. #include &lt;strstream&gt;
  5. #include &quot;hyperloglog.hpp&quot;
  6. #include &lt;vector&gt;
  7. #include &lt;string&gt;
  8. #include &lt;fstream&gt;
  9. #include &quot;JNI.h&quot;
  10. using namespace hll;
  11. using namespace std;
  12. JNIEXPORT jbyteArray JNICALL Java_JNI_call
  13. (JNIEnv *env, jobject obj,jobject jList){
  14. //实现代码
  15. HyperLogLog hll(10);
  16. // retrieve the java.util.List interface class
  17. jclass cList = env-&gt;FindClass(&quot;java/util/List&quot;);
  18. // retrieve the toArray method and invoke it
  19. jmethodID mToArray = env-&gt;GetMethodID(cList, &quot;toArray&quot;, &quot;()[Ljava/lang/Object;&quot;);
  20. jobjectArray array = (jobjectArray)env-&gt;CallObjectMethod(jList, mToArray);
  21. // now create the string array
  22. string* sArray = new string[env-&gt;GetArrayLength(array)];
  23. for(int i=0;i&lt;env-&gt;GetArrayLength(array);i++) {
  24. // retrieve the chars of the entry strings and assign them to the array!
  25. jstring strObj = (jstring)env-&gt;GetObjectArrayElement(array, i);
  26. const char * chr = env-&gt;GetStringUTFChars(strObj, NULL);
  27. sArray[i].append(chr);
  28. env-&gt;ReleaseStringUTFChars(strObj, chr);
  29. }
  30. // just print the array to std::cout
  31. for(int i=0;i&lt;env-&gt;GetArrayLength(array);i++) {
  32. //print value
  33. cout &lt;&lt; sArray[i] &lt;&lt; endl;
  34. const char* p = sArray[i].data();
  35. hll.add(p,10);
  36. }
  37. //print estimate value
  38. cout&lt;&lt;hll.estimate()&lt;&lt;endl;
  39. //输出到二进制流
  40. std::ostringstream sstream;
  41. hll.dump(sstream);
  42. const std::string nativeString = sstream.str(); //转换成字符串
  43. jbyteArray arr = env-&gt;NewByteArray(nativeString.length());
  44. env-&gt;SetByteArrayRegion(arr,0,nativeString.length(),(jbyte*)nativeString.c_str()); //存入到二进制array传回Java
  45. return arr;
  46. }

This is my JNI.h

  1. /* DO NOT EDIT THIS FILE - it is machine generated */
  2. #include &lt;jni.h&gt;
  3. /* Header for class JNI */
  4. #ifndef _Included_JNI
  5. #define _Included_JNI
  6. #ifdef __cplusplus
  7. extern &quot;C&quot; {
  8. #endif
  9. /*
  10. * Class: JNI
  11. * Method: call
  12. * Signature: (Ljava/util/ArrayList;)[B
  13. */
  14. JNIEXPORT jbyteArray JNICALL Java_JNI_call
  15. (JNIEnv *, jobject, jobject);
  16. #ifdef __cplusplus
  17. }
  18. #endif
  19. #endif

答案1

得分: 1

由于原生方法声明为com.tencent.wxapp.core.jni.JNI.call,Java正在寻找共享库中名为Java_com_tencent_wxapp_core_jni_JNI_call的方法。您已导出为Java_JNI_call

英文:

Since the native method is declared as com.tencent.wxapp.core.jni.JNI.call, Java is looking for a method in the shared library called Java_com_tencent_wxapp_core_jni_JNI_call. You have exported Java_JNI_call.

huangapple
  • 本文由 发表于 2020年8月4日 21:50:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63248342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定