Java JNI programming skills in windows -- Java calls C/C ++ (4)

Source: Internet
Author: User

Step 5: create a shared library file

Next, we will create a localCode.

In addition to creating executable machine code files, most C and C ++ compilers can also create shared library files.

The command used to create shared library files depends on the compiler you use.

The following commands are executed in windows.

Windows:

Use Visual Studio commandprompttool cl.exe

CL-I "C: \ Program Files \ Java \ jdk1.6.0 _ 10 \ include"-I "C: \ Program Files \ Java \ jdk1.6.0 _ 10 \ include \ Win32 "-LD test_jni_javacallc_test.c-Fe test_jni_javacallc_test.dll

You can also use vc6.0 to directly create a dynamic library.





During compilation, JNI-related header files and library files are required. Add two Java-related paths to the search path of vc6.0.

Tools> sptions> Directories



Linux: using the GCC Tool
Gcc-C-FPIC-I/usr/Java/jdk1.6.0 _ 22/include/-I/usr/Java/jdk1.6.0 _ 22/include/Linux/sample1.c
Gcc-shared-FPIC-O libsample1.so sample1.o

Step 6: run JavaProgram
The last step is to run the Java program and ensure that the code works correctly.

Because all Java code must be executed in the Java virtual machine, you need to use the Java Runtime Environment.

One way to complete this step is to use Java, which is a Java interpreter provided along with the SDK.

The command used is:

Java-CP. Test.Test_jni_javacallc_test

Or run it directly in the external ose.

Output:
Intmethod: 25
Booleanmethod: false
Stringmethod: Java
Intarraymethod: 33

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.