JAVA JNI compiles the so library under Liunx or cannot find the so library problem to resolve no XXX on Java.library.path__java

Source: Internet
Author: User

The amount of the Belch does not start with a BB. Time is pressing.
These two days have been working on the JNI thing, under Windows Normal, but under the Liunx always reported an error, no XXX on Java.library.path, in the Java.library.path can not find the specified so library.

Feel very strange, so use
System.out.println (System.getproperty ("Java.library.path"))
Prints out the path of the Java.library.path. means my path is at sixes and sevens.
Step by step debugging, to the ClassLoader class, found that the file will be confused to find a library of Lib, just started to think that the path did not match well, and later found that the system called a Method: Java.lang.System.mapLibraryName () Culprit. This method will be spliced "Lib" in front of our given name to see its example:

Example the
following Example shows the usage of Java.lang.System.mapLibraryName () method.

Package com.tutorialspoint;

Import java.lang.*;

public class Systemdemo {public

   static void Main (string[] args) {

      //prints the name of the operating System
      System.out.println (System.getproperty ("Os.name"));

      /* Maps a library name into a platform-specific string representing
         a native library */
      string str = System.maplib Raryname ("Os.name");   
      System.out.println (str);
   } 
Let-us compile and run the above program, this'll produce the following result−

Linux
libos.name.so

Our loading method is System.loadlibrary ("Aes128converter");
After the end of the program really to find the name into the Java.library.path under the "libaes128converter.so"

The solution is:
When we generate so file in front with a "Lib", so that the program in the runtime to find the so file, in addition, Liunx under the Java Lookup so library path is: ${your JAVA_HOME}/JRE/LIB/AMD64

You need to get two files before you can generate the so library: jni.h, Jni_md.h
They are in the ${your java_home}/include and ${under your java_home}/include/linux, copy them two and need to compile the file in the same folder,
Compile the so file with GCC under Liunx:
Gcc-fpic-shared-o libhelloworld.so HELLOWORLD.C
Specific meaning of each option interested students can go to their own Baidu, here to say, remember in your. c file to refer to your system library or local library, or you will report a compilation error.

More rough, and so have time to tidy up, I hope this article can help friends to step on a little inexplicable pit. Good bye

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.