C + + calls Java method Jvm.dll related error "Every time after the record has been resolved ..."

Source: Internet
Author: User

In the course of programming learning, the limitations of female thinking are increasingly felt.

Background introduction:

To learn about C + + Java calls in Jni, practice while reading the JNI technical Manual. However, according to the sample code, there are always various

Strange question (once regretted to have installed the Win7 64-bit system, and the computer was pre-installed Win8 32-bit ... )。 Because the Java section of the code is very simple,

Without any errors, the Javac, Javah commands can also output. class files and header files. But the C + + section has an error. The sample code you are learning is intended

Call Java through C + + to generate a startup file for an. exe.

The Java code is as follows:

1  Public classC2java {2     3      PublicC2java () {4         Super();5     }6     7      Public Static intAddintAintb) {8         returnA +b;9     }Ten      One      Public BooleanJudgeBooleanbool) { A         return!bool; -     }     -}

The C + + code is as follows:

//c2java.cpp: Defines the entry point of the console application. #include"stdafx.h"#include"windows.h"using namespacestd;intMainintargcChar*argv[]) {javavmoption options[1]; JNIEnv*env; JAVAVM*JVM;     Javavminitargs Vm_args; Longstatus;     Jclass CLS;     Jmethodid mid;     Jint Square;     Jboolean not;        Jobject jobj; options[0].optionstring ="-djava.class.path=."; Vm_args.version=jni_version_1_6; Vm_args.noptions=1; Vm_args.options=options; Vm_args.ignoreunrecognized=jni_true; Status= JNI_CREATEJAVAVM (&AMP;JVM, (void* *) &env,&Vm_args); if(Status! =Jni_err) {CLS= Env->findclass ("C2java"); if(CLS! =0) {Mid= Env->getstaticmethodid (CLS,"Add","(II) I"); if(Mid! =0) {Square= Env->callstaticintmethod (CLS, Mid,5,5); Std::cout<< Square <<Std::endl; } Mid= Env->getmethodid (CLS,"<init>","() V"); if(Mid! =0) {Jobj=env->NewObject (CLS, mid); Std::cout<<"Init OK"<<Std::endl; } Mid= Env->getmethodid (CLS,"Judge","(z) z"); if(Mid! =0) { not= Env->callbooleanmethod (Jobj, Mid,1); if(!Not ) {Std::cout<<"Boolean OK"<<Std::endl; }}} JVM-DESTROYJAVAVM (); return 0; } Else        return-1; }

Because the Jvm.dll dynamic link library file can be loaded in two ways during the creation of the JVM, a distinct error was found in the two-way attempt.

Error content:

Error 1: Implement static loading of DLL files by adding Jvm.dll paths in path, adding jvm.lib dependent libraries in project/properties/linker/outputs using the code above. Repeatedly tested many times,

Always error is as follows:

Single-step debugging does not seem to be able to use, check the relevant information, the console changed to window report more ... Then carefully search the relevant tutorials, according to: http://andy-li-chn.iteye.com/blog/1175075

Saying: "The custom class JVM cannot be found, the example is just to let yourself write a custom class, but it does not say where the generated classes should be placed to be properly found by the JVM." Typically, our class is placed on a specific

directory, and is not secure under the JVM path, so most examples on the web are missing a step, setting the path to the custom class. " Based on this idea, decide to customize the classpath and load DLL files dynamically.

Error 2: Modify the relevant section of your C + + code to:

//defines a function pointer, which is used to point to the JNI_CREATEJAVAVM function in the JVMtypedef jint (WINAPI *PFUNCREATEJAVAVM) (JAVAVM * *,void**,void*); Const  CharSzjvmpath[] ="C:\\Program Files\\java\\jdk1.7.0_51\\jre\\bin\\server\\jvm.dll";HINSTANCE hinstance =:: LoadLibrary (Szjvmpath); if(HInstance = =NULL) {cout<<"LoadLibrary False"<<Endl; cout<<::getlasterror () <<Endl; return-2; }       //get the JNI_CREATEJAVAVM function pointer insidePFUNCREATEJAVAVM FUNCREATEJAVAVM = (PFUNCREATEJAVAVM):: GetProcAddress (HINSTANCE,"JNI_CREATEJAVAVM"); Status= (*FUNCREATEJAVAVM) (&AMP;JVM, (void* *) &env, &vm_args);

after the run, the return value of -2,getlasterror is 193, which means that the DLL file was loaded dynamically and failed. When you set a breakpoint to view the contents of each variable, you find that the return value of LoadLibrary is empty. After accessing the information

The modification still does not work correctly. There are no methods found to successfully resolve this issue T.T

Run.

Come on, save me. Magic Blog Park ... I just hope that every stupid mistake can be engraved in my mind deeply.

C + + calls Java method Jvm.dll related error "Every time after the record has been resolved ..."

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.