I'm creating a C++ wrapper for an existing jarfile. In this case, I'm doing this with the Spigot Minecraft server jarfile.
When I execute the application, I have the issue where the input and the output of the application is dominated by the Java application. This means that when the java application terminates successfully, so does the C++ application, which indicates that the file descriptor for stdin is getting closed.
I've looked through a number of existing stackoverflow posts, and the closest that I've seen to achieve this, was making use of a forked process, and then piping the file descriptors using pipe()
and dup()
:
C/Linux - having trouble with redirecting stdin and stout
I'm currently rebuilding the code to make it more portable, and allow me to add additional functionality to the C++ code, but the following code is what I've used to get started, and is what I'll be using to test this.
#include <jni.h>
#include <iostream>
using namespace std;
int main () {
JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption* options = new JavaVMOption[1];
options[0].optionString = "-Djava.class.path=/path/to/spigot.jar";
vm_args.version = JNI_VERSION_1_8;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
jint instance = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
delete options;
if (instance != JNI_OK) {
cin.get();
exit(EXIT_FAILURE);
}
cout << "JVM Version: ";
jint ver = env->GetVersion();
cout << ((ver>>16)&0x0f) << "." << (ver&0x0f) << endl;
jclass cls = env->FindClass("org/bukkit/craftbukkit/Main");
if (cls == nullptr) {
cout << "Error starting minecraft" << endl;
} else {
jmethodID mid = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
if (mid == nullptr) {
cout << "Error: main not found" << endl;
} else {
jobjectArray arr = env->NewObjectArray(1,
env->FindClass("java/lang/String"),
env->NewStringUTF(""));
env->CallStaticVoidMethod(cls, mid, arr);
cout << "Started" << endl;
cout << endl;
}
}
cin.get();
jvm->DestroyJavaVM();
return 0;
}
Ideally, I would like to have the input and output of the java application run on a different set of file descriptors for stdin, stdout, and stderr, without forking it.
Is there a way to indicate to the JVM, using the JNI library within c++ to achieve this goal?