Skip to content

Instantly share code, notes, and snippets.

@alexminnaar
Last active March 23, 2020 10:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexminnaar/90cf1ea3de45e79a1b14081d90d214b7 to your computer and use it in GitHub Desktop.
Save alexminnaar/90cf1ea3de45e79a1b14081d90d214b7 to your computer and use it in GitHub Desktop.
Using the Ludii jar in cpp with JNI and calling the `listGames()` method
/*
Copyright (c) 2020 Alex Minnaar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//compiled with g++ -g -I/usr/lib/jvm/java-8-openjdk-amd64/include -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux -L/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/server/ ludii_jni_example.cpp -ljvm
#include <iostream>
#include <string.h>
#include <jni.h>
#include <stdlib.h>
using namespace std;
#define PATH_SEPARATOR ';'
#define USER_CLASSPATH "Ludii-0.3.0.jar"
#define GAME_LOADER "player/GameLoader"
JNIEnv *env;
JavaVM *jvm;
jint res;
void initJVM() {
#ifdef JNI_VERSION_1_2
JavaVMInitArgs vm_args;
JavaVMOption options[1];
options[0].optionString =
"-Djava.class.path=" USER_CLASSPATH;
vm_args.version = 0x00010002;
vm_args.options = options;
vm_args.nOptions = 1;
vm_args.ignoreUnrecognized = JNI_TRUE;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
#else
JDK1_1InitArgs vm_args;
char classpath[1024];
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
/* Append USER_CLASSPATH to the default system class path */
sprintf(classpath, "%s%c%s",
vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
vm_args.classpath = classpath;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
#endif /* JNI_VERSION_1_2 */
}
void closeJVM() {
jvm->DestroyJavaVM();
}
int main() {
//initialize the JVM
initJVM();
//Let's try to execute `final String[] games = GameLoader.listGames();` from the Ludii jar
//find the GameLoader class
jclass gameLoader = env->FindClass("player/GameLoader");
if(gameLoader == NULL)
{
cout << "Could not load class!" << endl;
return 1;
}
//find the listGames method
jmethodID mid = env->GetStaticMethodID(gameLoader,"listGames","()[Ljava/lang/String;");
if(mid == NULL)
{
cout << "Could not load method!" << endl;
return 1;
}
//execute the listGames method
jobjectArray stringArray = (jobjectArray) env->CallStaticObjectMethod(gameLoader,mid);
if(stringArray == NULL)
{
cout << "Could not load object!" << endl;
return 1;
}
//print the result of listGames
int stringCount = env->GetArrayLength(stringArray);
for (int i=0; i<stringCount; i++) {
//get array element and convert it from jstrng
jstring string = (jstring) (env->GetObjectArrayElement(stringArray, i));
const char *rawString = env->GetStringUTFChars(string, 0);
cout<<rawString<<endl;
env->ReleaseStringUTFChars(string, rawString);
}
//destroy the JVM
closeJVM();
}
@juliendehos
Copy link

Hi,
Is it possible to close and rerun a JVM using this gist ?
For example, if I change the previous main with this one:

int main() {

    env=0;
    jvm=0;
    initJVM();
    closeJVM();

    env=0;
    jvm=0;
    initJVM();
    closeJVM();

    return 0;
}

then I have the following runtime error:

$ ./test-ludii-jni 
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00000000004012ca, pid=28261, tid=0x00007fe72b950cc0
#
# JRE version: OpenJDK Runtime Environment (8.0_222) (build 1.8.0_222-ga)
# Java VM: OpenJDK 64-Bit Server VM (25.222-bga mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  [test-ludii-jni+0x12ca]  closeJVM()+0xa
#
# Core dump written. Default location: /data/julien/tmp/test-jni/build/core or core.28261
#
# An error report file with more information is saved as:
# /data/julien/tmp/test-jni/build/hs_err_pid28261.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#
Aborted (core dumped)

The execution seems ok when I run only one initJVM/closeJVM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment