Use cl commands in jni to generate DLL files

Source: Internet
Author: User

Problem description:

When calling DLL using JNI, you must first generate the DLL file

Solution:

(1.exe now use the cl.exe program of vs2008to generate the DLL file

(1.1)cl.exe environment setup

Note:

The location of the cl.exe program is in the VC \ bin directory of the VS installation directory.

When you directly run the preceding program, the program automatically exits. The correct method is to add Include, path, and lib paths to the system environment variables.

Note:

The preceding configurations are required for the cl.exe program in vs2008to execute the cl command in DOS, including LIB, Path, and INCLUDE.

Corresponding to the VS2008 lib, bin, and include directories respectively

Note:

After the preceding configuration, you can run the cl command normally.

(1.2) generate the DLL file

1)

Note:

When using the cl command to generate the DLL file, we mainly call the service for JNI. Here we take the header file com_jni_HelloWorld.h generated by Javah as an example.

The cpp file containing the header file is as follows:

Note:

Because the cl command needs to contain the jni. h header file, we copy the jni. h and jni_md.h files in jdk to the VC2008 \ VC \ include directory.

Note:

Copy jni. h and jni_md.h to the include directory of VC.

2) execute commands in DOS

The following error occurs:

Fatal error LNK1104: Unable to open the file "kernel32.lib

Solution:

Note:

Copy the Kernel32.Lib file in C: \ Program Files \ Microsoft SDKs \ Windows \ v6.0A \ Lib

Under the lib directory of the VS2008 installation directory

3) generate the dll file

Note:

Under the bin \ directory of the project, run the command cl-I % java_home % \ include \ win32-LD com_jni_HelloWorld.cpp-Fehello. dll.

Dll files can be generated.

Note:

The command for generating DLL files is as follows:

By default, the VS development environment does not recognize jni. h header file. in JDK, find jni. h, and add jni. h directory to the current working path: Right-click the project, property-general property-C/C ++-add % JAVA_HOME %/include and % JAVA_HOME %/include/win32 to the additional include directory on the right.

-I indicates the link in the command-Fehello. dll is followed by the generated dll name hello. dll-LD, which is an option to create a dynamic link library.

Note:

Create DLL files in the form of multiple files

Cl-I % java_home % \ include \ win32-LD com_jni_HelloWorld.cpp

XXXX. cpp... (cpp file)-Fehello. dll

4) run the java Project

Place the generated hello. dll file under the C: \ Windows \ System32 directory

Note:

C: \ Windows \ System32 is a path that can be accessed by java.

Note:

The DLL file is successfully accessed.

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.