Java Native Interface 入门示例
鉴于网上搜到的都是基于jdk1.4或以前版本,而且本地库用的是C语言。而现在是基于C++,所以更新记录如下:
第一步:创建Java源码文件
public class Hello{
static{
System.loadLibrary("Hello");
}
public Hello(){
}
public native void sayHello(String strName);
public static void main(String[] args){
Hello hello = new Hello();
hello.sayHello("fuye");
}
}
第二步:编译
javac Hello.java
第三部:生成C++文件头
javah Hello
Hello.h文件内容如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Hello */
#ifndef _Included_Hello
#define _Included_Hello
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Hello
* Method: sayHello
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_Hello_sayHello
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
第四步:新建C++实现文件
#include "Hello.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_Hello_sayHello(JNIEnv * env, jobject arg, jstring instring){
printf("This is my first jni program\n");
}
第五步: 编译,连接成本地库
1、生成Hello.o
g++ -I /usr/local/dev/java/jdk1.5.0_16/include/ -I /usr/local/dev/java/jdk1.5.0_16/include/linux/ -c Hello.cpp
2、生成libHello.so.1.0
g++ -shared -W1,-soname,libHell
相关文档:
List的用法
List包括List接口以及List接口的所有实现类。因为List接口实现了Collection接口,所以List接口拥有Collection接口提供的所有常用方法,又因为List是列表类型,所以List接口还提供了一些适合于自身的常用方法,如表1所示。
表1 List接口定义的常用方法及功能
从表1可以看出,List接口提供的适合于自身的 ......
插入式排序运行效率N*(N-1)/4 对于随机数字,这个算法比冒泡快1倍,比选择排序稍微快一点.
如果是基本有序的队列则优势最为明显需要O(N)
代码一样是从冒泡排序继承下来的.
/**
*
* @author leon.lee
*/
public class InsertSort extends BubbleSort {
public InsertSort(int lengthArray){
......
原帖地址:
http://coolshell.cn/?p=2235
----------------我是紫苑最萌的分割线XD--------------
概述:
本文主要研究的是JAVA的字符串拼接的性能,原文中的测试代码在功能上并不等价,导致concat的测
试意义不大。不过原作者在评论栏给了新的concat结果,如果有兴趣的同学建议自己修改代码测试。
原文出处:http://ww ......
再次从网上查询,搜到了RXTXcomm.jar包比较好,是封装了comm.jar的方法。
安装:
1.copy rxtxSerial.dll to [JDK-directory]\jre\bin\rxtxSerial.dll
2.copy RXTXcomm.jar to [JDK-directory]\jre\lib\ext\RXTXcomm.jar
&nbs ......