Ticket #245: CreateVM.c

File CreateVM.c, 3.2 KB (added by jep, 8 years ago)

C version for OW

Line 
1#define dllexport _System
2#define dllimport _System
3
4#define INCL_DOSMISC
5#define INCL_DOSERRORS
6#include <os2.h>
7#include <stdio.h>
8#include <ctype.h>
9#include <jni.h>
10#include <libgen.h>
11
12char* stristr( const char* haystack, const char* needle ) {
13 do {
14 const char* h = haystack;
15 const char* n = needle;
16 while( toupper( (unsigned char) *h ) == toupper( (unsigned char) *n ) && *n ) {
17 h++;
18 n++;
19 }
20 if ( *n == 0 )
21 return (char *) haystack;
22 } while( *haystack++ );
23 return 0;
24}
25
26JNIEnv* create_vm() {
27
28 UCHAR uchLIBPATH[512] = "";
29 PSZ pszJAVAHome = ""; /* Environment variable to JAVA */
30 UCHAR SearchResult[256] = ""; /* Result of PATH search */
31 APIRET rc = NO_ERROR; /* Return code */
32
33 JavaVM* jvm;
34 JNIEnv* env;
35 JavaVMInitArgs args;
36 JavaVMOption options[1];
37
38 int counter = 0;
39 rc = DosQueryExtLIBPATH( uchLIBPATH, BEGIN_LIBPATH ); /* Query the BeginLIBPATH */
40
41 if( stristr( uchLIBPATH, "\\bin\\client" ) == NULL ) {
42 rc = DosQueryExtLIBPATH( uchLIBPATH, END_LIBPATH ); /* Query the EndLIBPATH */
43 if( stristr( uchLIBPATH, "\\bin\\client" ) == NULL ) {
44 for( ; counter < 2; counter++ ) {
45 if( rc != NO_ERROR ) {
46 rc = DosSearchPath( SEARCH_CUR_DIRECTORY | SEARCH_IGNORENETERRS,
47 pszJAVAHome, /* Path value just obtained */
48 "JAVA.EXE", /* Name of file to look for */
49 SearchResult, /* Result of the search */
50 sizeof( SearchResult ) ); /* Length of search buffer */
51 }
52 if( rc != NO_ERROR ) {
53 if( counter < 1 )
54 rc = DosScanEnv( "JAVA_HOME", &pszJAVAHome ); /* Get contents of environment variable */
55 else
56 rc = DosScanEnv( "PATH", &pszJAVAHome ); /* Get contents of PATH environment */
57 } else
58 break;
59 }
60 rc = DosSetExtLIBPATH( dirname( SearchResult ),
61 BEGIN_LIBPATH ); /* Add to beginning of LIBPATH */
62 }
63 }
64 if( rc == NO_ERROR ) {
65 /* There is a new JNI_VERSION_1_4, but it doesn't add anything for the purposes of our example. */
66 args.version = JNI_VERSION_1_6;
67 args.nOptions = 1;
68 options[0].optionString = "-Djava.class.path=D:\\Programs\\vxrexx\\projects\\ODBCADMN\\j4o\\j4o.class";
69 args.options = options;
70 args.ignoreUnrecognized = JNI_FALSE;
71
72 JNI_CreateJavaVM( &jvm, (void**)env, &args );
73 }
74 return env;
75}
76
77void invoke_class(JNIEnv* env) {
78 jclass helloWorldClass;
79 jmethodID mainMethod;
80 jobjectArray applicationArgs;
81 jstring applicationArg0;
82
83 helloWorldClass = (*env)->FindClass(env, "HelloWorld");
84
85 mainMethod = (*env)->GetStaticMethodID(env, helloWorldClass, "main", "([Ljava/lang/String;)V");
86
87 applicationArgs = (*env)->NewObjectArray(env, 1, (*env)->FindClass(env, "java/lang/String"), NULL);
88 applicationArg0 = (*env)->NewStringUTF(env, "From-C-program");
89 (*env)->SetObjectArrayElement(env, applicationArgs, 0, applicationArg0);
90
91 (*env)->CallStaticVoidMethod(env, helloWorldClass, mainMethod, applicationArgs);
92}
93
94
95int main(int argc, char **argv) {
96 JNIEnv* env = create_vm();
97 invoke_class( env );
98 return 0;
99}