JNI Invocation Example

Source: Internet
Author: User

JNI allows programmers to write local methods to handle situations when an application cannot be written entirely in the Java programming language, such as when a standard Java class library does not support platform-specific features or libraries. It is also used to modify an existing application in an application written in another programming language to access Java applications. Many of the standard library classes rely on JNI developers and user-provided features, such as file I/O and sound capabilities. Includes performance and platform-sensitive implementations in a standard library that allow all applications to access this feature in a secure and platform-agnostic manner.

The JNI framework lets native methods use Java objects in the same way that Java code uses them. Local methods can create objects, and then examine and use these objects to perform its tasks. Local methods can also examine and use objects created by Java application code.

JNI is sometimes called "Escape pod" for Java developers because it makes it impossible for Java applications to provide added functionality to Java-standard APIs. It can be used to interface with code written in other languages, such as. It can also be used for real-time computing or manipulation, like solving complex mathematical equations, because the problem of native code some courses may run faster than Java Virtual machine code


ExamplesHelloWorld

make.sh

#!/bin/sh # Linux 2.6.32-358.el6.x86_64 # gcc 4.4.7 # openjdk 1.7.0  ld_library_path=$LD _library_path:. Javac Helloworld.javajavah HELLOWORLDGCC- Shared-fpic Libhelloworld.c-o Libhelloworld.sojava HelloWorld

BUILD.bat

:: Microsoft visual Studio, Visual C + + compilerSET VC="C:\Program Files (x86) \microsoft Visual Studio 11.0\VC":: Microsoft Windows SDK for Windows 7 and. NET Framework 4SET MSDK="C:\Program Files (x86) \microsoft Sdks\windows\v7.1a":: Java 1.7.0 UpdateSET Java_home="C:\Program Files (x86) \java\jdk1.7.0_21":: Add compiler Tools folder to the PATH variable. Don't run this too many times or the PATH would exceed the maximum limit.Pager %vc%\vcvarsall.batjavac Helloworld.javajavah HelloWorld:: On Windows, the JNI library should not has a "lib" prefix%vc%\bin\cl/i%java_home%\include /i%java_home%\include\win32 /i%vc%\include /i%vc%\lib /i%msdk%\libLibhelloworld.c/fehelloworld.dll /LDJava HelloWorld

Helloworld.java

class HelloWorld {privatenativevoidprint();  Public Static void Main (String[]args) {newHelloWorld(). Print (); }static{System. loadLibrary ("HelloWorld"); }}

HelloWorld.h

/* Don't EDIT this file-it are machine generated * /#include <jni.h>/ * Header for class HelloWorld * /#ifndef _included_helloworld#define _included_helloworld#ifdef __cplusplusextern "C" {#endif/** Class:helloworld* Method:print* Signature: () V */Jniexport void Jnicall Java_helloworld_print  (jnienv *, Jobject);#ifdef __cplusplus}#endif#endif

Libhelloworld.c

#include <stdio.h> #include "HelloWorld.h" Jniexport void Jnicall Java_helloworld_print (jnienv*env,jobjectobj) {     printf("Hello world! \ n " );     return ; }

Invocation:

chmod +x make.sh./make.sh

Reference URL:

Https://en.wikipedia.org/wiki/Java_Native_Interface

Finally, add the changes to the Mak file


#!/bin/sh

# Linux 2.6.32-358.el6.x86_64
# gcc 4.4.7
# OPENJDK 1.7.0

Export ld_library_path= $LD _library_path:.
Javac Helloworld.java
Javah HelloWorld
Gcc-shared-fpic libhelloworld.c-i./-i/usr/java/jdk1.7.0_67/include/-i/usr/java/jdk1.7.0_67/include/linux-o LibHe Lloworld.so
Java HelloWorld
(Note: Here,-i is the search path for the import header file, which is mainly for searching the jni.h in TestJNI.h.) Different system paths may be different, can be checked by locate jni.h)




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JNI Invocation Example

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.