Created
January 20, 2022 15:07
-
-
Save shannah/57fc4bf4be42cac4e2ab4475f1d308a6 to your computer and use it in GitHub Desktop.
Sample using JLI_Launch to launch JVM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <jni.h> | |
#include <string.h> | |
#include <objc/objc-runtime.h> | |
#ifdef _WIN32 | |
#include <windows.h> | |
#define LIBTYPE HINSTANCE | |
#define OPENLIB(libname) LoadLibrary(TEXT(libname)) | |
#define LIBFUNC(lib, fn) GetProcAddress((lib), (fn)) | |
#else | |
#include <dlfcn.h> | |
#define LIBTYPE void* | |
#define OPENLIB(libname) dlopen((libname), RTLD_LAZY) | |
#define LIBFUNC(lib, fn) dlsym((lib), (fn)) | |
#endif | |
typedef int (JNICALL *JLI_Launch_f)(int argc, char ** argv, | |
int jargc, const char** jargv, | |
int appclassc, const char** appclassv, | |
const char* fullversion, | |
const char* dotversion, | |
const char* pname, | |
const char* lname, | |
jboolean javaargs, | |
jboolean cpwildcard, | |
jboolean javaw, | |
jint ergo); | |
typedef jint (*JNI_CreateJavaVM_f)(JavaVM **, void **, void *); | |
int launchjli(char *jlilib, int argc, char** argv) { | |
LIBTYPE libJLI = OPENLIB(jlilib); | |
if (!libJLI) | |
{ | |
printf("Unable to load library %s\n", jlilib); | |
return 1; | |
} | |
JLI_Launch_f JLI_Launch = LIBFUNC(libJLI, "JLI_Launch"); | |
if (!JLI_Launch) | |
{ | |
printf("Unable to get symbol JLI_Launch from library %s\n", jlilib); | |
return 1; | |
} | |
int result = JLI_Launch(argc, argv, | |
0, NULL, | |
0, NULL, | |
"11.0.0", | |
"11.0.0", | |
"java", | |
"java", | |
JNI_FALSE, | |
JNI_FALSE, | |
JNI_FALSE, | |
0); | |
return result; | |
} | |
int launch() { | |
int argc =3; | |
char *argv[argc]; | |
int idx=0; | |
argv[idx++] = strdup("Main"); | |
argv[idx++] = strdup("-jar"); | |
argv[idx++] = strdup("javafx-crash-test-1.0-SNAPSHOT.jar"); | |
argv[idx] = NULL; | |
return launchjli("/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/lib/jli/libjli.dylib", argc, argv); | |
} | |
int main() { | |
id pool = ((id (*)(id, SEL))objc_msgSend)((id)objc_getClass("NSAutoreleasePool"), | |
sel_registerName("new")); | |
int result; | |
result = launch(); | |
((void (*)(id, SEL))objc_msgSend)(pool, | |
sel_registerName("drain")); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment