Android java 学习笔记2

目录

10th_exception

Div.java

java 复制代码
/* java Div 6 2
 * 6/2=3
 */

public class Div {

	public static void main(String args[]) {
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);

		System.out.println("Begin of div");
		int r = div(m, n);
		System.out.println("End of div");

		System.out.println(m+"/"+n+"="+r);
		
	}

	public static int div(int m, int n) {
		int r = m / n;
		return r;
	}
}

Div2.java

java 复制代码
/* java Div 6 2
 * 6/2=3
 */

public class Div2 {

	public static void main(String args[]) {
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);

		System.out.println("Begin of div");
		int r = div(m, n);
		System.out.println("End of div");

		System.out.println(m+"/"+n+"="+r);
		
	}

	public static int div(int m, int n) {
		int r = 0;
		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println(e);
		} finally {
			System.out.println("this is finally of div");
		}
		return r;
	}
}

Div3.java

java 复制代码
/* java Div 6 2
 * 6/2=3
 */

public class Div3 {

	public static void main(String args[]) {
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);

		System.out.println("Begin of div");
		int r = div(m, n);
		System.out.println("End of div");

		System.out.println(m+"/"+n+"="+r);
		
	}

	public static int div(int m, int n) throws ArithmeticException {
		int r = 0;
		r = m / n;
		return r;
	}
}

Div4.java

java 复制代码
/* java Div 6 2
 * 6/2=3
 */

public class Div4 {

	public static void main(String args[]) {
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);
		int r = 0;
		
		System.out.println("Begin of div");
		try {
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println(e);
		}
		System.out.println("End of div");

		System.out.println(m+"/"+n+"="+r);
		
	}

	public static int div(int m, int n) throws ArithmeticException {
		int r = 0;
		r = m / n;
		return r;
	}
}

Div5.java

java 复制代码
/* java Div 6 2
 * 6/2=3
 */

public class Div5 {

	public static void main(String args[]) {
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);
		int r = 0;
		
		System.out.println("Begin of div");
		try {
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println(e);
		}
		System.out.println("End of div");

		System.out.println(m+"/"+n+"="+r);
		
	}

	public static int div(int m, int n) throws ArithmeticException {
		
		int r = 0;

		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div :"+e);
		}
		return r;
	}
}

Div6.java

java 复制代码
/* java Div 6 2
 * 6/2=3
 */

public class Div6 {

	public static void main(String args[]) {
		int m = Integer.parseInt(args[0]);
		int n = Integer.parseInt(args[1]);
		int r = 0;
		
		System.out.println("Begin of div");
		try {
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println("main :"+e);
		}
		System.out.println("End of div");

		System.out.println(m+"/"+n+"="+r);
		
	}

	public static int div(int m, int n) throws ArithmeticException {
		
		int r = 0;

		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div :"+e);
			throw e;
		}
		return r;
	}
}

Div7.java

java 复制代码
/* java Div 6 2
 * 6/2=3
 */

public class Div7 {

	public static void main(String args[]) {
		int m = 0;
		int n = 0;
		int r = 0;
		
		System.out.println("Begin of div");
		try {
		    m = Integer.parseInt(args[0]);
		    n = Integer.parseInt(args[1]);
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println("main :"+e);
		} catch (NumberFormatException e) {
			System.out.println("main :"+e);
		} catch (RuntimeException e) {
			System.out.println("main :"+e);
		}
		System.out.println("End of div");

		System.out.println(m+"/"+n+"="+r);
		
	}

	public static int div(int m, int n) throws ArithmeticException {
		
		int r = 0;

		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div :"+e);
			throw e;
		}
		return r;
	}
}

Div8.java

java 复制代码
/* java Div 6 2
 * 6/2=3
 */

public class Div8 {

	public static void main(String args[]) {
		int m = 0;
		int n = 0;
		int r = 0;
		
		System.out.println("Begin of div");
		try {
		    m = Integer.parseInt(args[0]);
		    n = Integer.parseInt(args[1]);
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println("main :"+e);
		} catch (NumberFormatException e) {
			System.out.println("main :"+e);
		} catch (RuntimeException e) {
			System.out.println("main :"+e);
		}
		System.out.println("End of div");

		System.out.println(m+"/"+n+"="+r);
		
	}

	public static int div(int m, int n) throws ArithmeticException {
		
		int r = 0;

		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div :"+e);
			throw e;
		} finally {
			System.out.println("finally of div");
			return r;
		}
		//return r;
	}
}

