Skip to content

mahongquan/java-jni

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ChangeLog
copy *.c into src
remove folder bin,jni
add genInclude.sh,makefile,compile.sh,run.sh

Reference
http://my.unix-center.net/~Simon_fu/?p=359
http://java.sun.com/docs/books/jni/html/jniTOC.html

more articles
http://markmail.org/message/4b3ugju6o76v4g73

Be careful about the method signature.
in c header file I use
JNIEXPORT void JNICALL Java_org_xkit_demo_jni_HelloJNI_print
  (JNIEnv *, jobject);

but in c body I make a mistake, I omit the "Java" string at the start of the method name, I use
JNIEXPORT void JNICALL
org_xkit_demo_jni_HelloJNI_print(JNIEnv *env, jobject obj)

so an Exception produced at runtime

Exception in thread "main" java.lang.UnsatisfiedLinkError: org.xkit.demo.jni.HelloJNI.print()V
	at org.xkit.demo.jni.HelloJNI.print(Native Method)
	at org.xkit.demo.jni.HelloJNI.main(HelloJNI.java:7)

when call Java method from C, I encounter a fatal error:
# Problematic frame:
# V  [libjvm.so+0x414414]  jni_invoke_nonstatic(JNIEnv_*, JavaValue*, _jobject*, JNICallType, _jmethodID*, JNI_ArgumentPusher*, Thread*)+0x114
the full crash log is hs_err_pid31906.log

when I put jni.initNative() just before jni.call(), it actually works ok.
it is so weird, because before that, the code order is just like this
I think the variables do not get modified by others, so it should works when I put jni.initNative() in the beginning of the code

jni.print();
jni.print("I'm from Java layer");
jni.print("我是从Java层传递过来的");

System.out.println(jni.echo(2, "am i low case?"));

int[] data = { 7, 4, 5, 63, 3 };
data = jni.draw(data);

printArray(data);
jni.initNative();
jni.call();

compile & run steps

$ cc -I/home/guohai/dev/a/java/jdk1.6.0_25/include/linux/ -I/home/guohai/dev/a/java/jdk1.6.0_25/include/ -fPIC -shared -o libHelloJNI.so org_xkit_demo_jni_HelloJNI.c


run this demo from cmd line:
java -Djava.library.path=$SO_FILE_PATH org.xkit.demo.jni.HelloJNI

or from Eclise, but do not forget to add -Djava.library.path=$SO_FILE_PATH to VM arguments




I wrote another JNI demo on Windows platform years ago, I almost forget that.
the hyper link is http://lucane.iteye.com/blog/466187


guohai@KNIGHT:~/dev/workspace/github/0x01/hello-jni/bin$ javap -s -p org/xkit/demo/jni/HelloJNI
this command can get the exact method signature

guohai@KNIGHT:~/dev/workspace/github/0x01/hello-jni/jni$ ldd libHelloJNI.so 
	linux-gate.so.1 =>  (0x008d0000)
	libc.so.6 => /lib/libc.so.6 (0x00749000)
	/lib/ld-linux.so.2 (0x00d0d000)


guohai@KNIGHT:~/dev/workspace/github/0x01/hello-jni/jni$ nm -D libHelloJNI.so
         w _Jv_RegisterClasses
00002010 A __bss_start
         w __cxa_finalize
         w __gmon_start__
00002010 A _edata
00002018 A _end
000004b8 T _fini
00000320 T _init
0000044c T org_xkit_demo_jni_HelloJNI_print
         U puts


guohai@KNIGHT:~/dev/workspace/github/0x01/hello-jni/jni$ objdump -tT libHelloJNI.so 

libHelloJNI.so:     file format elf32-i386

