Java±¾µØ½Ó¿Ú(Java Native Interface (JNI))
JNI¹¤×÷ÔÀí
ÔÚJNIÖУ¬±¾µØº¯ÊýÊÇͨ¹ýÒ»¸ö¶ÀÁ¢µÄ.c»ò.cppÎļþÀ´ÊµÏÖµÄ(C++ΪJNIÌṩµÄ½çÃæ»á¸ü¼ò½àһЩ)¡£µ±JVMµ÷Óøú¯Êýʱ£¬Ëü´«µÝÁËÒ»¸öJNIEnvÖ¸Õë¡¢Ò»¸öjobjectÖ¸ÕëºÍͨ¹ýJava·½·¨¶¨ÒåµÄJava²ÎÊý£¬JNIº¯ÊýµÄÐÎʽÈçÏ£º
JNIEXPORT void JNICALL Java_ClassName_MethodName
(JNIEnv *env, jobjectobj)
{
//Method native implemenation
}
envÖ¸ÕëÊÇÒ»¸ö°üº¬ÁËJVM½Ó¿ÚµÄ½á¹¹£¬Ëü°üº¬ÁËÓëJVM½øÐн»»¥ÒÔ¼°ÓëJava¶ÔÏóÐͬ¹¤×÷Ëù±ØÐèµÄº¯Êý£¬Ê¾ÀýÖеÄJNIº¯Êý¿ÉÒÔÔÚ±¾µØÊý×éºÍJavaÊý×éÀàÐÍÖ®¼ä¡¢±¾µØ×Ö·û´®ºÍJava×Ö·û´®ÀàÐÍÖ®¼ä½øÐÐת»»£¬Æ书ÄÜ»¹°üÀ¨¶ÔÏóµÄʵÀý»¯¡¢Å׳öÒì³£µÈ¡£»ù±¾ÉÏÄú¿ÉÒÔʹÓÃJNIEnvÀ´ÊµÏÖËùÓÐJavaÄÜ×öµ½µÄÊÂÇ飬ËäȻҪ¼òµ¥ºÜ¶à¡£
¸ü¼ÓÕýʽµÄ½âÊÍÊÇÕâÑùµÄ£¬±¾µØ´úÂëͨ¹ýµ÷ÓÃJNIµÄº¯ÊýÀ´·ÃÎÊJVM£¬ÕâÊÇͨ¹ýÒ»¸ö½çÃæÖ¸ÕëʵÏֵģ¨½çÃæÖ¸Õëʵ¼ÊÉÏÊÇÖ¸ÏòÖ¸ÕëµÄÖ¸Õ룩£¬¸ÃÖ¸ÕëÖ¸ÏòÒ»¸öÖ¸ÕëÊý×飬Êý×éÖеÄÿ¸öÖ¸Õ붼ָÏòÁËÒ»¸ö½çÃ溯Êý£¬¶øÿ¸ö½çÃ溯Êý¶¼ÊÇÔÚÊý×éÖÐÔ¤Ïȶ¨Òå¹ýµÄ¡£
±¾µØ·½·¨½«JNI½çÃæÖ¸Õëµ±×÷Ò»¸ö²ÎÊý£¬Èç¹ûÔÚͬһ¸öJavaÏß³ÌÖУ¬³öÏֶԸñ¾µØ·½·¨µÄ¶àÖص÷Óã¬JVMÔò±£Ö¤´«µÝÏàͬµÄ½çÃæÖ¸Õëµ½±¾µØ·½·¨¡£²»¹ý£¬Ò»¸ö±¾µØ·½·¨¿ÉÒÔ±»²»Í¬µÄJavaÏ̵߳÷Óã¬Òò¶øÒ²¿ÉÄÜ»áÊÕµ½²»Í¬µÄJNI½çÃæÖ¸Õë¡£
±¾µØ·½·¨ÊÇͨ¹ýSystem.loadLibrary·½·¨¼ÓÔصģ¬ÔÚÒÔϵÄÀý×ÓÖУ¬ÀàµÄ³õʼ»¯·½·¨¼ÓÔØÁËÒ»¸öÖ¸¶¨Æ½Ì¨µÄ±¾µØÀà¿â£¬¸ÃÀà¿â¶¨ÒåÁ˱¾µØ·½·¨£º
packagepkg;
class Cls {
native double f(inti, String s);
static {
System.loadLibrary(pkg_Cls");
}
}
system.loadLibrary·½·¨µÄ²ÎÊýÊÇÒ»¸öÀà¿âµÄÃû³Æ£¬Ëü¿ÉÒÔÓɳÌÐòÔ±ÈÎÒâÑ¡È¡£¬ÏµÍ³Ôò×ñÑÒ»¸ö±ê×¼µÄ±¾µØ»¯Æ½Ì¨µÄ·½Ê½À´×ª»»Àà¿âµÄÃû³Æµ½Ò»¸ö±¾µØÀà¿âµÄÃû³Æ¡£ÀýÈ磬ÔÚSolaris²Ù×÷ϵͳÖлὫpkg_Clsת»»Îªlibpkg_Cls.so£¬¶øWin32ϵͳÔò»á½«Í¬ÑùµÄpkg_Clsת»»Îªpkg_Cls.dll¡£
¡¡¡¡¶¯Ì¬Ö¸Õë»á¸ù¾ÝËüÃǵÄÃû×ÖÀ´½øÐнâÎö£¬Ò»¸ö±¾µØ·½·¨µÄÃû³ÆÊÇ°´ÕÕ×é¼þ½øÐÐÁ¬½ÓµÄ£¬Ëü°üº¬ÁË£ºÇ°×º“Java_”¡¢Ò»¸ö·ÖÀëµÄºÏ·¨µÄÀàÃû³ÆºÍÒ»¸ö·ÖÀëµÄ·½·¨Ãû³Æ¡£
×¢Ò⣺΢ÈíµÄJVMÓÐÏàͬµÄ»úÖÆ´ÓJavaµ÷Óñ¾µØWindows´úÂ룬¸Ã»úÖƱ»³ÆΪÔʼ±¾µØ½Ó¿Ú(Raw Native
Ïà¹ØÎĵµ£º
Ò»£® Incident
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
......
¶ÔÓÚÕâ¸öϵÁÐÀïµÄÎÊÌ⣬ÿ¸öѧJavaµÄÈ˶¼Ó¦¸Ã¸ã¶®¡£µ±È»£¬Èç¹ûÖ»ÊÇѧJavaÍæÍæ¾ÍÎÞËùνÁË¡£Èç¹ûÄãÈÏΪ×Ô¼ºÒѾ³¬Ô½³õѧÕßÁË£¬È´²»ºÜ¶®ÕâЩÎÊÌ⣬Ç뽫Äã×Ô¼ºÖعé³õѧÕßÐÐÁС£ÄÚÈݾùÀ´×ÔÓÚCSDNµÄ¾µäÀÏÌù¡£
ÎÊÌâÒ»£ºÎÒÉùÃ÷ÁËʲô£¡
String s = "Hello world!";
Ðí¶àÈ˶¼×ö¹ýÕâÑùµÄÊÂÇ飬µ«ÊÇ£¬ÎÒÃǵ½µ×ÉùÃ÷ÁËʲô£¿»Ø´ðͨ³£ ......
ÐòÁл¯µÄ¹ý³Ì¾ÍÊǶÔÏóдÈë×Ö½ÚÁ÷ºÍ´Ó×Ö½ÚÁ÷ÖжÁÈ¡¶ÔÏó¡£½«¶ÔÏó״̬ת»»³É×Ö½ÚÁ÷Ö®ºó£¬¿ÉÒÔÓÃjava.io°üÖеĸ÷ÖÖ×Ö½ÚÁ÷ÀཫÆä±£´æµ½ÎļþÖУ¬¹ÜµÀµ½ÁíÒ»Ïß³ÌÖлòͨ¹ýÍøÂçÁ¬½Ó½«¶ÔÏóÊý¾Ý·¢Ë͵½ÁíÒ»Ö÷»ú¡£¶ÔÏóÐòÁл¯¹¦Äܷdz£¼òµ¥¡¢Ç¿´ó£¬ÔÚRMI¡¢Socket¡¢JMS¡¢EJB¶¼ÓÐÓ¦Ó᣶ÔÏóÐòÁл¯ÎÊÌâÔÚÍøÂç±à³ÌÖв¢²»ÊÇ× ......
Java Learning Path £¨Ò»£©¡¢¹¤¾ßƪ
Ò»¡¢ JDK (Java Development Kit)
JDKÊÇÕû¸öJavaµÄºËÐÄ£¬°üÀ¨ÁËJavaÔËÐл·¾³£¨Java Runtime Envirnment£©£¬Ò»¶ÑJava¹¤¾ßºÍJava»ù´¡µÄÀà¿â(rt.jar)¡£²»ÂÛʲôJavaÓ¦Ó÷þÎñÆ÷ʵÖʶ¼ÊÇÄÚÖÃÁËij¸ö°æ±¾µÄJDK¡£Òò´ËÕÆÎÕJDKÊÇѧºÃJavaµÄµÚÒ»²½¡£×îÖ÷Á÷µÄJDKÊÇSun¹«Ë¾·¢²¼µÄJDK£¬³ýÁËSunÖ®Íâ ......
(1)JavaÖеÄÖ÷·½·¨public static void main(String args[])ΪʲôҪÓÃstaticÀ´ÐÞÊÎ
ÒòΪjavaÊÇÍêÈ«ÃæÏò¶ÔÏóÓïÑÔ,Õâ¸öÖ÷º¯ÊýÆäʵÊÇÒ»¸öÀàµÄ·½·¨,Õâ¸ö·½·¨ÔÚÀàûÓÐÉú³É¶ÔÏóµÄʱºò¾Í±ØÐë±»JVMµ÷ÓÃ,ËùÒÔËü±ØÐëÊǾ²Ì¬µÄ³ÉÔ±º¯Êý.
£¨2£©javaÓïÑÔÖеÄpublic static void main(String[] args) ×÷ÓÃÊÇʲô.Ëù×öµÄÊÂÇéÓÖÊÇʲ ......