Programming related to Linux involving java

Source: Internet
Author: User
For details about Linux programming involving java and other programming-Linux general technology-Linux programming and kernel, refer to the following. On the Linux platform, how does one implement Local Shared libraries and Java collaboration. The Hello World Program is the first step to get started with the standard, so I also use the most similar applications.

The first step is to define a Java class -- Hello. It provides the SayHello method:

Pay attention to the following two points:

1. Compile a local method declaration for each local method to be used. The declaration method is no different from that of a common Java method interface, but the native keyword must be specified, as shown below:

Public native void SayHello (String strName );

In this function, we will say hello to someone based on the name of the person passed in.

2. The local code library must be explicitly loaded. We need to load this library in a static block of the class:

Static
{
System. loadLibrary ("hello ");
}

Add the necessary exception handling to generate the following source file Hello. java:

Public class Hello
{
Static
{
Try
{
// The Name Of The Linked database where the local method is located
System. loadLibrary ("hello ");
}
Catch (UnsatisfiedLinkError e)
{
System. err. println
("Cannot load hello library: \ n" + e. toString ());
}
}
Public Hello ()
{
}
// Declared local method
Public native void SayHello (String strName );
}

Generate the Hello. class file after compilation.

Step 2: generate a Local Link Library. The specific process is as follows:

1. to generate the Java Local interface header file for the class defined above, you must use javah. The javah function of the Java compiler will generate the necessary declaration based on the Hello class. This command will generate Hello. h file. We need to include it in the code of the shared library. javah does not use the default internal command and must specify the path. It is in the bin directory of JDK, in my Linux environment, the command is as follows:

/Home/jbuilder/jdk1.3.1/bin/javah Hello

The generated Hello. h file is as follows:

/* Do not edit this file-
It is machine generated */
# Include
/* Header for class Hello */
# Ifndef _ Included_Hello
# Define _ Included_Hello
# Ifdef _ cplusplus
Extern "C "{
# Endif
/*
* Class: Hello
* Method: SayHello
* Signature: (Ljava/lang/String;) V
*/
JNIEXPORT void JNICALL Java_Hello_SayHello
(JNIEnv *, jobject, jstring );
# Ifdef _ cplusplus
}
# Endif
# Endif

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.