Div9.java

java 复制代码
/* java Div 6 2
 * 6/2=3
 */

public class Div9 {

	public static void main(String args[]) {
		int m = 0;
		int n = 0;
		int r = 0;
		
		System.out.println("Begin of div");
		try {
		    m = Integer.parseInt(args[0]);
		    n = Integer.parseInt(args[1]);
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println("main :"+e);
		} catch (NumberFormatException e) {
			System.out.println("main :"+e);
		} catch (RuntimeException e) {
			System.out.println("main :"+e);
		}
		System.out.println("End of div");

		System.out.println(m+"/"+n+"="+r);
		
	}

//	public static int div(int m, int n) throws ArithmeticException {
	public static int div(int m, int n) {		
		int r = 0;

		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div :"+e);
			throw e;
		} finally {
			System.out.println("finally of div");
			//return r;
		}
		return r;
	}
}

Div10.java

java 复制代码
/* java Div 6 2
 * 6/2=3
 */

public class Div10 {

	public static void main(String args[]) {
		int m = 0;
		int n = 0;
		int r = 0;
		
		System.out.println("Begin of div");
		try {
		    m = Integer.parseInt(args[0]);
		    n = Integer.parseInt(args[1]);
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println("main :"+e);
		} catch (NumberFormatException e) {
			System.out.println("main :"+e);
		} catch (RuntimeException e) {
			System.out.println("main :"+e);
		} catch (Exception e) {
			System.out.println("main :"+e);
		}
		
		System.out.println("End of div");

		System.out.println(m+"/"+n+"="+r);
		
	}

//	public static int div(int m, int n) throws ArithmeticException {
	public static int div(int m, int n) throws Exception {		
		int r = 0;

		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div :"+e);
			throw new Exception("My Error");
		} finally {
			System.out.println("finally of div");
			//return r;
		}
		return r;
	}
}

Div11.java

java 复制代码
/* java Div 6 2
 * 6/2=3
 */

public class Div11 {

	public static void main(String args[]) {
		int m = 0;
		int n = 0;
		int r = 0;
		
		System.out.println("Begin of div");
		try {
		    m = Integer.parseInt(args[0]);
		    n = Integer.parseInt(args[1]);
			r = div(m, n);
		} catch (ArithmeticException e) {
			System.out.println("main :"+e);
		} catch (NumberFormatException e) {
			System.out.println("main :"+e);
		} catch (RuntimeException e) {
			System.out.println("main :"+e);
		} catch (Exception e) {
			System.out.println("main :"+e);
		}
		
		System.out.println("End of div");

		System.out.println(m+"/"+n+"="+r);
		
	}

//	public static int div(int m, int n) throws ArithmeticException {
	public static int div(int m, int n) {		
		int r = 0;

		try {
			r = m / n;
		} catch (ArithmeticException e) {
			System.out.println("div :"+e);
			try {
				throw new Exception("My Error");
			} catch (Exception e2) {
				System.out.println("div :"+ e2);
			}
		} finally {
			System.out.println("finally of div");
			//return r;
		}
		return r;
	}
}

11th_package

01

Pack.java

java 复制代码
package a.b.c.d;

public class Pack {
	public static void main(String args[]) {
		System.out.println("Hello, world!");
	}
}

02

lisi

文件夹里的Math.java

java 复制代码
package a.b.c.d1;

public class Math {
	public static int add(int x, int y) {
		return x + y;
	}
}

zhangsan

文件夹里的Math.java

java 复制代码
package a.b.c.d2;

public class Math {
	public static int sub(int x, int y) {
		return x - y;
	}

	public static int add(int x, int y) {
		return x + y + 2;
	}

}

文件夹里的Print.java

java 复制代码
package a.b.c.d2;

public class Print {
	public static void printInfo() {
		System.out.println("package: a.b.c.d2");
	}

}

根目录里的Pack.java

java 复制代码
import a.b.c.d1.*;
import a.b.c.d2.*;


public class Pack {
	public static void main(String args[]) {
		/* add */
		System.out.println(a.b.c.d1.Math.add(1,2));
		System.out.println(a.b.c.d2.Math.add(1,2));

		/* sub */
		System.out.println(a.b.c.d2.Math.sub(1,2));

		a.b.c.d2.Print.printInfo();
		Print.printInfo();
	}
}

