In Linux, Java JNI calls the C language dynamic link library (very useful !! Very classic !!)

Source: Internet
Author: User

From: http://watershitter.iteye.com/blog/477615


1. JavaC LanguageFunction Declaration

Public native static void greeting (); // It is like an interface declaration, but there is native!

2. Compile javac hellonative. Java and then use javah

Javah hellonative automatically generates the header file hellonative. h of C.

3. The first sentence of the generated header file is

# Include <JNI. h>
However, the default environment in GCC does not know what JNI. H is,JNI. H is under $ java_home/include or $ java_home/include/Linux of JDK., You can check it out ~

4. The next step is according to hellonative. the method declared in H is written into the implementation of C language. Note that the automatically generated function has a long name, And the Java at the beginning is in upper case, which is fatal (myProgramWhen the dynamic library has been loaded, the following error occurs: Java. Lang. unsatisfiedlinkerror: hellonative. Greeting () v. This is because the function name in C language is incorrect in case. It is strange! Compilation error .....)

<Comment: this author is a bit 2. Just copy it directly>


5. compile and generate dynamic libraries in Linux. Note that different environments are different ~

Gcc-FPIC-I JDK/include/Linux-shared-O libhellonative. So hellonative. c 

<My current compilation command was:

Gcc-FPIC-shared-d_reentrant-I $ java_home/include/Linux hello. C-o libtesthello. So

I think the author's command is wrong, unless the author really has the JDK folder and is recorded by his parents... I configured the Java environment on this premise, but it is not nonsense. >>>

Here, my mistakes are as follows:

A. I don't understand.-I jdk-I is an Include. The address of the specified library is displayed. Naturally, the JDK is replaced with your design address,

B. search online in a hurry. in Linux, the name of the dynamic link library must be lib ***. So. It must start with Lib! 

<I have been engaged by B for a long time, and I am just having a problem here .. Thank you for choosing >>>


6 compile and generate libhellonative. h, and then write a test class, such as hellonativetest,
Of course, you must call system. loadlibrary ("hellonative "); Note that do not use lib or. So !;
Call and execute hellonative. Greeting (); at this time, the error is returned:
Java. Lang. unsatisfiedlinkerror: No hellonative in Java. Library. Path. This error is classic because the library path cannot be found in Java ~ :
Obviously: Put libhellonative. So in the current path ".", However, during Linux execution, you do not know how to find it in the current path. Linux is silly and complicated "~
A. in Linux, the path of Java. Library. path is not the same as that of JDK/bin. Another default variable LD_LIBRARY_PATH is used to save the information. In Windows, Java will first find the current directory, and secondly, it will find the address of the Environment Variable!
B. Because the Linux path is special, solution 1-you can call sysout ( System. getproperty ("Java. Library. Path"); to view! Copy libxxxx. So to the directory.
2. Set the environment variable export LD_LIBRARY_PATH =.: $ LD_LIBRARY_PATH, but where is the setting? /Etc/profile? Or/root/. bashrc don't know... I forgot the Linux loading sequence ~
3. You can specify the library location during a single execution:
Java-djava. Library. Path =. hellonativetest

Summary mistakes:

1. Do not know the library specified during GCC Compilation

2. Do not understand the features of Java. libray. path, especially in Linux

3. Spelling errors when implementing functions in C Language

4. The problem is not rational and systematic, resulting in irritability, resulting in low efficiency. Always understand that the machine is just doing what you specify, and you always make mistakes yourself ~~~


Appendix: GCC parameter explanation (reproduced ):
The most important option is the GCC command line:
-Shared this option specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, and sometimes generate a weak connection W-type export symbol). External programs cannot connect without this sign. Equivalent to an executable file

-FPIC: indicates that the compilation is in a separate position.CodeWithout this option, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, rather than truly sharing code segments.

-L.: indicates that the database to be connected is in the current directory.

-Ltest: an implicit naming rule is used by the compiler to search for a dynamically connected database. That is, add lib before the given name and. So to determine the library name.

LD_LIBRARY_PATH: The environment variable indicates the path where the dynamic connector can load the dynamic library.

If you have the root permission, you can modify/etc/lD. so. CONF file, and then call/sbin/ldconfig to achieve the same purpose. However, if you do not have the root permission, you can only use the LD_LIBRARY_PATH output method.


4. Note

When calling a dynamic library, there are several problems that may occur frequently. Sometimes, the directory where the library header files are already included through "-I, the file where the library is located is guided by the "-l" parameter and the database name of "-l" is specified. However, when you run the LDD command to view the file, you cannot find the so file with the specified link, in this case, you need to modify LD_LIBRARY_PATH or/etc/lD. so. CONF file to specify the directory of the dynamic library. This usually solves the problem that the database cannot be linked.


<Although I used JNI (but it seems like a year ago), I spent two hours writing a helloworld !! However, I had a poor foundation, but I had always told others that my foundation was very powerful and very solid .. Truly self-satisfied >>>

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.