Example #1
0
__declspec( dllexport ) int printfFromJava(){
	JavaVMInitArgs vmArgs;
	vmArgs.version = JNI_VERSION_1_2;
	vmArgs.nOptions = 1;
	vmArgs.ignoreUnrecognized = JNI_TRUE;

	const char* class_ = "Main";
	const char* jar = 0;
	int argc = 0;
	const char** argv = 0;
	const char* classpath = ".";

	++vmArgs.nOptions;

	RUNTIME_ARRAY(JavaVMOption, options, vmArgs.nOptions);
	vmArgs.options = RUNTIME_ARRAY_BODY(options);

	unsigned optionIndex = 0;

	vmArgs.options[optionIndex++].optionString
	= const_cast<char*>("-Davian.bootimage=bootimageBin");

	vmArgs.options[optionIndex++].optionString
	= const_cast<char*>("-Davian.codeimage=codeimageBin");

#define CLASSPATH_PROPERTY "-Djava.class.path="

	unsigned classpathSize = strlen(classpath);
	unsigned classpathPropertyBufferSize
	= sizeof(CLASSPATH_PROPERTY) + classpathSize;

	RUNTIME_ARRAY(char, classpathPropertyBuffer, classpathPropertyBufferSize);
	memcpy(RUNTIME_ARRAY_BODY(classpathPropertyBuffer),
		CLASSPATH_PROPERTY,
		sizeof(CLASSPATH_PROPERTY) - 1);
	memcpy(RUNTIME_ARRAY_BODY(classpathPropertyBuffer)
		+ sizeof(CLASSPATH_PROPERTY) - 1,
		classpath,
		classpathSize + 1);

	vmArgs.options[optionIndex++].optionString
	= RUNTIME_ARRAY_BODY(classpathPropertyBuffer);

	JavaVM* vm;
	void* env;
	JNI_CreateJavaVM(&vm, &env, &vmArgs);
	//JNIEnv* e = static_cast<JNIEnv*>(env);
}
Example #2
0
int
main(int ac, const char** av)
{
  JavaVMInitArgs vmArgs;
  vmArgs.version = JNI_VERSION_1_2;
  vmArgs.nOptions = 1;
  vmArgs.ignoreUnrecognized = JNI_TRUE;

  const char* class_ = 0;
  const char* jar = 0;
  int argc = 0;
  const char** argv = 0;
  const char* classpath = ".";

  for (int i = 1; i < ac; ++i) {
    if (strcmp(av[i], "-cp") == 0
        or strcmp(av[i], "-classpath") == 0)
    {
      if (i + 1 == ac) usageAndExit(av[0]);
      classpath = av[++i];
    } else if (strcmp(av[i], "-jar") == 0)
    {
      if (i + 1 == ac) usageAndExit(av[0]);
      jar = av[++i];
    } else if (strncmp(av[i], "-X", 2) == 0
               or strncmp(av[i], "-D", 2) == 0)
    {
      ++ vmArgs.nOptions;
    } else if (strcmp(av[i], "-client") == 0
               or strcmp(av[i], "-server") == 0)
    {
      // ignore
    } else {
      if (jar == 0) {
        class_ = av[i++];
      }
      if (i < ac) {
        argc = ac - i;
        argv = av + i;
        i = ac;
      }
    }
  }

  if (jar) {
    classpath = jar;
    
    class_ = mainClass(jar);

    if (class_ == 0) {
      fprintf(stderr, "Main-Class manifest header not found in %s\n", jar);
      exit(-1);
    }
  }

#ifdef BOOT_LIBRARY
  ++ vmArgs.nOptions;
#endif

#ifdef BOOT_IMAGE
  vmArgs.nOptions += 2;
#endif

#ifdef BOOT_BUILTINS
  ++ vmArgs.nOptions;
#endif

  RUNTIME_ARRAY(JavaVMOption, options, vmArgs.nOptions);
  vmArgs.options = RUNTIME_ARRAY_BODY(options);

  unsigned optionIndex = 0;

#ifdef BOOT_IMAGE
  vmArgs.options[optionIndex++].optionString
    = const_cast<char*>("-Davian.bootimage=bootimageBin");

  vmArgs.options[optionIndex++].optionString
    = const_cast<char*>("-Davian.codeimage=codeimageBin");
#endif

#ifdef BOOT_LIBRARY
  vmArgs.options[optionIndex++].optionString
    = const_cast<char*>("-Davian.bootstrap=" BOOT_LIBRARY);
#endif

#ifdef BOOT_BUILTINS
  vmArgs.options[optionIndex++].optionString
    = const_cast<char*>("-Davian.builtins=" BOOT_BUILTINS);
#endif

#define CLASSPATH_PROPERTY "-Djava.class.path="

  unsigned classpathSize = strlen(classpath);
  unsigned classpathPropertyBufferSize
    = sizeof(CLASSPATH_PROPERTY) + classpathSize;

  RUNTIME_ARRAY(char, classpathPropertyBuffer, classpathPropertyBufferSize);
  memcpy(RUNTIME_ARRAY_BODY(classpathPropertyBuffer),
         CLASSPATH_PROPERTY,
         sizeof(CLASSPATH_PROPERTY) - 1);
  memcpy(RUNTIME_ARRAY_BODY(classpathPropertyBuffer)
         + sizeof(CLASSPATH_PROPERTY) - 1,
         classpath,
         classpathSize + 1);

  vmArgs.options[optionIndex++].optionString
    = RUNTIME_ARRAY_BODY(classpathPropertyBuffer);

  for (int i = 1; i < ac; ++i) {
    if (strncmp(av[i], "-X", 2) == 0
        or strncmp(av[i], "-D", 2) == 0)
    {
      vmArgs.options[optionIndex++].optionString = const_cast<char*>(av[i]);
    }
  }

  if (class_ == 0) {
    usageAndExit(av[0]);
  }

  JavaVM* vm;
  void* env;
  JNI_CreateJavaVM(&vm, &env, &vmArgs);
  JNIEnv* e = static_cast<JNIEnv*>(env);

  jclass c = 0;
  if (not e->ExceptionCheck()) {
    c = e->FindClass(class_);
  }

  if (jar) {
    free(const_cast<char*>(class_));
  }

  if (not e->ExceptionCheck()) {
    jmethodID m = e->GetStaticMethodID(c, "main", "([Ljava/lang/String;)V");
    if (not e->ExceptionCheck()) {
      jclass stringClass = e->FindClass("java/lang/String");
      if (not e->ExceptionCheck()) {
        jobjectArray a = e->NewObjectArray(argc, stringClass, 0);
        if (not e->ExceptionCheck()) {
          for (int i = 0; i < argc; ++i) {
            e->SetObjectArrayElement(a, i, e->NewStringUTF(argv[i]));
          }
          
          e->CallStaticVoidMethod(c, m, a);
        }
      }
    }
  }

  int exitCode = 0;
  if (e->ExceptionCheck()) {
    exitCode = -1;
    e->ExceptionDescribe();
  }

  vm->DestroyJavaVM();

  return exitCode;
}