Recently, some netizens asked: how to make a jar file run with JVM startup? Java.exeis not enough to do this. You can define java.exeand set java.exe to be useful in the following situations:
1.if you do not want the program's progress name to be displayed as java.exe
2. Double-click the program to run it.
The following is an example code:
// Jvm_cpp.cpp: defines the entry point for the console application. <br/> // <br/> # include "stdafx. H "<br/> # include" windows. H "<br/> # include <JNI. h> <br/> typedef jint (jnicall * jnicreateproc) (JavaVM **, void **, void *); <br/> bool setstream (jnienv * ENV, const char * pszfilename, const char * pszmethod); <br/> // Method for starting a Java VM <br/> bool startjvm () <br/> {<br/> // JVM dynamic library path <br/> const tchar szjvmpath [] = _ t ("E: // Java // JDK // jdk1.6.0 // JRE // bin // server // JVM. DLL "); <br/> // startup parameter of the Java Virtual Machine. Each parameter must be written. It cannot be written together. <br/> int noptioncount = 2; <br/> javavmoption options [2]; <br/> options [1]. optionstring = "-xmx256m"; <br/> // set classpath <br/> options [0]. optionstring = "-djava. class. path =. /test. jar; "; <br/> javavminitargs vm_args; <br/> vm_args.version = jni_version_1_4; <br/> vm_args.options = options; <br/> vm_args. Noptions = noptioncount; <br/> vm_args.ignoreunrecognized = jni_true; <br/> // the startup class. Note that the Delimiter is/, for example, the startup class test. jtest should be written as test/jtest <br/> const char szstartclass [] = "com/kortide/JVM/test"; <br/> // the startup method, usually the main function, you can also set it to another function <br/> const char szstartmethod [] = "Main "; <br/> // redirection file <br/> const char * szstdoutfilename = "stdout.txt"; <br/> const char * szstderrfilename = "stderr.txt "; <br/> // command line parameters of the Java program <br/> Int nparamcount = 2; <br/> const char * szparams [2] = <br/>{< br/> "arg1", "arg2" <br/> }; <br/> // load JVM. <Br/> hinstance jvmdll = loadlibrary (szjvmpath); <br/> If (jvmdll = NULL) <br/> {<br/> printf ("An error occurred while loading the JVM dynamic library. % L ",: getlasterror (); <br/> return false; <br/>}< br/> // query the jni_createjavavm process. <Br/> jnicreateproc release = (jnicreateproc) getprocaddress (jvmdll, <br/> "jni_createjavavm"); <br/> If (jvmcreateproc = NULL) <br/>{< br/> freelibrary (jvmdll); <br/> printf ("An error occurred while searching for jni_createjavavm. % L ",: getlasterror (); <br/> return false; <br/>}< br/> // create a JVM. <Br/> jnienv * env; <br/> JavaVM * JVM; <br/> jint r = (jvmcreateproc) (& JVM, (void **) & ENV, & vm_args); <br/> If (r <0 | JVM = NULL | Env = NULL) <br/>{< br/> freelibrary (jvmdll ); <br/> printf ("An error occurred while creating JVM. "); <Br/> return false; <br/>}< br/> // rewrite stdout and stderr to the output file <br/> If (! Setstream (ENV, szstdoutfilename, "setout") <br/>{< br/> printf ("failed to set stdout output file"); <br/> return false; <br/>}< br/> If (! Setstream (ENV, szstderrfilename, "seterr") <br/>{< br/> printf ("failed to set stderr output file"); <br/> return false; <br/>}< br/> // load the startup class. <Br/> jclass serviceclass = env-> findclass (szstartclass); <br/> If (env-> predictioncheck () = jni_true | serviceclass = NULL) <br/>{< br/> env-> exceptiondescribe (); <br/> env-> exceptionclear (); <br/> freelibrary (jvmdll ); <br/> printf ("failed to load startup class. "); <Br/> return false; <br/>}< br/> // startup method <br/> jmethodid mid = env-> getstaticmethodid (serviceclass, szstartmethod, <br/> "([ljava/lang/string;) V"); <br/> If (env-> predictioncheck () = jni_true | mid = NULL) <br/>{< br/> env-> exceptiondescribe (); <br/> env-> exceptionclear (); <br/> freelibrary (jvmdll ); <br/> printf ("failed to find the startup method. "); <Br/> return false; <br/>}< br/> // search for the string class. <Br/> jclass stringclass = env-> findclass ("Java/lang/string"); <br/> If (env-> exceptioncheck () = jni_true | stringclass = NULL) <br/>{< br/> env-> predictiondescribe (); <br/> env-> predictionclear (); <br/> freelibrary (jvmdll); <br/> printf ("failed to find the string class. "); <Br/> return false; <br/>}< br/> jstring jstr; <br/> jobjectarray ARGs = 0; <br/> ARGs = env-> newobjectarray (2, stringclass, 0); <br/> for (INT I = 0; I <nparamcount; I ++) <br/>{< br/> jstr = env-> newstringutf (szparams [I]); <br/> If (jstr = 0) <br/>{< br/> printf ("failed to allocate string/N"); <br/> If (env-> exceptionoccurred ()) <br/>{< br/> env-> predictiondescribe (); <br/> env-> predictionclear (); <br/>} <Br/> return false; <br/>}< br/> env-> setobjectarrayelement (ARGs, I, jstr ); <br/> If (env-> exceptioncheck () = jni_true) <br/> {<br/> printf ("parameter setting failed/N "); <br/> If (env-> predictionoccurred () <br/>{< br/> env-> predictiondescribe (); <br/> env-> predictionclear (); <br/>}< br/> return false; <br/>}< br/> // call the startup method of the startup class to start the Java program <br/> // env-> callstaticvoidmethod (serviceclass, mid, parameterarra Y); <br/> env-> callstaticvoidmethod (serviceclass, mid, argS); <br/> If (env-> exceptioncheck () = jni_true) <br/>{< br/> env-> exceptiondescribe (); <br/> env-> exceptionclear (); <br/> freelibrary (jvmdll ); <br/> return false; <br/>}< br/> return true; <br/>}< br/> // method for setting the output stream <br/> bool setstream (jnienv * ENV, const char * pszfilename, const char * pszmethod) <br/>{< br/> int pbuffersize = 1024; <br/> Cha R * pbuffer = new char [pbuffersize]; <br/> // create a String object. <Br/> jstring pathstring = env-> newstringutf (pszfilename); <br/> If (env-> predictioncheck () = jni_true | pathstring = NULL) <br/>{< br/> env-> predictiondescribe (); <br/> env-> predictionclear (); <br/> printf ("failed to create a string. "); <Br/> return false; <br/>}< br/> // find the fileoutputstream class. <Br/> jclass fileoutputstreamclass = env-> findclass ("Java/IO/fileoutputstream"); <br/> If (env-> exceptioncheck () = jni_true | fileoutputstreamclass = NULL) <br/>{< br/> env-> predictiondescribe (); <br/> env-> predictionclear (); <br/> printf ("failed to find the fileoutputstream class. "); <Br/> return false; <br/>}< br/> // find the fileoutputstream class constructor. <Br/> jmethodid fileoutputstreamconstructor = env-> getmethodid (fileoutputstreamclass, <br/> "<init>", <br/> "(ljava/lang/string ;) V "); <br/> If (env-> predictioncheck () = jni_true | fileoutputstreamconstructor = NULL) <br/>{< br/> env-> predictiondescribe (); <br/> env-> predictionclear (); <br/> printf ("An error occurred while searching for the fileoutputstream class constructor. "); <Br/> return false; <br/>}< br/> // create a fileoutputstream class object. <Br/> jobject fileoutputstream = env-> newobject (fileoutputstreamclass, <br/> fileoutputstreamconstructor, pathstring); <br/> If (env-> exceptioncheck () = jni_true | fileoutputstream = NULL) <br/>{< br/> env-> predictiondescribe (); <br/> env-> predictionclear (); <br/> printf ("failed to create the object of the fileoutputstream class. "); <Br/> return false; <br/>}< br/> // search for the printstream class. <Br/> jclass printstreamclass = env-> findclass ("Java/IO/printstream"); <br/> If (env-> exceptioncheck () = jni_true | printstreamclass = NULL) <br/>{< br/> env-> predictiondescribe (); <br/> env-> exceptionclear (); <br/> printf ("An error occurred while searching for the printstream class. "); <Br/> return false; <br/>}< br/> // query the printstream class constructor. <Br/> jmethodid printstreamconstructor = env-> getmethodid (printstreamclass, <br/> "<init>", <br/> "(ljava/IO/outputstream ;) V "); <br/> If (env-> exceptioncheck () = jni_true | printstreamconstructor = NULL) <br/>{< br/> env-> predictiondescribe (); <br/> env-> predictionclear (); <br/> printf ("An error occurred while searching for the printstream class constructor. "); <Br/> return false; <br/>}< br/> // create a printstream class object. <Br/> jobject printstream = env-> newobject (printstreamclass, <br/> printstreamconstructor, fileoutputstream); <br/> If (env-> exceptioncheck () = jni_true | printstream = NULL) <br/>{< br/> env-> predictiondescribe (); <br/> env-> predictionclear (); <br/> printf ("failed to create the printstream class object. "); <Br/> return false; <br/>}< br/> // find the system class. <Br/> jclass systemclass = env-> findclass ("Java/lang/system"); <br/> If (env-> exceptioncheck () = jni_true | systemclass = NULL) <br/>{< br/> env-> predictiondescribe (); <br/> env-> exceptionclear (); <br/> printf ("failed to find the system class. "); <Br/> return false; <br/>}< br/> // find the system class setting method. <Br/> jmethodid setstreammethod = env-> getstaticmethodid (systemclass, pszmethod, <br/> "(ljava/IO/printstream;) V "); <br/> If (env-> predictioncheck () = jni_true | setstreammethod = NULL) <br/>{< br/> env-> predictiondescribe (); <br/> env-> exceptionclear (); <br/> printf ("failed to find the system class setting method. "); <Br/> return false; <br/>}< br/> // set the system class stream. <Br/> env-> callstaticvoidmethod (systemclass, setstreammethod, printstream); <br/> If (env-> exceptioncheck () = jni_true) <br/>{< br/> env-> predictiondescribe (); <br/> env-> predictionclear (); <br/> printf ("failed to set the system class stream. "); <Br/> return false; <br/>}< br/> return true; <br/>}< br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> startjvm (); <br/> return 0; <br/>}< br/>
The above code references the http://www.wangchao.net.cn/bbsdetail_147855.html of how to use C ++ to load Java Virtual Machine