Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux编程 > » 正文

Step By Step(Java 本地方法篇)(13)

来源: 未知 分享至:
->FindClass(\"java/lang/NullPointerException\"),
18 \"Printf.fprint: format is NULL\");
19 return;
20 }
21 const char* cformat = env->GetStringUTFChars(format,NULL);
22 char* cstr = (char*)calloc(strlen(cformat) + 20,sizeof(char));
23 if (cstr == NULL) {
24 env->ThrowNew(env->FindClass(\"java/lang/OutOfMemoryError\")
25 ,\"Printf.fprint: malloc failed\");
26 return;
27 }
28 sprintf(cstr,cformat,x);
29 env->ReleaseStringUTFChars(format,cformat);
30 //调用PrintWriter.print(char)方法。
31 jclass class_PrintWriter = env->GetObjectClass(out);
32 jmethodID id_print = env->GetMethodID(class_PrintWriter,\"print\",\"(C)V\");
33 //ExceptionOccurred将返回Java中抛出的异常,如果为NULL则表示没有异常出现。
34 //如果不希望知道具体的异常,可以调用jboolean ExceptionCheck()方法来判断
35 //是否异常发生了。如果C语言的代码在获悉异常的类型后,可以自行处理该异常,
36 //那么在处理之后,需要调用JNI函数ExceptionClear()来清除JVM传递过来的异常。
37 for (int i = 0; cstr[i] != 0 && !env->ExceptionOccurred(); ++i) {
38 env->CallVoidMethod(out,id_print,cstr[i]);
39 }
40 free(cstr);
41 }

    3)    含有main函数的调用测试代码:

 1     import java.io.PrintWriter;
2 public class MyTest {
3 public static void main(String[] args) throws Exception {
4 double price = 44.95;
5 double tax = 7.75;
6 double amountDue = price * (1 + tax / 100);
7 PrintWriter out = new PrintWriter(System.out);
8 //This call will throw an exception
9

Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史