12th_access_control

01

a

文件夹里的

Pack.class 打不开

b

文件夹里的Mymath.class 打不开

根目录下:

Mymath.java

java 复制代码
package b;

class Mymath {
}

Pack.java

java 复制代码
package a;
import b.*;

public class Pack {
	public static void main(String args[]) {
		Mymath m = new Mymath();
	}
}

02

Mymath.java

java 复制代码
package a;

class Mymath {
}

Pack.java

java 复制代码
package a;
//import b.*;

public class Pack {
	public static void main(String args[]) {
		Mymath m = new Mymath();
	}
}

03

Mymath.java

java 复制代码
package a;

class Mymath {
	//private int x;
	int x;
}

Pack.java

java 复制代码
package a;
//import b.*;

public class Pack {
	public static void main(String args[]) {
		Mymath m = new Mymath();
		m.x = 0;
	}
}

04

Mymath.java

java 复制代码
package b;

public class Mymath {
	//private int x;
	//int x;
	protected int x;
}

Pack.java

java 复制代码
package a;
import b.*;

public class Pack {
	public static void main(String args[]) {
		Mymath m = new Mymath();
		m.x = 0;
	}
}

05

Mymath.java

java 复制代码
package b;

public class Mymath {
	//private int x;
	//int x;
	protected int x;
}

Pack.java

java 复制代码
package a;
import b.*;

class Mysubmath extends Mymath {
	void printInfo() {
		System.out.println("Mysubmath : x = "+x);
	}
}

public class Pack {
	public static void main(String args[]) {
		Mysubmath m = new Mysubmath();
		//m.x = 0;
		m.printInfo();
	}
}

13th_jni

01

native.c

c 复制代码
#include <jni.h>  /* /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ */
#include <stdio.h>

#if 0
typedef struct {
    char *name;          /* Java里调用的函数名 */
    char *signature;    /* JNI字段描述符, 用来表示Java里调用的函数的参数和返回值类型 */
    void *fnPtr;          /* C语言实现的本地函数 */
} JNINativeMethod;
#endif

void c_hello(JNIEnv *env, jobject cls)
{
	printf("Hello, world!\n");
}


static const JNINativeMethod methods[] = {
	{"hello", "()V", (void *)c_hello},
};




/* System.loadLibrary */
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
	JNIEnv *env;
	jclass cls;

	if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {
		return JNI_ERR; /* JNI version not supported */
	}
	cls = (*env)->FindClass(env, "JNIDemo");
	if (cls == NULL) {
		return JNI_ERR;
	}

	/* 2. map java hello <-->c c_hello */
	if ((*env)->RegisterNatives(env, cls, methods, 1) < 0)
		return JNI_ERR;

	return JNI_VERSION_1_4;
}

JNIDemo.java

java 复制代码
public class JNIDemo {
	static { 		/* 1. load */
		System.loadLibrary("native"); /* libnative.so */
 	}
	public native void hello();
	public static void main (String args[]) {
		JNIDemo d = new JNIDemo();		

		/* 2. map java hello <-->c c_hello */

		/* 3. call */
		d.hello();
	}
}

readme.txt

bash 复制代码
javac JNIDemo.java
gcc -I /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/include/ -I /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0/include/linux/ -fPIC -shared -o libnative.so native.c
export LD_LIBRARY_PATH=.
java JNIDemo 

02

native.c

c 复制代码
#include <jni.h>  /* /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ */
#include <stdio.h>

#if 0
typedef struct {
    char *name;          /* Java里调用的函数名 */
    char *signature;    /* JNI字段描述符, 用来表示Java里调用的函数的参数和返回值类型 */
    void *fnPtr;          /* C语言实现的本地函数 */
} JNINativeMethod;
#endif

jint c_hello(JNIEnv *env, jobject cls, jint m)
{
	printf("Hello, world! val = %d\n", m);
	return 100;
}


static const JNINativeMethod methods[] = {
	{"hello", "(I)I", (void *)c_hello},
};




/* System.loadLibrary */
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
	JNIEnv *env;
	jclass cls;

	if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {
		return JNI_ERR; /* JNI version not supported */
	}
	cls = (*env)->FindClass(env, "JNIDemo");
	if (cls == NULL) {
		return JNI_ERR;
	}

	/* 2. map java hello <-->c c_hello */
	if ((*env)->RegisterNatives(env, cls, methods, 1) < 0)
		return JNI_ERR;

	return JNI_VERSION_1_4;
}

