Ticket #245: CreateVM.cpp

File CreateVM.cpp, 5.7 KB (added by jep, 7 years ago)

CPP version for g++ (need var.hpp)

Line 
1//#define dllexport _System
2//#define dllimport _System
3
4#define INCL_DOS
5#define INCL_DOSMODULEMGR
6#define INCL_DOSMISC
7#define INCL_DOSERRORS
8#include <os2.h>
9#include <stdio.h>
10#include <ctype.h>
11#include <jni.h>
12#include <stdlib.h>
13#include <iostream>
14#include "var.hpp"
15
16
17//Ensure to add "APIRET rc = NO_ERROR;" to every function that call INITPROC as it set rc
18#ifdef __WATCOMC__
19  #define INITPROC( h, r, f, p ) r( __syscall *f)p; rc = DosQueryProcAddr( h, NULL, #f, (PFN *) &f ); if( rc != NO_ERROR ) cerr << "Unable to load " << #f;
20#else
21  #define INITPROC( h, r, f, p ) r(*f)p; rc = DosQueryProcAddr( h, NULL, ( PCSZ )#f, (PFN *) &f ); if( rc != NO_ERROR ) cerr << "Unable to load " << #f;
22#endif
23HMODULE hmod;
24
25APIRET loadLibrary() {
26
27    var static_variable;
28    var szFailName( _MAX_PATH, Grow );
29    var LIBPATH( 1024, Grow );
30    var SearchPath( _MAX_PATH, Grow );
31    var JAVAHome( _MAX_PATH, Grow );                       /* Environment variable to JAVA   */
32    APIRET rc = NO_ERROR;                                  /* Return code                  */
33    int i = 0;
34
35    int counter = 0;
36    rc = DosQueryExtLIBPATH( LIBPATH, BEGIN_LIBPATH );     /* Query the BeginLIBPATH */
37
38    if( !LIBPATH.contains( "\\bin\\client" ) ) {
39        rc = DosQueryExtLIBPATH( LIBPATH, END_LIBPATH );   /* Query the EndLIBPATH */
40        if( !LIBPATH.contains( "\\bin\\client" ) ) {
41            for( ; counter < 3; counter++ ) {
42                rc = DosSearchPath( SEARCH_CUR_DIRECTORY | SEARCH_IGNORENETERRS,
43                                   LIBPATH,                /* Path value just obtained */
44                                   static_variable.cpy( "JAVA.EXE" ),        /* Name of file to look for */
45                                   JAVAHome,               /* Result of the search     */
46                                   _MAX_PATH ); /* Length of search buffer  */
47
48                cout << "LibPath: " << LIBPATH << " rc: " << rc << " JAVAHome: " << JAVAHome << endl;
49
50                if( rc != NO_ERROR ) {
51                    if( counter < 1 ) {
52                        rc = DosScanEnv( static_variable.cpy( "JAVA_HOME" ), LIBPATH ); /* Get contents of environment variable */
53                    } else {
54                        rc = DosScanEnv( static_variable.cpy( "PATH" ), LIBPATH );      /* Get contents of PATH environment */
55                    }
56                } else {
57                    break;
58                }
59            }
60        }
61        cout << "JAVAHome: " << JAVAHome << endl;
62        JAVAHome.reSize().before( '\\', true ).reSize(); /* To last \ */
63        cout << "JAVAHome: " << JAVAHome << endl;
64        JAVAHome.cpy( "D:\\PROGRAMS\\OPENJDK\\BIN" );
65        cout << "JAVAHome: " << JAVAHome << endl;
66        LIBPATH.reSize( 0 ).flush().cat( JAVAHome, ';', JAVAHome, "\\client;%PATH%" );
67        rc = DosSetExtLIBPATH( LIBPATH,
68                              BEGIN_LIBPATH );             /* Add to beginning of LIBPATH */
69    }
70
71    rc = DosLoadModule( szFailName,                        /* Failed module name */
72                       sizeof( szFailName ),               /* Size of buffer     */
73                       static_variable.cpy( "jvm.dll" ),                          /* Name of DLL        */
74                       &hmod );                            /* Module handle here */
75    if( rc != NO_ERROR ) {
76        DosFreeModule( hmod );                             /* Frees the DLL module */
77        return ERROR_FILE_NOT_FOUND;
78    }
79    return NO_ERROR;
80}
81
82JNIEnv* create_vm() {
83    APIRET rc = loadLibrary();
84    if( rc == NO_ERROR ) {
85        JavaVM* jvm;
86        JNIEnv* env;
87        JavaVMInitArgs args;
88        //memset(&args, 0, sizeof(args));
89        //memset(options, 0, sizeof(options));
90        JavaVMOption options[] = {
91            { "-Xmx512m", NULL },
92            { "-XX:+TraceClassLoading", NULL },//To see the origin of the problem class
93            { "-Djava.class.path=.", NULL },  //D:\\Programs\\vxrexx\\projects\\ODBCADMN\\j4o";
94            { "-Djava.library.path=.", NULL },// set native library path
95            { "-Djava.compiler=NONE", NULL }, // disable JIT
96            { "-verbose:gc", NULL },          // print gc messages
97            { "-verbose:jni", NULL },         // print JNI-related messages
98            { "-verbose:class", NULL }        // print class loading messages
99        };
100
101        args.version = JNI_VERSION_1_6;
102        args.nOptions = sizeof( options ) / sizeof( JavaVMOption );
103        args.options = options;
104        args.ignoreUnrecognized = JNI_FALSE;
105
106        INITPROC( hmod, jint, JNI_CreateJavaVM, ( JavaVM **p_vm, void **p_env, void *vm_args ) );
107        jint jrc = JNI_CreateJavaVM( &jvm, (void**)&env, &args );
108//#ifdef __WATCOMC__
109        if( jrc != JNI_OK )
110            cerr << "Problems loading JVM" << endl;
111        else
112            cout << "JVM has been loaded sucessfully" << endl;
113//#endif
114        //delete options;
115        return env;
116    }
117    return NULL;
118}
119
120void invoke_class( JNIEnv* env ) {
121  jclass helloWorldClass;
122  jmethodID mainMethod;
123  jobjectArray applicationArgs;
124  jstring applicationArg0;
125
126  helloWorldClass = env->FindClass( "HelloWorld" );
127
128  mainMethod = env->GetStaticMethodID( helloWorldClass, "main", "([Ljava/lang/String;)V" );
129
130  applicationArgs = env->NewObjectArray( 1, env->FindClass( "java/lang/String" ), NULL );
131  applicationArg0 = env->NewStringUTF( "From-C-program" );
132  env->SetObjectArrayElement( applicationArgs, 0, applicationArg0 );
133
134  env->CallStaticVoidMethod( helloWorldClass, mainMethod, applicationArgs);
135}
136
137int main( int argc, char **argv ) {
138    JNIEnv* env = create_vm();
139    if( env )
140        invoke_class( env );
141    else
142        return 1;
143  return 0;
144}