遇到问题(一):
java.lang.NoSuchMethodError: no static method with name='add' signature='(II)I' in class Ltest/test;
找问题思路:
1. 或检查对应java类中,add函数接口是否被“static”修饰:
package test;
public class test {
public int add(int a, int b)
{
return a + b;
}
}
如果没有被“static”修饰,加上
2. 或检查对应的jar包类中,add接口是否被“static”修饰:
// Method descriptor #15 (II)I
// Stack: 2, Locals: 3
public int add(int a, int b);
0 iload_1 [a]
1 iload_2 [b]
2 iadd
3 ireturn
如果没有加上
遇到问题(二):执行GetEnv时崩溃,JNI_OnLoad()没有执行
过程:
1. 在关键代码的地方加打印信息,发现JNI_OnLoad()函数没有执行;
2. 网上查一下原因,System.loadLibrary 没有加载动态库, 添加上就好了;
注:java头文件里已经loadlibrary , 不知道啥原因这里还需要load一次
由C转的java头文件如下:
package com.example.jnidemo2;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
/**
* JNA Wrapper for library <b>testAdd</b><br>
* This file was autogenerated by <a href="http://jnaerator.googlecode.com/">JNAerator</a>,<br>
* a tool written by <a href="http://ochafik.com/">Olivier Chafik</a> that <a href="http://code.google.com/p/jnaerator/wiki/CreditsAndLicense">uses a few opensource projects.</a>.<br>
* For help, please visit <a href="http://nativelibs4java.googlecode.com/">NativeLibs4Java</a> , <a href="http://rococoa.dev.java.net/">Rococoa</a>, or <a href="http://jna.dev.java.net/">JNA</a>.
*/
public interface TestAdd extends Library {
public static final String JNA_LIBRARY_NAME = "jniDemo2";
public static final NativeLibrary JNA_NATIVE_LIB = NativeLibrary.getInstance(TestAdd.JNA_LIBRARY_NAME);
public static final TestAdd INSTANCE = (TestAdd)Native.loadLibrary(TestAdd.JNA_LIBRARY_NAME, TestAdd.class);
/**
* Original signature : <code>int add(int, int)</code><br>
* <i>native declaration : line 14</i>
*/
int add(int a, int b);
}