1

First of: I don't know if this problem is rather related to UWP and it's strange security settings or rather me mis-using JNI's Invocation

Anyhow, I use this code to spawn a jvm inside a c++ static library (With /ZW, so compiling for UWP, but disabling it doesn't change a thing).

The following code is based upon Oracle's Notes and openjdk's code (exeinvoke.c, launcher.c, ...):

void JVM4UWP::loadVM(string vmOptions[], int numOptions, int jni_version) {
    /* Copy the VM Options */
    options = new JavaVMOption[numOptions];
    for (uint8_t i = 0; i < numOptions; i++) {
        options[i].optionString = new char[vmOptions[i].length() + 1]; // \0 takes the additional byte
        strncpy_s(options[i].optionString, vmOptions[i].length() + 1, vmOptions[i].c_str(), vmOptions[i].length() + 1);
    }

    //options[0].optionString = "-Djava.class.path=/usr/lib/java";
    vm_args = new JavaVMInitArgs();
    vm_args->version = jni_version;
    vm_args->nOptions = numOptions;
    vm_args->options = options;
    vm_args->ignoreUnrecognized = false;

    /* load and initialize a Java VM, return a JNI interface
    * pointer in env */
    JNI_CreateJavaVM(&jvm, (void**)&env, vm_args);
    delete options;

    /* invoke the Main.test method using the JNI */
    jclass cls = env->FindClass("Main");
    jmethodID mid = env->GetStaticMethodID(cls, "main", "(I)V");
    env->CallStaticVoidMethod(cls, mid, 100);

    /* We are done. */
    jvm->DestroyJavaVM();
}

Now the problem with this is: I added #include <jni.h>, I added lib/jvm.lib as static library (taken from oracle's jdk), however it seems that something is still incomplete.

When I run the application, I get an error upon loadup (App::App of the sample UWP application isn't even run).

The console returns:

Das Programm "[1892] TestJVM.exe" wurde mit Code -1073741515 (0xc0000135) 'Es wurde keine abhängige DLL gefunden' beendet.

Loosely translated it's: Could find no dependant dll.

Now this reminds me of this MSDN Article. I have to admit that I don't fully understand it's contents. Especially since jvm.lib cannot have any Manifests. I tried to add jvm.dll, java.dll and jli.dll to the folder of the .exe but for some reason it still doesn't work.

Since the above code should work without dlls, I think it might be related to UWP, maybe even to my code:

My Setup currently is like this: I have the JVM4UWP static library which in-turn is linked statistically against jvm.lib. This Library is then included into the Demo UWP Project, which I am trying to execute.

MeFisto94
  • 157
  • 5

3 Answers3

1

Note that only subset of usual Windows APIs is available for uwp applications and I doubt that jvm is compatible with these restrictions. "lib/jvm.lib" might actually be an export library, not static library.

Also you probably might want to get familiar with Using a Win32 DLL in a Universal Windows Platform App.

user7860670
  • 35,849
  • 4
  • 58
  • 84
1

It looks like JVM library is missing.

Take a look here for a sample where JVM is executed from C code:

http://jnicookbook.owsiak.org/recipe-no-027/

This sample is little bit more complex (it uses POSIX threads) but still, basics are the same.

Make sure that all libraries are on PATH so they can be loaded by either C++ or JVM.

Have fun with JNI!

Oo.oO
  • 12,464
  • 3
  • 23
  • 45
0

Okay, so I made progress: This Question, compiling for Win32 and the fact that you have to embed dll's so they are available, showed me that the jvm.dll was missing, even though I added it to the program's path. Using gflags as pointed out above shows you which dlls it's trying to load and where it tried to locate them.

Actually, you don't have to embed dll's for debugging purposes, but the dlls have to reside inside of the AppX Folder, instead of the Root folder (even though there is also an exe file there).

So since it's not mentioned elsewhere: When linking against jvm.lib, you still need the jvm.dll since this lib only seems to handle dynamic loading/wrapping/etc

Note that only subset of usual Windows APIs is available for uwp applications and I doubt that jvm is compatible with these restrictions.

Yes and no, I had a look at openJDKs Sourcecode and the Majority of it is only File Handling and such, so I guess the part which could require reworking is the Memory and Threading related things.

Anyhow I was expecting such errors rather as Access Violations since some OS imports are simply 0x0 (aka not found), so I could now what I have to change to make it UWP compatible. I might also give recompiling the openJDK with /ZW a try, but it's a challenging task.

Using a Win32 DLL in a Universal Windows Platform App also tells that at least the static librarys seem to work as is, maybe one needs wrappers for the illegal API to link into the project.

Community
  • 1
  • 1
MeFisto94
  • 157
  • 5