A simple JNI program implemented under Eclipse returns a double type

Source: Internet
Author: User

When generated under Eclipse, it is important to note that, since the class is usually added in the package, rather than being added separately as a separate project, it is necessary to go into the folder where the packages are located, javac the package name , and compile it in the same way. The Javah command is the same.

First create two Java files:

Getpara.java:

1  Package Com.niiam; 2 3  Public class Getpara {4      Public native Double getparameter (); 5 }

This establishes a class in which a locally implemented method, GetParameter (), is defined in the class, which does not pass in parameters, outgoing a fixed double type data, and is later implemented by the C + + language

Set up a Test.java to test:

1  PackageCom.niiam;2 3  Public classTest {4  Public Static voidMain (string[] args) {5System.loadlibrary ("Getpara");6Getpara para=NewGetpara ();7System.out.println ("The height is:" +Para.getparameter ());8     }9}

Attention!!!

Here is the Getpara.dll link library that we are going to generate in the main method.

The statement can also be placed inside the Getpara class and decorated with static{}, so that each time the class is instantiated, it is equivalent to implementing the introduction of library operations.

In conjunction with the Springboot project to use the second way, remember!

Enter the folder where the Com.niiam package is located

Javac Com.niiam.GetPara.java

Javac Com.niiam.Test.java to compile

Javah Com.niiam.GetPara.java generating. h files Com_niiam_getpara.h

Note that the. h file here is a function declaration that contains the name of the package, within it:

Jniexport jdouble Jnicall Java_com_niiam_getpara_getparameter
(JNIENV *, jobject);

Also has the package name exists, differs from the manual establishment project situation, the generated DLL file is also different, cannot equal

Setting up the console project in VS, output set to DLL

Introduce a header file that introduces the directory where the jni.h jni_md.h is in the included directory

Create a Getpara.cpp file

1 " com_niiam_getpara.h " 2 3 4 5 jniexport jdouble jnicall java_com_niiam_getpara_getparameter 6 (jnienv *, Jobject)  7{  8     double104.06 ; 9     return A; Ten }

Compiling build DLL files

Note that because Java platform is a x64 platform, the DLL files generated here must also be x64

Where to put the generated DLL files, here are a few options:

1, put in the disk development directory, the directory is added to the environment variable path, so that the runtime can be introduced. This approach is also appropriate for running in Eclipse and running Java Com.niiam.GetPara in the console

2, put in the project directory, and add the project directory into the Java build path,

Or is:

Tested, put them in these two places.

This will directly work directly in Eclipse.

A simple JNI program implemented under Eclipse returns a double type

Related Article

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.