Last active
May 3, 2023 11:08
-
-
Save g-pechorin/2d5a332b142d87c6e199fd4ca6af59e3 to your computer and use it in GitHub Desktop.
test of JNI_CreateJavaVM
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 <stdio.h> | |
#include <stdlib.h> | |
/// | |
// include JNI and suppress all the warnings it emits | |
#pragma once | |
#pragma warning( push ) | |
#pragma warning( suppress : 26451 ) | |
#pragma warning( suppress : 26495 ) | |
#pragma warning( suppress : 26812 ) | |
#include <jni.h> | |
#pragma warning( pop ) | |
/// | |
// assertion macros | |
// for things that must always be true or we crash | |
#define REQUIRED(CON) do { if (CON) break; fprintf(stderr, ("REQUIRED !FAILED! " #CON "\n")); exit(EXIT_FAILURE); } while(false) | |
// for things that should be true but we can probably continue without | |
#define EXPECTED(CON) do { if (CON) break; fprintf(stderr, ("EXPECTED ?FAILED? " #CON "\n")); } while(false) | |
int main(int argc, char* argv[]) | |
{ | |
printf("Hello World!\n"); | |
// create and release the JVM several times | |
for (int i = 0; i < 5; ++i) | |
{ | |
printf("\n\nstarting loop %d ...\n", i); | |
JavaVMInitArgs args = {}; | |
args.version = JNI_VERSION_1_2; | |
JavaVMOption options[] = { | |
{"-verbose:shutdown",nullptr}, | |
// {"-Dibm.java9.forceCommonCleanerShutdown=true",nullptr} | |
}; | |
args.nOptions = sizeof(options) / sizeof(JavaVMOption); | |
args.options = options; | |
JavaVM* jvm; | |
JNIEnv* env; | |
// create the jvm | |
auto create_ret = JNI_CreateJavaVM(&jvm, (void**)&env, &args); | |
EXPECTED(JNI_OK == create_ret); | |
if (i % 2) | |
{ | |
auto sys = env->FindClass("java/lang/System"); | |
REQUIRED(sys); | |
auto gc = env->GetStaticMethodID(sys, "gc", "()V"); | |
REQUIRED(gc); | |
env->CallStaticVoidMethod(sys, gc); | |
} | |
// destroy the jvm | |
const auto destroy_ret = jvm->DestroyJavaVM(); | |
EXPECTED(JNI_OK == destroy_ret); | |
printf("\n\tloop %d FINISHED\n\n", i); | |
} | |
return EXIT_SUCCESS; | |
} |
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
λ test | |
Assuming JAVA_HOME ends with \ and that we're running in a VS Tools prompt ... | |
C:\Program Files\Semeru\jdk-18.0.2.9-openj9\ | |
Microsoft (R) C/C++ Optimizing Compiler Version 19.35.32217.1 for x64 | |
Copyright (C) Microsoft Corporation. All rights reserved. | |
cl : Command line warning D9002 : ignoring unknown option '/lib' | |
create.cpp | |
Microsoft (R) Incremental Linker Version 14.35.32217.1 | |
Copyright (C) Microsoft Corporation. All rights reserved. | |
/out:create.exe | |
"C:\Program Files\Semeru\jdk-18.0.2.9-openj9\lib\jvm.lib" | |
create.obj | |
Hello World! | |
starting loop 0 ... | |
Thread "Common-Cleaner" is still alive after running the shutdown hooks. | |
EXPECTED ?FAILED? JNI_OK == destroy_ret | |
loop 0 FINISHED | |
starting loop 1 ... | |
EXPECTED ?FAILED? JNI_OK == create_ret | |
EXPECTED ?FAILED? JNI_OK == destroy_ret | |
loop 1 FINISHED | |
starting loop 2 ... | |
EXPECTED ?FAILED? JNI_OK == create_ret | |
EXPECTED ?FAILED? JNI_OK == destroy_ret | |
loop 2 FINISHED | |
starting loop 3 ... | |
EXPECTED ?FAILED? JNI_OK == create_ret | |
EXPECTED ?FAILED? JNI_OK == destroy_ret | |
loop 3 FINISHED | |
starting loop 4 ... | |
EXPECTED ?FAILED? JNI_OK == create_ret | |
EXPECTED ?FAILED? JNI_OK == destroy_ret | |
loop 4 FINISHED |
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
@ECHO OFF | |
ECHO Assuming JAVA_HOME ends with \ and that we're running in a VS Tools prompt ... | |
ECHO %JAVA_HOME% | |
cl /I "%JAVA_HOME%include" /I "%JAVA_HOME%include\win32" /lib "%JAVA_HOME%lib\jvm.lib" create.cpp | |
CMD /C "SET PATH=%JAVA_HOME%bin\server;%PATH% && create.exe" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment