Eclipse calls JNI

Source: Internet
Author: User

Http://blog.chinaunix.net/uid-27003388-id-3235189.html

1. Create a Java project (jni_test) in eclipse;

Create a package in SRC, named Jni;

Then create a Java class (Helloworld.java) with the native declaration method in the JNI, with the following file details:

Package JNI;

public class HelloWorld {

Static {

System. loadLibrary ("HelloWorld");

}

public native void disphelloworld ();

/**

* @param args

*/

public static void main (string[] args) {

// TODO auto-generated method stub

//system.getproperty ("Java.library.path");

new HelloWorld (). Disphelloworld ();

}

}

The Eclipse Engineering Tree chart looks like this:

2. Generating JNI header files

On the command line, CD to the SRC directory of the Java Project (jni_test), for example:

CD E:\WORKSPACE\JNI_TEST\SRC

Then execute the JAVAH-JNI Jni.helloworld (package does not miss out)

The resulting jni_helloworld.h file is as follows

/* Don't EDIT this file-it are machine generated */

#include <jni.h>

/* Header for class Jni_helloworld */

#ifndef _included_jni_helloworld

#define _included_jni_helloworld

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class:jni_helloworld

* Method:disphelloworld

* Signature: () V

*/

jniexport void Jnicall Java_jni_helloworld_disphelloworld

(JNIENV *, jobject);

#ifdef __cplusplus

}

#endif

#endif

The Eclipse Engineering Tree chart is updated as follows:

3. Create jni_helloworldimp.cpp in the SRC directory of the Java Project (jni_test), implement functions in jni_helloworld.h, note the function name, the function name in CPP is consistent with the function name of h file.

Jni_helloworldimp.cpp content is as follows:

#include <jni.h>

#include "Jni_helloworld.h"

#include <stdio.h>

jniexport void Jnicall Java_jni_helloworld_disphelloworld

(jnienv *env, Jobject obj)

{

printf ("Hello world!");

Return

}

The Eclipse Engineering Tree chart is updated as follows:

4. In the Java Engineering (jni_test) SRC directory compilation jni_helloworldimp.cpp produce the DLL,

Under the console >cl-ld Jni_helloworldimp.cpp-fehelloworld.dll,
In the parameters of the above command,-LD represents the name that produces the DLL after Dll,-fe, and the specific parameters can be viewed with the cl-help command.
If compiled successfully, it will produce Helloworld.dll, Helloworld.lib, Helloworld.exp,

And a jni_helloworldimp.obj of four files.

The Eclipse Engineering Tree chart is updated as follows:

The following issues may be encountered in this process:

<1>. CL is not an internal command, the workaround:

Add the Vc-bin path to the system environment variable path,

C:\Program Files\Microsoft Visual Studio\vc98\bin

C:\Program Files\Microsoft Visual Studio\common\msdev98\bin

<2>. Fatal error C1083:cannot open include file: ' jni.h ': No such file or directory

Workaround:

Copy%java_home%\include\jni.h

%java_home%\include\win32\jawt_md.h

%java_home%\include\win32\jni_md.h

To the Vc-include directory (C:\Program Files\Microsoft Visual Studio\vc98\include),

Note: Java_home refers to your JDK directory.

Add the Vc-include directory to the System environment variable, adding an include variable,

The variable value is: C:\Program Files\Microsoft Visual Studio\vc98\include

<3>. Link:fatal Error Lnk1104:cannot Open file "LIBCMT.lib"

Workaround:

Add the Vc-lib directory to the System environment variable, adding a Lib variable,

The variable value is: C:\Program Files\Microsoft Visual Studio\vc98\lib

5. Set the Lib path of the Jni_test project, that is, add the Helloworld.dll path to the project's Lib path;

The specific methods are:

Right-click the project name à select attribute properties à select "Java Build Path" in the list on the left and select "+" on the Right tab by selecting "Source" à click the "+" number in front of the project name, choose "Native Library Location "," Edit "to select the path. (Of course, if you copy the DLL to workspace, you can also use a relative path.) You can also right-click "src" to set its properties within the native library item. )

6. Running Jni_test project, run as Java application,console output Hello world!

Eclipse calls JNI

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.