Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Basic Java JNI     - Ubuntu 14.04 Enable root and disable the guest (Linux)

- Linux and hardware (Linux)

- How to set the default Fedora from the command line (Linux)

- To assign multiple IP addresses NIC on the CentOS 7 (Linux)

- How to create a cloud encrypted file system in Linux systems (Linux)

- TCP network communication Java Programming (Programming)

- Video editing captions under Linux (Linux)

- Kubernetes cluster deployment (Server)

- To compiler and install MariaDB-10.0.20 under CentOS 6.6 (Database)

- Hive start being given: Found class jline.Terminal, but interface was expected (Database)

- Three strategies to teach you to release the device memory (Linux)

- Ubuntu uses under KVM + Qemu virtual machine build (Linux)

- Experts teach you safety precautions Linux Sniffer (Linux)

- How to add any of a Linux distribution beautiful fonts (Linux)

- Nginx version of helloworld (Server)

- GDB remote connections RX Probe online debug program (Programming)

- LAMP and LNMP automated installation scripts (Server)

- GROUP BY extension (Database)

- SSH without password (Linux)

- Install Unity 8 preview version of the desktop in Ubuntu (Linux)

 
         
  Basic Java JNI
     
  Add Date : 2018-11-21      
         
         
         
  JNI is Java with C, C ++, Objective-C, Objective-C ++ and other statically compiled language and assembly language interface to interact. Although for now, Java provides a lot of running high performance run-time library, but in many respects, especially in the field of high performance computing, Java provides an efficient library is not a lot, so we can through the JNI interface we use static Java application to load and assembler language after the call is connected to a dynamic library.

First, Java provides for different operating system platforms to adapt to their environment and run-time depending on the compiler provides JNI header files. JNI header files generally consists of two components: jni_md.h provides a platform independent header files; jni.h provides an interface declaration jni needed and various types of definitions. The two header files can be found in the JDK include. When we create a JNI dynamic library project should be an output target of the project as a dynamic link library (for the next .dll, Unix-like systems .so, OS X for the next blast shake dead under .dylib). We can these two header files into the project when you create a project.

Then we can look at the following code:

Then we can look at the following code:

#include

#include "jni.h"

JNIEXPORT jint JNICALL Java_MyJNI_myTest (JNIEnv * env, jobject obj, jintArray dstArray, jintArray srcArray)

