ºìÁªLinuxÃÅ»§
Linux°ïÖú

jniÐèҪעÒâµÄ³£¼ûÎÊÌâ

·¢²¼Ê±¼ä:2011-12-12 12:34:18À´Ô´:ºìÁª×÷Õß:ÎíÔÂOÇïË®
×î½üѧϰÁËjni£¬×ܽáһЩÐĵÃÌåÑ飬ϣÍû¸ø´ó¼ÒÓеã°ïÖú¡£ÔÚѧϰjniµÄ¹ý³ÌÖУ¬Óöµ½Ò»Ð©³£¼ûµÄÎÊÌâ¡£ÔÚÕâÀï¾Í²»ÔÙ̸¼°ÔõÑùµ÷ÓÃjniµÄ²½Ö裬ÍøÉϲéÕÒ¼´¿É£¬ÔÚ´ËÍƼöһƪдµÄµ÷ÓúÜÈ«µÄÎÄÕ£ºhttp://netcome.iteye.com/blog/monthblog/2010-06?show_full=true

ÏÂÃæÊÇÎÒÔÚ±àд¹ý³ÌÖÐÓöµ½µÄÎÊÌ⣺
1.ÊäÈëµÄÎÊÌ⣺C/C++ÓïÑÔµÄ.c»òÕß.c++µÄÎļþÒ»¶¨Òª¾¡Á¿ÊÖ´òÊäÈ룬ÕâÑù¿ÉÒÔ±ÜÃâ¸ñʽÉÏÊäÈëµÄ´íÎó¡£ÀýÈ磺
testjni.cpp:10: error: stray ¡®\240¡¯ in program
testjni.cpp:10: error: stray ¡®\302¡¯ in program
testjni.cpp:10: error: stray ¡®\240¡¯ in program
testjni.cpp:14: error: stray ¡®\302¡¯ in program
testjni.cpp:14: error: stray ¡®\240¡¯ in program
testjni.cpp:14: error: stray ¡®\302¡¯ in program
testjni.cpp:14: error: stray ¡®\240¡¯ in program
testjni.cpp:16: error: stray ¡®\302¡¯ in program
testjni.cpp:16: error: stray ¡®\302¡¯ in program
testjni.cpp:16: error: stray ¡®\302¡¯ in program
testjni.cpp:16: error: stray ¡®\302¡¯ in program
Õâ¾ÍÊÇÒ»¸öºÜÃ÷ÏÔÓÉÓÚ´ÓÍøÒ³Éϸ´ÖÆÏÂÀ´Õ³Ìù¶øÒýÆðµÄ¿Õ¸ñÓëlinuxϵÄspace¼ü²»Í¬µÄÎÊÌ⣬×îºÃ×Ô¼ºÊÖ¶¯ÊäÈ룬»òÕßÌæ»»£¬Ìæ»»²»Ò»¶¨ºÃ¡£ÔÚдnativeº¯ÊýµÄʱºò×îºÃÒ²´ÓÏà¶ÔÓ¦µÄ.hÎļþ¸´Öƺ¯ÊýÃûÓë²ÎÊý¡£

2. ÕÒ²»µ½ÏàÓ¦µÄ¿âÎļþÁ´½ÓµÄÎÊÌ⣬È磺
Exception in thread "main" java.lang.UnsatisfiedLinkError: no testjni1 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at Weiwei.(Weiwei.java:9)
Could not find the main class: Weiwei. Program will exit.
ÁÙʱ½â¾öµÄ°ì·¨£º
a.ÉèÖÃexport LD_LIBRARY_PATH=./ ¼´¿É£¬ÔÚÊä³ö¾Í¿ÉÒÔµÄÁË
b.java -Djava.library.path=. -cp . ˈ̞
c.¿ÉÒÔµ÷ÓÃSystem.out.print(System.getProperty("java.library.path")); À´²é¿´£¡ È»ºó°ÑlibXXXX.so£¨windows»·¾³½«.dllÎļþ£©¿½±´µ½ÄÇÀïÃæµÄĿ¼ÏÂÈ¥
3.LinuxϱàÒëÏÂÕÒ²»µ½¿âÎļþ·ûºÅµÄÎÊÌ⣺libXXX.so: undefined symbol: _ZdlPv
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
ÕâÊÇÓÉgccÓëg++ÓõIJ»µ±Ôì³ÉµÄ¡£¾ßÌåµÄÓ÷¨ÊÇ£º
jstr=env->NewStringUTF(str);ÊÇC++ÖеÄд·¨£¬²»±ØʹÓÃenvÖ¸Õë¡£ÒòΪJNIEnvº¯ÊýµÄC++°æ±¾°üº¬ÓÐÖ±½Ó²åÈë³ÉÔ±º¯Êý£¬ËûÃǸºÔð²éÕÒº¯ÊýÖ¸Õë¡£¶ø¶ÔÓÚCµÄд·¨£¬Ó¦¸ÄΪ£ºjstr=(*env)->NewStringUTF(env,str);ÒòΪËùÓÐJNIº¯ÊýµÄµ÷Óö¼Ê¹ÓÃenvÖ¸Õ룬ËüÊÇÈÎÒâÒ»¸ö±¾µØ·½·¨µÄµÚÒ»¸ö²ÎÊý¡£envÖ¸ÕëÊÇÖ¸ÏòÒ»¸öº¯ÊýÖ¸Õë±íµÄÖ¸Õë¡£Òò´ËÔÚÿ¸öJNIº¯Êý·ÃÎÊÇ°¼Óǰ׺(*env)->£¬ÒÔÈ·±£¼ä½ÓÒýÓú¯ÊýÖ¸Õ룬Òò¶ø¿ÉÒÔÒÔ´ËÀ´Åжϡ£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2 ÌõÆÀÂÛ

  1. Dsucov ÓÚ 2011-12-12 16:36:00·¢±í:

    ±¾ÈË»¹¿´²»¶®£¬555555

  2. ÁúÍõÒ¯88 ÓÚ 2011-12-12 13:49:42·¢±í:

    ²»´í£¬ÕýºÃÐèҪŶ{:2_91:}