JNIDemo.java

java 复制代码
public class JNIDemo {
	static { 		/* 1. load */
		System.loadLibrary("native"); /* libnative.so */
 	}
	public native int hello(int m);
	public static void main (String args[]) {
		JNIDemo d = new JNIDemo();		

		/* 2. map java hello <-->c c_hello */

		/* 3. call */
		System.out.println(d.hello(123));
	}
}

readme.txt

bash 复制代码
javac JNIDemo.java
gcc -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ -fPIC -shared -o libnative.so native.c
export LD_LIBRARY_PATH=.
java JNIDemo 

03

native.c

c 复制代码
#include <jni.h>  /* /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ */
#include <stdio.h>

#if 0
typedef struct {
    char *name;          /* Java里调用的函数名 */
    char *signature;    /* JNI字段描述符, 用来表示Java里调用的函数的参数和返回值类型 */
    void *fnPtr;          /* C语言实现的本地函数 */
} JNINativeMethod;
#endif

jstring JNICALL c_hello(JNIEnv *env, jobject cls, jstring str)
{
	//printf("this is c : %s\n", str);
	//return "return from C";

	const jbyte *cstr;
	cstr = (*env)->GetStringUTFChars(env, str, NULL);
	if (cstr == NULL) {
		return NULL; /* OutOfMemoryError already thrown */
	}
	printf("Get string from java :%s\n", cstr);
	(*env)->ReleaseStringUTFChars(env, str, cstr);

	return (*env)->NewStringUTF(env, "return from c");
}


static const JNINativeMethod methods[] = {
	{"hello", "(Ljava/lang/String;)Ljava/lang/String;", (void *)c_hello},
};




/* System.loadLibrary */
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
	JNIEnv *env;
	jclass cls;

	if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {
		return JNI_ERR; /* JNI version not supported */
	}
	cls = (*env)->FindClass(env, "JNIDemo");
	if (cls == NULL) {
		return JNI_ERR;
	}

	/* 2. map java hello <-->c c_hello */
	if ((*env)->RegisterNatives(env, cls, methods, 1) < 0)
		return JNI_ERR;

	return JNI_VERSION_1_4;
}

JNIDemo.java

java 复制代码
public class JNIDemo {
	static { 		/* 1. load */
		System.loadLibrary("native"); /* libnative.so */
 	}
	public native String hello(String str);
	public static void main (String args[]) {
		JNIDemo d = new JNIDemo();		

		/* 2. map java hello <-->c c_hello */

		/* 3. call */
		System.out.println(d.hello("this is java"));
	}
}

readme.txt

bash 复制代码
javac JNIDemo.java
gcc -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ -fPIC -shared -o libnative.so native.c
export LD_LIBRARY_PATH=.
java JNIDemo 

04

native.c

c 复制代码
#include <jni.h>  /* /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ */
#include <stdio.h>

#if 0
typedef struct {
    char *name;          /* Java里调用的函数名 */
    char *signature;    /* JNI字段描述符, 用来表示Java里调用的函数的参数和返回值类型 */
    void *fnPtr;          /* C语言实现的本地函数 */
} JNINativeMethod;
#endif

jint c_hello(JNIEnv *env, jobject cls, jintArray arr)
{
	jint *carr;
	jint i, sum = 0;
	carr = (*env)->GetIntArrayElements(env, arr, NULL);
	if (carr == NULL) {
		return 0; /* exception occurred */
	}
	for (i=0; i< (*env)->GetArrayLength(env, arr); i++) {
		sum += carr[i];
	}
	(*env)->ReleaseIntArrayElements(env, arr, carr, 0);
	return sum;
}


static const JNINativeMethod methods[] = {
	{"hello", "([I)I", (void *)c_hello},
};




/* System.loadLibrary */
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
	JNIEnv *env;
	jclass cls;

	if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {
		return JNI_ERR; /* JNI version not supported */
	}
	cls = (*env)->FindClass(env, "JNIDemo");
	if (cls == NULL) {
		return JNI_ERR;
	}

	/* 2. map java hello <-->c c_hello */
	if ((*env)->RegisterNatives(env, cls, methods, 1) < 0)
		return JNI_ERR;

	return JNI_VERSION_1_4;
}

JNIDemo.java

