aboutsummaryrefslogtreecommitdiffstats
path: root/CNativeCode/invokejvm.c
blob: dd43a2c0e3264198ef6c86281f77247ad38f2b22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#ifdef _X11_
	#include <dlfcn.h>
#endif

#ifdef _WIN32_	 
	#include <windows.h>
#endif

#include <jni.h>

static char * defargs[] = { "-loadgljlibs", "gl4java.GLContext", "-info" };

#ifdef _X11_
static char * libname="libGL4JavaJauGljJNI13.so";
#endif

#ifdef _WIN32_	 
static char * libname="GL4JavaJauGljJNI13.DLL";
#endif

int main(int argc, char ** argv)
{
     JNIEnv *env;
     JavaVM *jvm=NULL;
     JavaVMInitArgs jvm_args;
     JavaVMOption options[100];
     jstring jstrargs[100];
     jstring jstrdummy;
     jobjectArray jargs;
     int jargnum=0;
     jint res;
     jclass cls;
     jmethodID mid;
     int opt_num=0, i;
     int argidx=1;
#ifdef _WIN32_	 
     HMODULE lib;
#endif
#ifdef _X11_
     void * lib;
     const char *err=NULL;
#endif
     char ** myargv;
  
     if(argc<2)
     {
        printf("loading default gl4java arguments !\n");
        myargv=calloc(4, sizeof(char *));
	memcpy(myargv, argv, sizeof(char *));
     	myargv[argidx] = defargs[0];
     	myargv[argidx+1] = defargs[1];
     	myargv[argidx+2] = defargs[2];
	argv=myargv;
	argc=argidx+3;
     }

     for ( i = 0; i< argc; i++)
     	printf("calling argv[%d]: %s\n", i, argv[i]);

     if(strcmp(argv[argidx],"-loadgljlibs")==0)
     {
        printf("loading default gl4java libraries !\n");
     	argidx++;
#ifdef _X11_
	lib = dlopen (libname, RTLD_LAZY | RTLD_GLOBAL);
	if (lib == NULL)
	{
                err=dlerror();
		printf ("GLERROR: cannot access library %s\n", libname);
                if(err!=NULL) printf("\t dlerror: %s\n", err);
		exit(1);
	}
#endif
#ifdef _WIN32_
    lib = LoadLibrary (libname);
	if (lib == NULL)
	{
		printf ("GLERROR: cannot access library %s\n", libname);
		exit(1);
	}
#endif

     }

     options[opt_num++].optionString = 
     	"-Djava.compiler=NONE";     /* disable JIT */
     options[opt_num++].optionString = 
     	"-Djava.class.path=."; /* user classes */

/*
     options[opt_num++].optionString = 
     	"-verbose:jni"; 
     options[opt_num++].optionString = 
     	"-Djava.library.path=c:\mylibs";
*/

     for(i=0; i<opt_num; i++)
        printf("jvm option[%d]: %s\n", i, options[i].optionString);

     jvm_args.version = JNI_VERSION_1_2;
     jvm_args.options = options;
     jvm_args.nOptions = opt_num;
     jvm_args.ignoreUnrecognized = 1;

     /* Note that in the Java 2 SDK, there is no longer any need to call 
      * JNI_GetDefaultJavaVMInitArgs. 
      */
     res = JNI_CreateJavaVM(&jvm, (void **)&env, &jvm_args);

     if (res == 0)
     {

	cls = (*env)->FindClass(env, argv[argidx]);

	if (cls == 0) {
		fprintf(stderr, "Can't find %s class\n", argv[argidx]);
		exit(1);
	}

	printf("jvm calling class %s\n", argv[argidx]);

	mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");
	if (mid == 0) {
		fprintf(stderr, "Can't find %s.main\n", argv[argidx]);
		exit(1);
	}
	argidx++;

	jstrdummy = (*env)->NewStringUTF(env, " ");
	if (jstrdummy == 0) {
		fprintf(stderr, "Out of memory\n");
		exit(1);
	}

	if(argidx==argc)
	{
		jstrargs[jargnum] = jstrdummy;
		printf("jvm calling jstrargs[%d]: %s\n",
		                        jargnum, " ");
		jargnum++;
	}

        for(; argidx<argc; argidx++, jargnum++)
	{
		jstrargs[jargnum] = (*env)->NewStringUTF(env, argv[argidx]);
		if (jstrargs[jargnum] == 0) {
			fprintf(stderr, "Out of memory\n");
			exit(1);
		}
		printf("jvm calling jstrargs[%d]: %s\n", 
			jargnum, argv[argidx]);
        }

	if(jargnum>0)
	{
	        jargs = (*env)->NewObjectArray(env, jargnum, 
			(*env)->FindClass(env, "java/lang/String"), jstrdummy);
		if (jargs == 0) {
			fprintf(stderr, "Out of memory\n");
			exit(1);
		}
		for(i=0; i<jargnum; i++)
			(*env)->SetObjectArrayElement(env, jargs, 
			                              i, jstrargs[i]);
	        (*env)->CallStaticVoidMethod(env, cls, mid, jargs);
	} 


	(*jvm)->DestroyJavaVM(jvm);
     }

#ifdef _X11_
     if(lib!=NULL)
     {
	      dlclose (lib);
	      lib = NULL;
     }
#endif

#ifdef _WIN32_
     if(lib!=NULL)
     {
	      FreeLibrary(lib);
	      lib = NULL;
     }
#endif

     return 1;
}