SYMBOL TABLE:
000000f4 l    d  .note.gnu.build-id	00000000              .note.gnu.build-id
00000118 l    d  .gnu.hash	00000000              .gnu.hash
00000154 l    d  .dynsym	00000000              .dynsym
00000204 l    d  .dynstr	00000000              .dynstr
000002a2 l    d  .gnu.version	00000000              .gnu.version
000002b8 l    d  .gnu.version_r	00000000              .gnu.version_r
000002e8 l    d  .rel.dyn	00000000              .rel.dyn
00000308 l    d  .rel.plt	00000000              .rel.plt
00000320 l    d  .init	00000000              .init
00000350 l    d  .plt	00000000              .plt
00000390 l    d  .text	00000000              .text
000004b8 l    d  .fini	00000000              .fini
000004d4 l    d  .rodata	00000000              .rodata
000004e0 l    d  .eh_frame	00000000              .eh_frame
00001f14 l    d  .ctors	00000000              .ctors
00001f1c l    d  .dtors	00000000              .dtors
00001f24 l    d  .jcr	00000000              .jcr
00001f28 l    d  .dynamic	00000000              .dynamic
00001fe8 l    d  .got	00000000              .got
00001ff4 l    d  .got.plt	00000000              .got.plt
0000200c l    d  .data	00000000              .data
00002010 l    d  .bss	00000000              .bss
00000000 l    d  .comment	00000000              .comment
00000000 l    df *ABS*	00000000              crtstuff.c
00001f14 l     O .ctors	00000000              __CTOR_LIST__
00001f1c l     O .dtors	00000000              __DTOR_LIST__
00001f24 l     O .jcr	00000000              __JCR_LIST__
00000390 l     F .text	00000000              __do_global_dtors_aux
00002010 l     O .bss	00000001              completed.7065
00002014 l     O .bss	00000004              dtor_idx.7067
00000410 l     F .text	00000000              frame_dummy
00000000 l    df *ABS*	00000000              crtstuff.c
00001f18 l     O .ctors	00000000              __CTOR_END__
000004e0 l     O .eh_frame	00000000              __FRAME_END__
00001f24 l     O .jcr	00000000              __JCR_END__
00000480 l     F .text	00000000              __do_global_ctors_aux
00000000 l    df *ABS*	00000000              org_xkit_demo_jni_HelloJNI.c
00001ff4 l     O *ABS*	00000000              _GLOBAL_OFFSET_TABLE_
0000200c l     O .data	00000000              __dso_handle
00001f20 l     O .dtors	00000000              __DTOR_END__
00000447 l     F .text	00000000              __i686.get_pc_thunk.bx
00001f28 l     O *ABS*	00000000              _DYNAMIC
00000000  w      *UND*	00000000              __gmon_start__
00000000  w      *UND*	00000000              _Jv_RegisterClasses
0000044c g     F .text	00000026              org_xkit_demo_jni_HelloJNI_print
000004b8 g     F .fini	00000000              _fini
00002010 g       *ABS*	00000000              __bss_start
00002018 g       *ABS*	00000000              _end
00000000       F *UND*	00000000              puts@@GLIBC_2.0
00002010 g       *ABS*	00000000              _edata
00000000  w    F *UND*	00000000              __cxa_finalize@@GLIBC_2.1.3
00000320 g     F .init	00000000              _init


DYNAMIC SYMBOL TABLE:
00000000  w   D  *UND*	00000000              __gmon_start__
00000000  w   D  *UND*	00000000              _Jv_RegisterClasses
00000000      DF *UND*	00000000  GLIBC_2.0   puts
00000000  w   DF *UND*	00000000  GLIBC_2.1.3 __cxa_finalize
00002018 g    D  *ABS*	00000000  Base        _end
00002010 g    D  *ABS*	00000000  Base        _edata
0000044c g    DF .text	00000026  Base        org_xkit_demo_jni_HelloJNI_print
00002010 g    D  *ABS*	00000000  Base        __bss_start
00000320 g    DF .init	00000000  Base        _init
000004b8 g    DF .fini	00000000  Base        _fini


guohai@KNIGHT:~/dev/workspace/github/0x01/hello-jni/jni$ nm --demangle libHelloJNI.so 
00001f28 a _DYNAMIC
00001ff4 a _GLOBAL_OFFSET_TABLE_
         w _Jv_RegisterClasses
00001f18 d __CTOR_END__
00001f14 d __CTOR_LIST__
00001f20 d __DTOR_END__
00001f1c d __DTOR_LIST__
000004e0 r __FRAME_END__
00001f24 d __JCR_END__
00001f24 d __JCR_LIST__
00002010 A __bss_start
         w __cxa_finalize@@GLIBC_2.1.3
00000480 t __do_global_ctors_aux
00000390 t __do_global_dtors_aux
0000200c d __dso_handle
         w __gmon_start__
00000447 t __i686.get_pc_thunk.bx
00002010 A _edata
00002018 A _end
000004b8 T _fini
00000320 T _init
00002010 b completed.7065
00002014 b dtor_idx.7067
00000410 t frame_dummy
0000044c T org_xkit_demo_jni_HelloJNI_print
         U puts@@GLIBC_2.0


guohai@KNIGHT:~/dev/workspace/github/0x01/hello-jni/jni$ readelf -s libHelloJNI.so 

Symbol table '.dynsym' contains 11 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
     2: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
     3: 00000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.0 (2)
     4: 00000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@GLIBC_2.1.3 (3)
     5: 0000045c    38 FUNC    GLOBAL DEFAULT   11 Java_org_xkit_demo_jni_He
     6: 00002018     0 NOTYPE  GLOBAL DEFAULT  ABS _end
     7: 00002010     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
     8: 00002010     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
     9: 00000324     0 FUNC    GLOBAL DEFAULT    9 _init
    10: 000004c8     0 FUNC    GLOBAL DEFAULT   12 _fini