java 复制代码
public class JNIDemo {
	static { 		/* 1. load */
		System.loadLibrary("native"); /* libnative.so */
 	}
	public native int hello(int[] a);
	public static void main (String args[]) {
		JNIDemo d = new JNIDemo();	
		int [] a = {1, 2, 3}; 

		/* 2. map java hello <-->c c_hello */

		/* 3. call */
		System.out.println(d.hello(a));
	}
}

readme.txt

bash 复制代码
javac JNIDemo.java
gcc -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ -fPIC -shared -o libnative.so native.c
export LD_LIBRARY_PATH=.
java JNIDemo 

05

native.c

c 复制代码
#include <jni.h>  /* /usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ */
#include <stdio.h>
#include <stdlib.h>
 
#if 0
typedef struct {
    char *name;          /* Java里调用的函数名 */
    char *signature;    /* JNI字段描述符, 用来表示Java里调用的函数的参数和返回值类型 */
    void *fnPtr;          /* C语言实现的本地函数 */
} JNINativeMethod;
#endif

jintArray c_hello(JNIEnv *env, jobject cls, jintArray arr)
{
	jint *carr;
	jint *oarr;
	jintArray rarr;
	
	jint i, n = 0;
	carr = (*env)->GetIntArrayElements(env, arr, NULL);
	if (carr == NULL) {
		return 0; /* exception occurred */
	}

	n = (*env)->GetArrayLength(env, arr);
	oarr = malloc(sizeof(jint) * n);
	if (oarr == NULL)
	{
		(*env)->ReleaseIntArrayElements(env, arr, carr, 0);
		return 0;
	}

	for (i = 0; i < n; i++)
	{
		oarr[i] = carr[n-1-i];
	}
	
	(*env)->ReleaseIntArrayElements(env, arr, carr, 0);

	/* create jintArray */
	rarr = (*env)->NewIntArray(env, n);
	if (rarr == NULL)
	{
		return 0;
	}

	(*env)->SetIntArrayRegion(env, rarr, 0, n, oarr);
	free(oarr);
	
	return rarr;
}


static const JNINativeMethod methods[] = {
	{"hello", "([I)[I", (void *)c_hello},
};




/* System.loadLibrary */
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
	JNIEnv *env;
	jclass cls;

	if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_4)) {
		return JNI_ERR; /* JNI version not supported */
	}
	cls = (*env)->FindClass(env, "JNIDemo");
	if (cls == NULL) {
		return JNI_ERR;
	}

	/* 2. map java hello <-->c c_hello */
	if ((*env)->RegisterNatives(env, cls, methods, 1) < 0)
		return JNI_ERR;

	return JNI_VERSION_1_4;
}

JNIDemo.java

java 复制代码
public class JNIDemo {
	static { 		/* 1. load */
		System.loadLibrary("native"); /* libnative.so */
 	}
	public native int[] hello(int[] a);
	public static void main (String args[]) {
		JNIDemo d = new JNIDemo();	
		int [] a = {1, 2, 3}; 
		int [] b = null;
		int i;

		/* 2. map java hello <-->c c_hello */

		/* 3. call */
		b = d.hello(a);

		for (i = 0; i < b.length; i++)		
			System.out.println(b[i]);
	}
}

readme.txt

bash 复制代码
javac JNIDemo.java
gcc -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ -fPIC -shared -o libnative.so native.c
export LD_LIBRARY_PATH=.
java JNIDemo 

06th_c_call_java

01th_call_static_method

caller.c

c 复制代码
#include <stdio.h>  
#include <jni.h> 


jint create_vm(JavaVM** jvm, JNIEnv** env) 
{  
    JavaVMInitArgs args;  
    JavaVMOption options[1];  
    args.version = JNI_VERSION_1_6;  
    args.nOptions = 1;  
    options[0].optionString = "-Djava.class.path=./";  
    args.options = options;  
    args.ignoreUnrecognized = JNI_FALSE;  
    return JNI_CreateJavaVM(jvm, (void **)env, &args);  
}  


