Question

I've created a Windows registry entry in HKEY_CLASSES_ROOT* with the value: "C:\test.exe" "%1" to create a new right-click context menu entry that passes the filepath of the clicked file to the registered test.exe. Inside the test.exe a dll-file is loaded that should inject into an already running jvm to call a method and pass the filepath. The problem is that the .dll does not find a JVM instance although it is running in a while-loop (prototype). Could you please help me to solve that problem? How do I have to access the JVM? Thanks

.java:

public class Main
{
    public static void main(String[] args)
    {   
        while(true)
        {

        }
    }

    public static void readAndOutputFilePath(String result)
    {       
        System.out.println("Java result: "+result); 
        try
        {
            System.in.read();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

.dll:

HMODULE m_hDllInstance = LoadLibrary("jvm.dll");
if( m_hDllInstance == 0)
{
    printf("The jvm.dll could not be found");
    getchar();    
}
else
    printf("jvm.dll found");


JavaVM *jvm;   
JNIEnv *env;  

typedef jint (JNICALL * GetCreatedJavaVMs)(JavaVM**, jsize, jsize*);
GetCreatedJavaVMs jni_GetCreatedJavaVMs = (GetCreatedJavaVMs)GetProcAddress(m_hDllInstance, "JNI_GetCreatedJavaVMs");

jint size = 1;
jint vmCount;

jint ret= jni_GetCreatedJavaVMs(&jvm, size, &vmCount);

cout << endl << "GetCreated: " << vmCount << endl;
getchar();

jint rc = jvm->AttachCurrentThread((void **) & env, NULL);

jclass cls = env->FindClass("Main");
jmethodID mid = env->GetStaticMethodID(cls, "readAndOutputFilePath", "(Ljava/lang/String;)V");

jstring resultString = env->NewStringUTF(path);
env->CallStaticVoidMethod(cls, mid, resultString);
...
Was it helpful?

Solution

There is nothing special about communications with a Java program running in an independent, separate process. Any IPC facility supported by Java can be used. Local sockets is the first thing that comes to mind, but also shared memory and plain old filesystem.

JNI is probably irrelevant, as your Java program is in a separate process - unless you want to use some windows-specific communication modes such as OLE. In that case you'll need to write your communication protocol in another language an load them into your Java program.

OTHER TIPS

You don't seem to understand what a JVM actually is. It is itself a DLL that is loaded into the process. It isn't a 'thing' that's just 'out there'. Using the JNI Invocation API you can start one, and you won't find 'the' JVM in a .exe that you just started yourself unless you do so.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top