有关C调java遇到问题总结

遇到问题(一):

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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值