int main(int argc, char **argv)
{
	JavaVM* jvm;
	JNIEnv* env;

	jclass cls;
	int ret = 0;

	jmethodID mid;
		
	/* 1. create java virtual machine */
	if (create_vm(&jvm, &env)) {
		printf("can not create jvm\n");
		return -1;
	}

	/* 2. get class */
	cls = (*env)->FindClass(env, "Hello");
	if (cls == NULL) {
		printf("can not find hello class\n");
		ret = -1;
		goto destroy;
	}

	/* 3. create object */

	/* 4. call method
	 * 4.1 get method
	 * 4.2 create parameter
	 * 4.3 call method
	 */

	mid = (*env)->GetStaticMethodID(env, cls, "main","([Ljava/lang/String;)V");
	if (mid == NULL) {
		ret = -1;
		printf("can not get method\n");
		goto destroy;
	}

	(*env)->CallStaticVoidMethod(env, cls, mid, NULL);

destroy:

	(*jvm)->DestroyJavaVM(jvm);
	return ret;
}

Hello.java

java 复制代码
public class Hello {
	public static void main(String args[]) {
		System.out.println("Hello, world!");
	}

    public static void sayhello_to(String name) {
    }

    public static void sayhello_to() {
    }
}

readme.txt

bash 复制代码
javac Hello.java

javap -p -s Hello.class // get Signature

gcc -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ -o caller caller.c -L /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server -ljvm

LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server ./caller

02th_call_non_static_metod

caller.c

c 复制代码
#include <stdio.h>  
#include <jni.h> 


jint create_vm(JavaVM** jvm, JNIEnv** env) 
{  
    JavaVMInitArgs args;  
    JavaVMOption options[1];  
    args.version = JNI_VERSION_1_6;  
    args.nOptions = 1;  
    options[0].optionString = "-Djava.class.path=./";  
    args.options = options;  
    args.ignoreUnrecognized = JNI_FALSE;  
    return JNI_CreateJavaVM(jvm, (void **)env, &args);  
}  


int main(int argc, char **argv)
{
	JavaVM* jvm;
	JNIEnv* env;

	jclass cls;
	int ret = 0;

	jmethodID mid;
	jmethodID cid;

	jobject jobj;
	jstring jstr;

	int r;
		
	/* 1. create java virtual machine */
	if (create_vm(&jvm, &env)) {
		printf("can not create jvm\n");
		return -1;
	}

	/* 2. get class */
	cls = (*env)->FindClass(env, "Hello");
	if (cls == NULL) {
		printf("can not find hello class\n");
		ret = -1;
		goto destroy;
	}

	/* 3. create object 
	 * 3.1 get constructor method
	 * 3.2 create parameters
	 * 3.3 NewObject
	 */

	/* Get the method ID for the String constructor */
	cid = (*env)->GetMethodID(env, cls,	"<init>", "()V");
	if (cid == NULL) {
		ret = -1;
		printf("can not get constructor method");
		goto destroy;
	}

	jobj = (*env)->NewObject(env, cls, cid);
	if (jobj == NULL) {
		ret = -1;
		printf("can not create object");
		goto destroy;
	}

	/* 4. call method
	 * 4.1 get method
	 * 4.2 create parameter
	 * 4.3 call method
	 */

	mid = (*env)->GetMethodID(env, cls, "sayhello_to","(Ljava/lang/String;)I");
	if (mid == NULL) {
		ret = -1;
		printf("can not get method\n");
		goto destroy;
	}

	jstr = (*env)->NewStringUTF(env, "weidongshan@qq.com");

	r = (*env)->CallIntMethod(env, jobj, mid, jstr);
	printf("ret = %d\n", r);

destroy:

	(*jvm)->DestroyJavaVM(jvm);
	return ret;
}

Hello.java

java 复制代码
public class Hello {
	public static void main(String args[]) {
		System.out.println("Hello, world!");
	}

    public int sayhello_to(String name) {
		System.out.println("Hello, "+name);       
        return 123;
    }

    public static void sayhello_to() {
    }
}

readme.txt

bash 复制代码
javac Hello.java

javap -p -s Hello.class // get Signature

gcc -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ -o caller caller.c -L /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server -ljvm

LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server ./caller

03th_set_field

caller.c

c 复制代码
#include <stdio.h>  
#include <jni.h> 


jint create_vm(JavaVM** jvm, JNIEnv** env) 
{  
    JavaVMInitArgs args;  
    JavaVMOption options[1];  
    args.version = JNI_VERSION_1_6;  
    args.nOptions = 1;  
    options[0].optionString = "-Djava.class.path=./";  
    args.options = options;  
    args.ignoreUnrecognized = JNI_FALSE;  
    return JNI_CreateJavaVM(jvm, (void **)env, &args);  
}  


