Linux under Java JNI invoke C language Dynamic link library (very use!!) Very classic!! )

Source: Internet
Author: User

Comes from:http://watershitter.iteye.com/blog/477615


1 the declaration of the C language function in Java is public native static void greeting (); It's like an interface declaration, but there's native!.

2 Compile Javac Hellonative.java, then use Javah

Javah hellonative automatically generates a header file for C HelloNative.h

3 The first sentence of the generated header file is #include <jni.h>
But GCC inside the default environment can not know what jni.h is, jni.h in the JDK $java_home/include or $java_home/include/linux below, can go in to check out ~

4 The next step is to write the implementation of the C language according to the method declared in HelloNative.h, note that the automatically generated function name is very long, and the opening Java is uppercase, the case is very fatal, (Finally, my program in the dynamic library has been loaded in the case of the error: java.lang.UnsatisfiedLinkError:HelloNative.greeting () V, is because of the C language function name wrong case, strange.) Compile without error ... )

<<< Comments: This author is a little 2, just copy it right >>>
5 Linux compiler build dynamic library, pay attention to different environments

Gcc-fpic-i jdk/include-i jdk/include/linux-shared-o libhellonative.so hellonative.c

<<< my compile command at the time was:

Gcc-fpic-shared-d_reentrant-i $JAVA _home/include-i $JAVA _home/include/linux hello.c-o libtesthello.so

I think the author's order is problematic unless the author really has the JDK folder and is really in his parents ' record ... My premise is also configured with the Java environment, but this is not nonsense mody. >>>

Here, I make the mistake:

A, do not understand-I jdk-i is include, display the address of the library of the specified library, the JDK behind it will be replaced with your design address,

b, anxious to go to the Internet search problem, unnoticed in Linux, the name of the dynamic link library must be lib****.so, must start with Lib.

<<< I've been doing this for a long time, and I just have a problem here. Thank you very much, author >>>


6 After the libHelloNative.h is generated, a test class, such as Hellonativetest, is then written,
Of course to call System.loadlibrary ("hellonative"), note that this time do not lib, also do not. so!;
Call execution hellonative.greeting (); This time the mistake came again:
Java.lang.UnsatisfiedLinkError:no hellonative in Java.library.path. This error is very classic, because: Java can not find the library path ~:
Obviously: libhellonative.so is placed on the current path ".", but Linux does not know what to do at the current path. Linux is "stupid" very "complex" ~
A. Under Linux Java.library.path and environment jdk/bin that path is not the same thing, there is another default variable Ld_library_path to save his information. and windows, first Java will find the current directory, second, it will go to the address of the environment variable to find.
B. Because Linux has a special path, workaround 1-Can call Sysout (System.getproperty ("Java.library.path")); And then copy the libxxxx.so to the catalogue.
2 Setting environment variables Export ld_library_path=.: $LD _library_path, but where to set it. /etc/profile? OR/ROOT/.BASHRC Don't know ... Forgot the Linux loading sequence ~
3 You can specify the location of the library at one time of execution:
Java-djava.library.path=. Hellonativetest

Summary error:

1. Do not know GCC compile time specified library

2. Do not understand the characteristics of Java.libray.path, especially under Linux

3.c Language implementation function spelling error

4. The elimination of the problem is not rational, systematic, the emergence of irritability, resulting in low efficiency. Always understand that the machine is just doing what you specify, and that you have made a mistake.
Appendix: GCC parameter Explanation (reprint):
The main thing is an option for the GCC command line:
-shared This option specifies that a dynamic connection library is generated (which allows the connector to generate an export symbol table of type T, and sometimes a weakly connected W-type export symbol) without which the external program cannot connect. Equivalent to an executable file

-fpic: For code that is compiled as location independent, the compiled code is location-dependent without this option, so dynamic loading is a way to copy the code to meet the needs of different processes, but not the purpose of real code segment sharing.

-L.: Indicates the library to connect to in the current directory

-ltest: When the compiler looks for a dynamic connection library, there is an implied naming rule, that is, precede the given name with Lib, followed by. So to determine the name of the library

Ld_library_path: This environment variable indicates that the dynamic connector can load the path of the dynamic library.

Of course, if you have root permissions, you can modify the/etc/ld.so.conf file, and then call/sbin/ldconfig to achieve the same purpose, but if you do not have root permissions, then only the output Ld_library_path method.


4. Note

When you call a dynamic library, there are a few problems that are often encountered, sometimes, it is clear that the library's header file directory through the "-I" included, the library file is guided by the "-L" parameter, and specified the "-L" library name, but through the LDD command to see if you can not find the link you specify the so file , all you have to do is modify the Ld_library_path or/etc/ld.so.conf file to specify the directory of the Dynamic library. This is usually done to solve the problem that the library cannot be linked.


<<< Although I used JNI (though it seems to be a year ago), it still took me two hours to write a HelloWorld. But I have always told others that my foundation is very strong, very solid. It's embarrassing. >>>

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.