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
相关推荐
yaoxin5211232 小时前
286. Java Stream API - 使用Stream.iterate(...)创建流
java·开发语言
qq_12498707532 小时前
基于springboot的鸣珮乐器销售网站的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·spring·毕业设计·计算机毕业设计
海南java第二人2 小时前
SpringBoot核心注解@SpringBootApplication深度解析:启动类的秘密
java·spring boot·后端
win x2 小时前
Redis集群
java·数据库·redis
r_oo_ki_e_2 小时前
java23--异常
java·开发语言
qq_12498707532 小时前
基于Spring Boot的“味蕾探索”线上零食购物平台的设计与实现(源码+论文+部署+安装)
java·前端·数据库·spring boot·后端·小程序
编程之路从0到12 小时前
React Native 之Android端 Bolts库
android·前端·react native
爬山算法2 小时前
Hibernate(38)如何在Hibernate中配置乐观锁?
android·java·hibernate