int main(int argc, char **argv)
{
	JavaVM* jvm;
	JNIEnv* env;

	jclass cls;
	int ret = 0;

	jmethodID mid;
	jmethodID cid;

	jobject jobj;
	jstring jstr;

	jfieldID nameID;
	jfieldID ageID;

	int r;
		
	/* 1. create java virtual machine */
	if (create_vm(&jvm, &env)) {
		printf("can not create jvm\n");
		return -1;
	}

	/* 2. get class */
	cls = (*env)->FindClass(env, "Hello");
	if (cls == NULL) {
		printf("can not find hello class\n");
		ret = -1;
		goto destroy;
	}

	/* 3. create object 
	 * 3.1 get constructor method
	 * 3.2 create parameters
	 * 3.3 NewObject
	 */

	/* Get the method ID for the String constructor */
	cid = (*env)->GetMethodID(env, cls,	"<init>", "()V");
	if (cid == NULL) {
		ret = -1;
		printf("can not get constructor method");
		goto destroy;
	}

	jobj = (*env)->NewObject(env, cls, cid);
	if (jobj == NULL) {
		ret = -1;
		printf("can not create object");
		goto destroy;
	}

	/* get/set field
	 * 1. get field id
	 * 2. get/set field
	 */

	nameID = (*env)->GetFieldID(env, cls, "name", "Ljava/lang/String;");
	if (nameID == NULL) {
		ret = -1;
		printf("can not get field name");
		goto destroy;
	}
	jstr = (*env)->NewStringUTF(env, "Bill");
	(*env)->SetObjectField(env, jobj, nameID, jstr);

	ageID = (*env)->GetFieldID(env, cls, "age", "I");
	if (ageID == NULL) {
		ret = -1;
		printf("can not get field age");
		goto destroy;
	}
	(*env)->SetIntField(env, jobj, ageID, 10);

	/* 4. call method
	 * 4.1 get method
	 * 4.2 create parameter
	 * 4.3 call method
	 */

	mid = (*env)->GetMethodID(env, cls, "sayhello_to","(Ljava/lang/String;)I");
	if (mid == NULL) {
		ret = -1;
		printf("can not get method\n");
		goto destroy;
	}

	jstr = (*env)->NewStringUTF(env, "weidongshan@qq.com");

	r = (*env)->CallIntMethod(env, jobj, mid, jstr);
	printf("ret = %d\n", r);

destroy:

	(*jvm)->DestroyJavaVM(jvm);
	return ret;
}

Hello.java

java 复制代码
public class Hello {
    private String name;
    private int age;
    
	public static void main(String args[]) {
		System.out.println("Hello, world!");
	}

    public int sayhello_to(String name) {
		System.out.println("Hello, "+name+"! I am "+this.name+", "+age+" years old.");       
        return 123;
    }

    public static void sayhello_to() {
    }
}

readme.txt

bash 复制代码
javac Hello.java

javap -p -s Hello.class // get Signature

gcc -I/usr/lib/jvm/java-1.7.0-openjdk-amd64/include/ -o caller caller.c -L /usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server -ljvm

LD_LIBRARY_PATH=/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server ./caller
相关推荐
Resean02232 小时前
架构设计系列(十六):DDD 与代码架构
java·开发语言·架构
学长毕设3 小时前
基于SpringBoot 车辆租赁系统平台-附源码
java·spring boot·后端
Flynt5 小时前
Spring AI Alibaba 真停更了?我把它的 GitHub 翻了个底朝天
java·spring·开源
三少爷的鞋5 小时前
为什么成熟 Android 项目几乎看不到 IdleHandler?
android
kururunga6 小时前
Android知识点回顾之蓝牙协议
android·蓝牙·车载开发
StarsGreen7 小时前
aosp中的SELinux 策略配置和编译流程
android
她的男孩8 小时前
Spring Boot 接 Flowable:请假单要同时走财务和行政,包容分支和默认分支怎么配
java·spring boot·工作流引擎
咖啡八杯9 小时前
LoginUser 适配器模式:适配 SysUser 到 UserDetails
java·开发语言·后端·框架·适配器模式·若依
北冥有鱼10 小时前
从 bean::method 里抠出注解:Java 方法引用元数据反查全记录
java·后端
CoderYanger10 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第四弹):@ControllerAdvice 源码分析+案例代码
java·数据库·spring boot·后端·spring·java-ee·mybatis