ÔÚLinuxƽ̨ÏÂÈçºÎʵÏÖ±¾µØ¹²Ïí¿âÓëJavaÐͬ¹¤×÷¡£Hello World³ÌÐòÊÇÄ¿Ç°±ê×¼µÄÈëÃŵÚÒ»²½£¬ÄÇô£¬ÎÒÒ²ÒÔÀàËƵÄÓ¦ÓÃ×îΪÑùÀý¡£
µÚÒ»²½£¬¶¨ÒåÒ»¸ö Java Àà -- Hello. ËüÌṩSayHello·½·¨£º
´ËʱӦעÒâÁ½µã£º
1¡¢ÎªÒªÊ¹ÓõÄÿ¸ö±¾µØ·½·¨±àд±¾µØ·½·¨ÉùÃ÷£¬ÆäÉùÃ÷·½Ê½ÓëÆÕͨJava·½·¨½Ó¿Úûʲô²»Í¬£¬Ö»ÊDZØÐëÖ¸¶¨ native ¹Ø¼ü×Ö£¬ÈçÏÂËùʾ£º
public native void SayHello(String strName);
ÔÚÕâ¸öº¯ÊýÖУ¬ÎÒÃǽ«¸ù¾Ý´«½øµÄÈËÃû£¬ÏòijÈËÎʺá£
2¡¢±ØÐëÏÔʽµØ¼ÓÔر¾µØ´úÂë¿â¡£ÎÒÃÇÐèÔÚÀàµÄÒ»¸ö¾²Ì¬¿éÖмÓÔØÕâ¸ö¿â£º
static
{
System.loadLibrary("hello");
}
ÔÙ¼ÓÉϱØÒªµÄÒì³£´¦Àí¾ÍÉú³ÉÈçÏÂÔ´ÎļþHello.java£º
public class Hello
{
static
{
try
{
//´Ë´¦¼´Îª±¾µØ·½·¨ËùÔÚÁ´½Ó¿âÃû
System.loadLibrary("hello");
}
catch(UnsatisfiedLinkError e)
{
System.err.println
( "Cannot load hello library:\n " +e.toString() );
}
}
public Hello()
{
}
//ÉùÃ÷µÄ±¾µØ·½·¨
public native void SayHello(String strName);
}
±àÒëºóÉú³ÉHello.classÎļþ¡£
µÚ¶þ²½£¬Éú³É±¾µØÁ´½Ó¿â¡£¾ßÌå¹ý³ÌÈçÏ£º
1¡¢ÒªÎªÒÔÉ϶¨ÒåµÄÀàÉú³É Java ±¾µØ½Ó¿ÚÍ·Îļþ£¬ÐèʹÓÃjavah£¬Java ±àÒëÆ÷µÄ javah ¹¦Äܽ«¸ù¾ÝHelloÀàÉú³É±ØÒªµÄÉùÃ÷£¬´ËÃüÁÉú³ÉHello.hÎļþ£¬ÎÒÃÇÔÚ¹²Ïí¿âµÄ´úÂëÖÐÒª°üº¬Ëü£¬javah²»Ê¹Ä¬ÈÏÄÚ²¿ÃüÁÐèÒªÖ¸Ã÷·¾¶£¬ËüÔÚJDKµÄbinĿ¼Ï£¬ÔÚÎÒµÄLinux»·¾³ÏÂÃüÁîÈçÏ£º
/home/jbuilder/jdk1.3.1/bin/javah Hello
Éú³ÉµÄHello.h Îļþ ÄÚÈÝÈçÏÂËùʾ£º
/* DO NOT EDIT THIS FILE -
it is machine generated */
#include
/* 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