File: library.c

package info (click to toggle)
icepick 0.01-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 19,608 kB
  • ctags: 28,706
  • sloc: java: 134,567; sh: 7,395; xml: 1,565; makefile: 530; ansic: 217
file content (91 lines) | stat: -rw-r--r-- 1,969 bytes parent folder | download
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
#include <jni.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
  JNIEnv *env;
  JavaVM *vm;
  JavaVMInitArgs vm_args;
  JavaVMOption options[argc-1];
  char** read_pos;
  char** write_pos;
  int a;
  jint vm_count;
  jint main_count;
  jint res;
  jclass cls;
  jmethodID main_method;
  jclass string_class;
  jobjectArray args;

  vm_args.version = JNI_VERSION_1_2;
  vm_args.ignoreUnrecognized = JNI_TRUE;
  read_pos = argv + 1;
  write_pos = argv;
  for (a = 0; a < argc - 1; ++a)
    {
      if (strncmp(*read_pos, "-J", (size_t)2) == 0)
	{
	  options[vm_count].optionString = *read_pos + 2;
	  ++vm_count;
	}
      else
	{
	  *write_pos = *read_pos;
	  ++write_pos;
	  ++main_count;
	}
      ++read_pos;
    }
  for (a = 0; a < main_count; ++a)
    printf("main option %d: %s\n", a, argv[a]);
  for (a = 0; a < vm_count; ++a)
    printf("vm option %d: %s\n", a, options[a].optionString);
  vm_args.options = options;
  vm_args.nOptions = vm_count;

  res = JNI_CreateJavaVM(&vm, (void**)&env, &vm_args);
  if (res < 0)
    {
      fprintf(stderr, "Couldn't create Java VM\n");
      exit(-1);
    }
  cls = (*env)->FindClass(env, "CLASS_NAME");
  if (cls == NULL) {
    goto destroy;
  }
 
  main_method = (*env)->GetStaticMethodID(env, cls, "main",
					  "([Ljava/lang/String;)V");
  if (main_method == NULL) {
    goto destroy;
  }

  string_class = (*env)->FindClass(env, "java/lang/String");
  args = (*env)->NewObjectArray(env, main_count, string_class, NULL);
  if (args == NULL) {
    goto destroy;
  }

  for (a = 0; a < main_count; ++a)
    {
      jstring jstr;

      jstr = (*env)->NewStringUTF(env, *argv);
      if (jstr == NULL) 
	goto destroy;
      ++argv;
      (*env)->SetObjectArrayElement(env, args, a, jstr);
    }

  (*env)->CallStaticVoidMethod(env, cls, main_method, args);
 
 destroy:
  if ((*env)->ExceptionOccurred(env)) 
    (*env)->ExceptionDescribe(env);
  (*vm)->DestroyJavaVM(vm);
  
  return 0;
}