JNI programming example of LinuxonPOWER

Source: Internet
Author: User
The JNI programming instance of LinuxonPOWER-Linux general technology-Linux programming and kernel information. The following is a detailed description. This article describes the important Java Native Interface (JNI) programming concepts through some simplified examples, and points out that it is specific to POWER™On Linux™And common programming risks.

Introduction

Programming those new to the Java Local interface and wanting to release the Linux version for POWER (such as Red Hat Enterprise Linux 3 and suse linux Enterprise Linux V9) for developers who have tested their JNI programming capabilities, this article will be helpful.

Compiler

In this article, the simplified source code is compiled on SLES 9 and rhel as 3 Update 3, using the latest Service Update (Service Refresh) of the 64-bit IBM Java SDK 1.4.2) and ibm xl c/C ++ Advanced Edition V7.0 for Linux. Note that it is not necessary to force the use of the IBM Java SDK and the ibm xl c/c ++ compiler, but the results will be very good. Use other compilers to compile Java and jni c/C ++ code on the Linux release for POWER.

The ibm xl c/C ++ compiler can be™The processor generates extremely optimized binary code, which can significantly improve the performance of all C/C ++ code in most cases. The ibm xl c/C ++ compiler for Linux is well compatible with gnu c/C ++ and contains gxl * compiler variables, you can mark the gnu c/C ++ compiler as a corresponding command for the ibm xl c/C ++ compiler and then call it. When the ibm xl c/C ++ compiler is used, this significantly simplifies the porting of Makefile files (compiled by GNU.

Example

The examples in this section are based on the common and publicly available sample JNI code and show you how to compile these examples in Linux on POWER. For more information about JNI Programming, see the following developerWorks articles: Java environments for Linux on POWER architecture (October 2004) and JNI Programming on AIX (March 2004 ).

Call Local functions using Java code examples



Listing 1. HelloWorld. java


Class HelloWorld {
/* Native method declaration */
Public native void displayHelloWorld ();
/* Use static intializer */
Static {
System. loadLibrary ("hello ");
}
/* Main function callnative method */
Public static void main (String [] args ){
New HelloWorld (). displayHelloWorld ();
}
}



Listing 2. HelloWorld. c


# Include
# Include "HelloWorld. h"
# Include

JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld (JNIEnv * env, jobject obj)
{
Printf ("Hello world! \ N ");
Return;
}




Compile

Compile the Java code to generate the HelloWorld. class file:


Javac HelloWorld. java



Use javah to generate the HelloWorld. h file:


Javah? Jni HelloWorld




Listing 3. HelloWorld. h


/* Do not edit this file-it is machine generated */
# Include
/* Header for class HelloWorld */

# Ifndef _ Included_HelloWorld
# Define _ Included_HelloWorld
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: () V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject );

# Ifdef _ cplusplus
}
# Endif
# Endif





Compile local C code:


Xlc_r? Q64? Qarch = auto? Qmkshrobj HelloWorld. c? O libhello. so





Run


Java? Cp. HelloWorld



Output


HelloWorld!



Note:
QUOTE:

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.