darkstarslyp / JavaJniDemo

The sample for java jni develop

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JavaJniDemo

The sample for java jni develop

How to load library ?

System.load();

You must specify the absolute path of shared library.

System.load("/Users/demon.li/JavaJniDemo/jni/build/libHelloJNI.so");

Systeme.loadLibrary();

You only specify the shared library simple name . If your shared library file name is libHelloJni.so,you can call Systeme.loadLibrary("HelloJNI");,but you must add the path to java.library.path where the shared library is.

      System.setProperty("java.library.path", TestJni.class.getResource("/").getPath()+":"+System.getProperty("java.library.path")+":"+
                    ":"+System.getenv("LD_LIBRARY_PATH")
            );

            try {

                Field fieldSysPath = null;
                fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths");
                fieldSysPath.setAccessible(true);
                fieldSysPath.set(null, null);

            } catch (Exception e) {
                e.printStackTrace();
            }
      System.loadLibrary("HelloJNI");

If TestJni.java locate at /Users/demon.li/JavaJniDemo/java/com/demon/jni/TestJni.java The shared library must locate at /Users/demon.li/JavaJniDemo/java/libHelloJNI.so.

About

The sample for java jni develop


Languages

Language:CMake 42.0%Language:Java 35.0%Language:Shell 14.4%Language:C++ 8.5%