{

    jboolean isCopy = 0;

    jint * csrcArray = (* (* env) -> GetIntArrayElements) (env, srcArray, & isCopy);

    jsize dstSize = (* (* env) -> GetArrayLength) (env, dstArray);

    jsize srcSize = (* (* env) -> GetArrayLength) (env, srcArray);

    jsize length = dstSize> = srcSize dstSize:? srcSize;

    printf ( "The length is:% u \ n", length);

    printf ( "Is copy available% d \ n?", isCopy);

    printf ( "The sum of source array is:% d", csrcArray [0] + csrcArray [1]);

    jint dstBuffer [32];

    for (jsize i = 0; i
        dstBuffer [i] = csrcArray [i] + i + 100;

   

    (* (* Env) -> SetIntArrayRegion) (env, dstArray, 0, length, dstBuffer);

    return 100;

}

The code above provides an export Java method, Java_ behind the name indicates the class name of a Java application side, after the class name _ the name is the method name with the class (which may be a member of the method may be a class method) . A standard JNI method should provide two parameters, one is JNIEnv *, and the other is jobject. env provides a handle to the Java Runtime Environment, the latter calling various methods require Java Runtime pass this parameter. jobject is a pointer to the object, the equivalent Java application side of this. So if this JNI methods you define in the Java application side is a class method, this parameter thus ignored.

The above method is the function of each element of the second array value plus 100 plus its corresponding index after assignment to the first object of the array elements. Here is assumed the number of elements of two arrays of up to 32. Finally, a return value of type int 100.

We can then look at the corresponding end of the Java code:

class MyJNI {

    native static int myTest (int [] dstArray, int [] srcArray);

}

public class Test {

    static {

        System.loadLibrary ( "MyJNI");

    }


    / **

    * @param Args

    * /

    public static void main (String [] args) {

        // TODO Auto-generated method stub

        int [] dstArray = new int [2];

        int [] srcArray = {100, 600};

       

        System.out.println ( "The value is:" + MyJNI.myTest (dstArray, srcArray));

        System.out.println ( "The sum is:" + (dstArray [0] + dstArray [1]));

    }

}

Here call System.loadLibrary method to load the dynamic link library.

When using Linux environment must be noted that the path must be set before running the LD_LIBRARY_PATH environment variable to specify where the dynamic libraries, such as:

LD_LIBRARY_PATH = '/ usr / home / java_test'

export LD_LIBRARY_PATH

If you use the Eclipse development environment, we can set this environment variable in the Run Configuration of the current project and in the Debug Configuration. Convenience, we can use the button to the right of the variable when setting the path, select project_loc. The built-in variable specifies the absolute path of the current system of the project. Then the dynamic library into the current project's root directory can be.

Finally, we give a more complete example. In this case, we join together for assembly language code is compiled and linked in the JNI, made .so file. Here we should note that, due to the assembler does not support -fPIC options, so you want to compile and link files together, then you can not take -fPIC compiler options, otherwise the connection will fail.

We look at the Java code:

package test;

public class Main {

    static {
        System.loadLibrary (( "ctest"));
    }
    
    native static int myJNITest ();
    
    public static void main (String [] args) {
        // TODO Auto-generated method stub
        System.out.println ( "The answer is:" + myJNITest ());
    }

}

Here to join a package - test, therefore, for the native JNI function myJNITest, its prefix will need to add the package name, becomes -

Java_test_Main_myJNITest.

Here we see JNI section. Here we can then create a JNI works to make them easier to compile shell. First created in this project in a C source file, the file name can be easily named, .c suffix can be, here with a.c:

#include

extern int __attribute __ ((fastcall)) asmTest (void);

JNIEXPORT jint JNICALL Java_test_Main_myJNITest (JNIEnv * env, jobject obj)
{
    return 100 + asmTest ();
}

Here asmTest function is defined in an assembly file, you will be seen below. As used herein, fastcall calling convention, so parameters and return values can be in the register, so you can easily compile function to get the argument. Of course, in 64-bit applications do not need to fastcall the environment. Because the 64-bit environment, x86-64 follow the System-V function calling convention specific reference --http:? //www.cocoachina.com/bbs/read.php Tid-66986.html

Let's look at an assembly source file (asmtest.s):

.text
.align 2

.globl asmTest

asmTest:

    mov $ 15,% eax
    ret

After completion of the C source code and assembly code, we will write a simple shell file them separately compiled, then linked into a shared library files so dynamic.

gcc -Wall -c -I / home / zenny_chen / MyPrograms / eclipse / jdk / include -I / home / zenny_chen / MyPrograms / eclipse / jdk / include / linux a.c
gcc -Wall -c asmtest.s
gcc -shared -z noexecstack -o libctest.so a.o asmtest.o

Since jni.h under jdk / include, but jni_md.h is under jdk / include / linux (other operating system is the name of other operating systems), and therefore, there should two header files contain paths are added. In addition, the final -z noexecstack to add, because Java stack will be checked, if there is no connection option, Java when calling this function will report warning (more annoying whistling ~). Finally, connect the generated library name is libctest.so, the file name must match the name of the library in Java loadLibrary.
     
         
         
         
  More:      
 
- How to extend / remove swap partitions (Linux)
- Getting the Linux shell flow control statements (Programming)
- Database start listening TNS-12537, TNS-12560 error (Database)
- Use PuTTY to access the virtual machine Linux under Windows xp (Programming)
- Compile and install the latest version of Redis Stable (Database)
- Swift used in the application to add a local push at the specified time (Programming)
- 11.2.04 Oracle RAC directory crfclust.bdb file is too large, Bug 20186278 (Database)
- CentOS directory structure Explanation (Linux)
- Ubuntu 14.10 splash screen brightness settings (Linux)
- Partition contrast manifestations under Windows and Linux (Linux)
- Python in yield (Programming)
- C ++ based foundation: the difference between C and C ++ (Programming)
- ORA-12154 TNS could not resolve the specified identifier (Database)
- To compile and install MySQL 5.7.7 RC under CentOS 7.1 (Database)
- Nginx version information hidden or modified (Server)
- Binary tree and some basic operations with binary list (Programming)
- Android source code compiled fatal error solutions (Programming)
- How to Install Apache on Linux servers (Server)
- Analytical Ceph: handle network layer (Server)
- Ubuntu 13.04 configure MyEclipse 10.7 Environment (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.