Start class through exe

Source: Internet
Author: User
Yesterday, I downloaded the so-called pure Java version of online game "The Legend of heroes of the Sea" and found that it will include JVM. all the components including DLL and all the classes are encapsulated and used. This method is feasible, which not only ensures pure Java Development in theory, but also avoids the risk of decompilation of core code; as a result, I tried to write a similar method. extract one of them and call the main function directly in the EXE file.

This method is implemented through JNI.

/**
* Start class directly through EXE (without external configuration)
*
* Project: loonframework
* Author: chenpeng
* Email: ceponline@yahoo.com.cn
*/
// PS ...... If you have any questions, we can help you optimize them ......
# Include "stdafx. H"
# Include "JNI. H"

// Displayed in the prompt box
Void MessageBox (lpctstr text );
// Used for path Filtering
Char * dirpath (char * path );
// MessageBox title name.
Static const char messageboxtitle [] = "provided by loonframework ";
// The default relative path of JVM. dll in this program.
Const static char _ default_jvm [] = "// JRE // bin // client // JVM. dll ";
// The Name Of The main function. You can also change it to another name. JVM queries the startup interface.
Const char mainname [] = "Main ";
// The total number of VM startup parameters.
Const int jvmoptioncount = 5;
// Set JVM compiler. If none is set, the default compiler is used.
Static char compiler [] = "-djava. compiler = none ";
// Minimum Memory
Static char minmb [] = "-xms256m ";
// Maximum memory
Static char maxmb [] = "-xmx512m ";
// Path of the main function class in the jar package.
Static char appclass [] = "org/loon/framework/GAME/main ";
// The path of the jar package to be executed. './' is short for the current path. Multiple jar packages are separated.
Static char classpath [] = "-djava. Class. Path =./loonlangrisser0.01.jar ";
Static char librarypath [] = "-djava. Library. Path = ./";
Typedef jint (winapi * jnicreatejavavm) (JavaVM **, jnienv **, void *);
/**
* Win Main Function
**/
Int apientry winmain (hinstance,
Hinstance hprevinstance,
Lptstr lpcmdline,
Int ncmdshow)
{
 
// JVM path (PS: The Chinese path is not supported in this writing ).
Char jvmpath [max_path];
/**
* This program uses the Registry to find the JVM. dll location. If the virtual machine is not registered, the program will be directly obtained in the relative path of the execution file .)
*/
// Set the space size
Char subkey [max_path * 2];
// Copy the Registry path string to the subkey
Lstrcpy (subkey, "software // javasoft // Java Runtime Environment ");
Hkey HK;
// Query the registry and return results
Long result = regopenkeyex (HKEY_LOCAL_MACHINE, subkey, 0, key_read, & HK );
// Host the JVM. dll handle
Hmodule jvm_dll;
// When the registry does not contain software // javasoft // Java Runtime Environment, the JVM. dll is read from the local machine.
If (result! = Error_success ){
// PS: This is used to obtain other processing methods when JVM. dll is directly obtained from the local path.
// Obtain the absolute path of the object. pathlength indicates the length of the returned path.
Int pathlength = getmodulefilename (null, jvmpath, max_path );
// Assemble the actual path
Lstrcat (dirpath (jvmpath), _ default_jvm );
// MessageBox ("software // javasoft // Java Runtime Environment does not exist! ");
// Return-1;
// When the Registry already exists
} Else {
Char feedback [max_path];
// Obtain the space size
DWORD feedback_length = sizeof (feedback) * sizeof (feedback [0]);
Result = regqueryvalueex (HK, "CurrentVersion", null, null, (lpbyte) feedback, & feedback_length );
// Close the registry
Regclosekey (HK );
If (result! = Error_success ){
MessageBox ("software // javasoft // CurrentVersion in Java Runtime Environment failed to be read! ");
Return-1;
}
// Obtain the path (lstrcat is used to assign the second string and the first string to the first string)
Lstrcat (subkey ,"//");
Lstrcat (subkey, feedback );
// Query the Registry
Result = regopenkeyex (HKEY_LOCAL_MACHINE, subkey, 0, key_read, & HK );
If (result! = Error_success ){
Lstrcat (subkey, "not found! ");
MessageBox (subkey );
Return-1;
}

Feedback_length = sizeof (feedback) * sizeof (feedback [0]);
Result = regqueryvalueex (HK, "runtimelib", null, null, (lpbyte) feedback, & feedback_length );
Regclosekey (HK );
If (result! = Error_success ){
Lstrcat (subkey, "failed to read runtime Lib! ");
MessageBox (subkey );
Return-1;
}
// Obtain the JVM. dll path
Lstrcpy (jvmpath, feedback );
}
// Obtain the JVM. dll startup entity
Jvm_dll = loadlibrary (jvmpath );
If (jvm_dll = NULL ){
Lstrcpy (subkey, jvmpath );
Lstrcat (subkey, "loading failed! ");
MessageBox (subkey );
Return-1;
}

// JVM internal function jni_createjavavm read
Jnicreatejavavm createjavavm = (jnicreatejavavm) getprocaddress (jvm_dll, "jni_createjavavm ");
If (createjavavm = NULL ){
MessageBox ("jni_createjavavm function reading failed! ");
Return-1;
}

// Point to the local method call interface
Jnienv * env;
// Indicates the Java Virtual Machine
JavaVM * JVM;

// Set JVM startup parameters
Javavminitargs vm_args;
Javavmoption options [jvmoptioncount];

/**
* Java tools language (jtl) Setting: JVM uses the "instant" Compiler (or JIT [bytecode compiler]) to execute bytecode by default.
* When a class is loaded, JIT converts the class bytecode into machine code. Using JIT causes a short delay after each class is loaded,
* But it can improve the overall performance of the program. In some cases, the execution time can be reduced by a tenth.
* You can use compiler to specify jtl. For example, after compiler = foo is set, the virtual machine in this example searches for the JIT compiler named Foo. dll.
* Other compilers are searched in the JRE/bin directory and on the system path.
* If such a compiler is not found, the virtual machine uses the interpreter by default.
*/
Options [0]. optionstring = compiler;
// Class address
Options [1]. optionstring = classpath;
// Lib address
Options [2]. optionstring = librarypath;
// Minimum Memory
Options [3]. optionstring = minmb;
// Maximum memory
Options [4]. optionstring = maxmb;
// PS: this parameter is used to set the tracking runtime information, which is not required for the moment.
// Options [3]. optionstring = "-verbose: JNI ";

// The JNI version used. Currently, the maximum value is jni_version_1_6.
Vm_args.version = jni_version_1_4;
Vm_args.options = options;
Vm_args.noptions = jvmoptioncount;
Vm_args.ignoreunrecognized = jni_true;

// Start JVM and return the result
Int res = createjavavm (& JVM, & ENV, & vm_args );
If (RES <0 ){
MessageBox ("JVM startup failed! ");
Return-1;
}

// Search for the target class
Jclass clazz = env-> findclass (appclass );
If (clazz = 0 ){
Lstrcpy (subkey, appclass );
Lstrcat (subkey, "class not found! ");
MessageBox (subkey );
Return-1;
}

// Obtain the serial number of the main function
Jmethodid mid = env-> getstaticmethodid (clazz, mainname, "([ljava/lang/string;) V ");
If (mid = 0 ){
Lstrcpy (subkey, appclass );
Lstrcat (subkey, "the main function is not found! ");
MessageBox (subkey );
Return-1;
}

// Start main
Env-> callstaticvoidmethod (clazz, mid, null );

// JVM release
JVM-> destroyjavavm ();

Return 0;
}

/**
* The prompt box encapsulates the original MessageBox
*/
Void MessageBox (lpctstr text ){
MessageBox (null, text, messageboxtitle, mb_iconexclamation | mb_applmodal | mb_ OK | mb_setforeground );
}

/**
* Filter the absolute path of the file and remove the last '/' character.
*/
Char * dirpath (char * path)
{
Char * pH = path;
Char * tag = Ph;
While (* pH)
{
If (* pH) = '//')
Tag = Ph;
++ PH;
}
* Tag = '/0 ';
Return path;
}

Run the following command:

The source code is as follows.

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.