linux java 调用so文件

pwd
/home/xxx/apache-tomcat-8.0.51/webapps/test

touch test.c

vim test.c

#include
int add(int a,int b);
int add(int a,int b)
{
	int c = a + b ;
	return c ;
} 

gcc -fpic -c test.c
gcc -shared -z execstack  -o libtest.so test.o
ls


touch TestSo.java
vim TestSo.java

import com.sun.jna.Library;
import com.sun.jna.Native;
public class TestSo {
	public interface LgetLib extends Library {
		LgetLib INSTANCE = (LgetLib) Native.loadLibrary("test",LgetLib.class);
		int add(int a,int b);
	}
	public int add(int a,int b){
		return LgetLib.INSTANCE.add(a,b);
	}
	public static void main(String[] args) {
		TestSo ts = new TestSo();
		int c = ts.add(10,20);
		System.out.println("10+20="+c);
	}
}

export CLASSPATH=$CLASSPATP:/home/xxx/apache-tomcat-8.0.51/webapps/test/lib/jna-3.0.9.jar
export LD_LIBRARY_PATH=/home/xxx/apache-tomcat-8.0.51/webapps/test
javac TestSo.java

java TestSo

 

你可能感兴趣的:(JAVA)