Symbol table '.symtab' contains 53 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 000000f4     0 SECTION LOCAL  DEFAULT    1 
     2: 00000118     0 SECTION LOCAL  DEFAULT    2 
     3: 00000154     0 SECTION LOCAL  DEFAULT    3 
     4: 00000204     0 SECTION LOCAL  DEFAULT    4 
     5: 000002a6     0 SECTION LOCAL  DEFAULT    5 
     6: 000002bc     0 SECTION LOCAL  DEFAULT    6 
     7: 000002ec     0 SECTION LOCAL  DEFAULT    7 
     8: 0000030c     0 SECTION LOCAL  DEFAULT    8 
     9: 00000324     0 SECTION LOCAL  DEFAULT    9 
    10: 00000354     0 SECTION LOCAL  DEFAULT   10 
    11: 000003a0     0 SECTION LOCAL  DEFAULT   11 
    12: 000004c8     0 SECTION LOCAL  DEFAULT   12 
    13: 000004e4     0 SECTION LOCAL  DEFAULT   13 
    14: 000004f0     0 SECTION LOCAL  DEFAULT   14 
    15: 00001f14     0 SECTION LOCAL  DEFAULT   15 
    16: 00001f1c     0 SECTION LOCAL  DEFAULT   16 
    17: 00001f24     0 SECTION LOCAL  DEFAULT   17 
    18: 00001f28     0 SECTION LOCAL  DEFAULT   18 
    19: 00001fe8     0 SECTION LOCAL  DEFAULT   19 
    20: 00001ff4     0 SECTION LOCAL  DEFAULT   20 
    21: 0000200c     0 SECTION LOCAL  DEFAULT   21 
    22: 00002010     0 SECTION LOCAL  DEFAULT   22 
    23: 00000000     0 SECTION LOCAL  DEFAULT   23 
    24: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    25: 00001f14     0 OBJECT  LOCAL  DEFAULT   15 __CTOR_LIST__
    26: 00001f1c     0 OBJECT  LOCAL  DEFAULT   16 __DTOR_LIST__
    27: 00001f24     0 OBJECT  LOCAL  DEFAULT   17 __JCR_LIST__
    28: 000003a0     0 FUNC    LOCAL  DEFAULT   11 __do_global_dtors_aux
    29: 00002010     1 OBJECT  LOCAL  DEFAULT   22 completed.7065
    30: 00002014     4 OBJECT  LOCAL  DEFAULT   22 dtor_idx.7067
    31: 00000420     0 FUNC    LOCAL  DEFAULT   11 frame_dummy
    32: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
    33: 00001f18     0 OBJECT  LOCAL  DEFAULT   15 __CTOR_END__
    34: 000004f0     0 OBJECT  LOCAL  DEFAULT   14 __FRAME_END__
    35: 00001f24     0 OBJECT  LOCAL  DEFAULT   17 __JCR_END__
    36: 00000490     0 FUNC    LOCAL  DEFAULT   11 __do_global_ctors_aux
    37: 00000000     0 FILE    LOCAL  DEFAULT  ABS org_xkit_demo_jni_HelloJN
    38: 00001ff4     0 OBJECT  LOCAL  DEFAULT  ABS _GLOBAL_OFFSET_TABLE_
    39: 0000200c     0 OBJECT  LOCAL  DEFAULT   21 __dso_handle
    40: 00001f20     0 OBJECT  LOCAL  DEFAULT   16 __DTOR_END__
    41: 00000457     0 FUNC    LOCAL  DEFAULT   11 __i686.get_pc_thunk.bx
    42: 00001f28     0 OBJECT  LOCAL  DEFAULT  ABS _DYNAMIC
    43: 00000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__
    44: 00000000     0 NOTYPE  WEAK   DEFAULT  UND _Jv_RegisterClasses
    45: 000004c8     0 FUNC    GLOBAL DEFAULT   12 _fini
    46: 0000045c    38 FUNC    GLOBAL DEFAULT   11 Java_org_xkit_demo_jni_He
    47: 00002010     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
    48: 00002018     0 NOTYPE  GLOBAL DEFAULT  ABS _end
    49: 00000000     0 FUNC    GLOBAL DEFAULT  UND puts@@GLIBC_2.0
    50: 00002010     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
    51: 00000000     0 FUNC    WEAK   DEFAULT  UND __cxa_finalize@@GLIBC_2.1
    52: 00000324     0 FUNC    GLOBAL DEFAULT    9 _init



About

[JNI]Java JNI Demo on Linux Platform

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C 57.4%
  • Makefile 26.6%
  • C++ 7.6%
  • HTML 4.3%
  • CMake 2.2%
  • Java 1.6%
  • Other 0.3%