We help IT Professionals succeed at work.

JNI problems

mzimmer74 asked
Medium Priority
Last Modified: 2008-03-17
I'm attempting to play around with JNI for a project at work and am running into a few problems.  Here's the setup for what I'm currently doing.

Say I have java class Calc with method bob.  I also have a c dll with a method (say fred) that loads the jvm, looks up method bob in class Calc, calls it, and returns the answer to whoever called fred.  

This all works if I compile the class and run the c executable.  However, things start to fall apart when I get into dlls.  When I compile the c program into a dll, and invoke method fred from another program (the one I want to do this from is excel) I get some weird results.  The first time I call it I am able to call fred, which successfully calls bob, and display the result from fred.

However, when I try to run the macro in excell that calls fred a second time, I freeze up in my dll at the line JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args) and the computer just sits and spins.  I suspect the problem is the JNI_CreateJavaVM method isn't returning.  Now, at the end of method fred, I do get rid of the jvm I get with (*jvm)->DestroyJavaVM(jvm) so it should be cleaned up when I next call the method.

Does anybody have any clue what's going on here.  As a side note, please do not reference sun's JNI tutorial as I've read through it (unless you know of a specific line in there that I might have missed that deals with this problem) and haven't found my answer there.  Thanks for the help.

Currently this question is worth 200 points.  However, if somebody gives especially usefull help I'll bump it up to 300 points.  Thanks again.
Watch Question

The answer is that you only get to create the VM once.  While the docs seem to indicate that you can create as many as needed (even that this functionality is available only on windows), there is a small footnote that indicates that this functionality is not implemented and creates after the first one will fail.

You'll need to hang onto your VM instance after the first call and use it in subsequent calls.


Thank you, thank you, thank you! :)

That solved the problem I was having for the most part.  The only thing that is left is a small complaint from VBA that this is a bad dll calling convention:

__declspec(dllexport) void closeDll()

I'm just curious if this is the correct way to close out the jvm (or if I even have to do this at all).  Thanks.


Never mind...figured out what I was doing wrong.  Thanks again.  Bumped up to 300 as promised.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.