C + + cross-language calling Java

Source: Internet
Author: User

C + + cross-language calling Java

The Java JDK provides a JNI interface for C + + programs to invoke Java-compiled classes and methods, primarily dependent on the header file (jni.h) and the Dynamic Library (Jvm.so/jvm.dll), because JNI contains rich interface mappings and cross-language data communication, very complex (pit bottomless ), so this is a simple description of a test program.

At the beginning of the test, I chose the WINDOW7 64 environment, the Java JDK installed is 64 bit, but we all know that the VS compiler program is a 32-bit program by default, so I always fail at LoadLibrary ("Jvm.dll"), so I give up Test in the Windows environment (lazy to compile the 64 program), eventually I used CentOS7 64 to complete the test, and the Java version of the test is 1.7 (the system comes with OPENJDK).

The Java code for the test is shown below.

1  Public classMyTest {2     Private Static intMagic_counter = 777;3 4      Public Static voidcallback () {5System.out.println ("Hello World in Java from Cplusplus");6System.out.print ("Magic Number:");7 System.out.println (magic_counter);8     }9}

After writing the Java test program, use the command: Javac Mytest.java to compile the Java class, and generate the corresponding Mytest.class file, the location of this file is very important, it is related to our C + + JNI program can find this file, because we The C + + JNI program specifies "-djava.class.path=." is the current directory where the C + + program runs, all of which we need to copy the Mytest.class file to the running directory of the C + + program.

Also, because the JNI function needs to pass in the Signature ID of the calling object, we need to know all of the Signature that you use the object. By command: The Javap-s-P mytest.class command can obtain the Signature of all functions and variables. As shown in.

Before compiling the program, you need to know the Java version on your machine, and the location of jni.h and jvm.so, so that the program can be compiled to find the corresponding Java dependencies, here I use the CMake to assist the compilation, the corresponding CMakeList.txt as shown below.

Cmake_minimum_required (VERSION3.5) Project (TESTJNI)Set(Cmake_cxx_flags"${cmake_cxx_flags}-std=c++11") include_directories (#/opt/program/jdk1.8.0_121/include/Linux #/opt/program/jdk1.8.0_121/include/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.111-2.6.7.2. el7_2.x86_64/include/Linux/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.111-2.6.7.2. el7_2.x86_64/include)
Link_directories (#/opt/program/jdk1.8.0_121/jre/lib/amd64/Server/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.111-2.6.7.2. el7_2.x86_64/jre/lib/amd64/server/)Set(Source_files main.cpp) add_executable (Testjni ${source_files}) target_link_libraries (Testjni JVM )

The following is a simple example of a C + + JNI program, as shown below.

#include <iostream>#include<jni.h>#include<memory.h>intMain () {CharOpt1[] ="-djava.compiler=none";/** I don't know what it means, I copied it online.*/    CharOpt2[] ="-djava.class.path=.";/** Specifies the directory where the Java class compiles the. class file*/    CharOpt3[] ="-verbose:none";/** I don't know what it means, I copied it online.*/javavmoption options[3]; options[0].optionstring = OPT1; options[0].extrainfo =NULL; options[1].optionstring = Opt2; options[1].extrainfo =NULL; options[2].optionstring = OPT3; options[2].extrainfo =NULL;    Javavminitargs jargv; Jargv.version= Jni_version_1_6;/** JDK JNI VERSION*/jargv.noptions=3; Jargv.options=options; Jargv.ignoreunrecognized=jni_true; JAVAVM* JVM =NULL; JNIEnv* Jenv =NULL; Jint Res= JNI_CREATEJAVAVM (&AMP;JVM, (void* *) &jenv, &jargv); if(0!=Res)return 1; Jclass JC= Jenv->findclass ("MyTest" ); if(NULL = =JC)return 1; Jmethodid Jmid= Jenv->getstaticmethodid (JC,"Callback","() V" ); if(NULL = =jmid)return 1; Jenv-Callstaticvoidmethod (JC, Jmid); /** No description of space-related JAVAVM and jnienv resource release found on the Web*/Std::cout<<"Hello, world!."<<Std::endl; return 0;}

The result of the final output is consistent with the expected, so here, in such a deep pit, wish everyone good luck oh.

C + + cross-language calling Java

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.