Current location - Quotes Website - Signature design - How to run jar file with C++ program
How to run jar file with C++ program
First, you need to load the JVM, then find the corresponding function according to the function signature in the jar, and then run the corresponding function.

The sample code is as follows

# Contains? & ltiostream & gt

# Contains? & ltjni.h & gt

# Contains? & ltwindows.h & gt

# Contains? & ltcstring & gt

# Contains? & ltdirect.h & gt

Use? Namespace? std

# Definition? Dylan? 10000

char*? GetJrepath(int? Bit);

int? CallJAR(int? BIT,char*buf,char*classPath,char * function name);

int? main(int? argc,? Charles? *argv[])

{

Charles? *? buff? =? New? char[DIRLEN];

getcwd(buff,DIRLEN);

strcat(buff," \ \ lib \ \ support . jar ");

What if? (argc & lt3)

{

Cout < < "wrong input parameter";

Return? 0;

}

int? Bit? =? 32;

If (! strcmp(argv[2]," 64 "))

Bit? =? 64? ;

//-30? No jre environment installed? -3 1? Failed to create jvm? -32? Related libraries are missing.

What if? (! strcmp(argv[ 1]," 0 "))

{

//-20? Configuration file error? -2 1? JSON exception

Return? CallJAR(BIT,buff," com/run/Author "," start Author ");

}

Or what? If (! strcmp(argv[ 1]," 1 "))

{

Return? CallJAR(BIT,buff," com/run/UpdateStatus "," UpdateStatus 1 ");

}

Or what? If (! strcmp(argv[ 1]," 2 "))

{

Return? CallJAR(BIT,buff," com/run/SendMsg_webchinese "," send SMS ");

}

other

{

Return? CallJAR(BIT,buff," com/run/UpdateStatus "," UpdateANDSMS ");

}

}

char*? GetJrepath(int? Bit)

{

# Definition? MYBUFF? 123

HKEY? hKey

Dragon? lRet

char*? dir? =? New? char[my buff];

DWORD? dwBufLen? =? MYBUFF

char*? s? =? New? char[my buff];

If (bit? ==? 64)

strcpy(s," SOFTWARE \ \ wow 6432 node \ \ Java soft \ \ Java? Runtime? Environment \ \ ";

other

strcpy(s," SOFTWARE\\JavaSoft\\Java? Runtime? Environment \ \ ";

lRet? =? RegOpenKeyEx(HKEY _ LOCAL _ MACHINE,

Text ((TCHAR*)s),

0,

Keyword query value,

& amphKey);

if(lRet! = Error _ Success)

Return? NULL

lRet? =? RegQueryValueEx(hKey,

Text ((TCHAR*) "current version"),

Empty,

Empty,

(LPBYTE)dir,

& ampdwBufLen);

RegCloseKey(hKey);

if(lRet! = Error _ Success)

Return? NULL

strcat(s,dir);

lRet? =? RegOpenKeyEx(HKEY _ LOCAL _ MACHINE,

Text ((TCHAR*)s),

0,

Keyword query value,

& amphKey);

if(lRet! = Error _ Success)

Return? NULL

dwBufLen? =? MYBUFF

lRet? =? RegQueryValueEx(hKey,

Text ((TCHAR *)“runtime lib "),

Empty,

Empty,

(LPBYTE)dir,

& ampdwBufLen);

RegCloseKey(hKey);

if(lRet! = Error _ Success)

Return? NULL

Return? dir

}

int? CallJAR(int? BIT,char*buf,char*classPath,char*functionName)

{

typedef? Quint? (WINAPI? *PFunCreateJavaVM)(JavaVM? **,? Invalid? **,? Invalid? *); ?

int? res?

JavaVMInitArgs? vm _ args?

JavaVMOption? Option [3]; ?

JavaVM? * jvm?

JNIEnv? * env?

int? Ryan. =strlen("-Djava.class.path=。 ;" )+strlen(buf)+20;

char*? Temporary workers? =? New? char[len];

strcpy(temp,"-Djava.class.path=。 ;" );

strcat(temp,buf);

/* Set initialization parameters */?

//Disabled? JIT, this is the explanation in JNI document. Isn't the specific meaning clear? It is not clear which values can be taken. ?

//From the sample code given in the JNI document?

Option [0]. optionString? =? "-DJ ava . compiler = NONE "; ?

//Set the classpath. If the program uses a third-party JAR package, can it also be included in it?

Option [1]. optionString? =? Temperature; ?

//Set the type of displayed message. These values are gc, class and jni. If more than one message is selected at a time, these values are separated by commas, such as -verbose:gc, class?

//This parameter can be used to observe the process of C++ calling JAVA. After setting this parameter, the program will print the call information on the standard output device?

Option [2]. optionString? =? "-Detailed: None"; ?

//Set the version number, including JNI _ version _ 1_ 1, JNI _ version _ 1_2, JNI _ version _ 1_4?

//Just select the latest version number of the JRE version you installed, but your JRE version must be equal to or higher than the specified version number?

vm_args.version? =? JNI _ Version _1_ 4; ?

vm _ args.nOptions? =? 3; ?

vm_args.options? =? Options; ?

//This parameter specifies whether nonstandard parameters are ignored. If JNI_FLASE is filled, JNI_CreateJavaVM will return JNI_ERR when it encounters nonstandard parameters.

vm_args.ignoreUnrecognized? =? JNI _ Really; ?

//Load JVM.DLL dynamic library?

HINSTANCE? hInstance? =? * load the library (GetJrepath(BIT));); ?

What if? (hInstance? ==? NULL)?

{?

Return? -30; ?

}?

//get the JNI_CreateJavaVM function pointer inside?

PFunCreateJavaVM? funCreateJavaVM? =? (PFunCreateJavaVM)::GetProcAddress(h instance," JNI _ CreateJavaVM "); ?

//Call JNI_CreateJavaVM to create a virtual machine?

res? =? (*funCreateJavaVM)。 jvm,? (void * *)& amp; env,? & ampVM _ args); ?

What if? (res? & lt? 0)?

{?

Return? -3 1; ?

}?

//Find the test. Demonstrates a class and returns the class object of a JAVA class.

jclass? cls? =? env->; FindClass (classpath); ?

//Get an instance of a class according to its class object?

What if? (cls==0)

{

printf("FindClass? Failed \ n ");

(JVM)-& gt; DestroyJavaVM(); ?

Return? -32;

}

job object? obj? =? env->; alloc object(cls); ?

//Get the methods in the class. The last parameter is the signature of the method, through javap? -s? -p? -Classpath? Path? filename

jmethodID? mid? =? env->; GetMethodID(cls,functionName,"()I "); ?

What if? (mid==0)

{

printf("getMethodID? Failed \ n ");

(JVM)-& gt; DestroyJavaVM(); ?

Return? -32;

}

//Construct parameters and call the method of the object?

jstring? MSG? =? (jstring)? env->; CallObjectMethod(obj,mid); ?

//Destroy the virtual machine and release the dynamic library?

JVM-& gt; DestroyJavaVM(); ?

* free library (Hinstance); ?

Return? (int)msg;

}