Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to use JNI to start JVM in builder C++ application


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Aug 28, 2014 9:40 AM Last Post By: Remy Lebeau (Te...
SALHI Med

Posts: 2
Registered: 7/17/14
How to use JNI to start JVM in builder C++ application  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 28, 2014 5:26 AM
So i need to use jni to call java method. i started with this code :
JNIEnv *env1;
JavaVM**  jvm1;
JavaVMInitArgs vm_args1;
JavaVMOption options1[1];
options1[0].optionString = "-Djava.class.path=D:\\Java Src\\TestStruct"; //Path to the java source code
vm_args1.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
vm_args1.nOptions = 1;
vm_args1.options = options1;
vm_args1.ignoreUnrecognized = 0;
 
int reAt = JNI_CreateJavaVM(jvm1, (void**)&env1, &vm_args1);
if(reAt < 0)
    Label1->Caption = "failed";
else
    Label1->Caption = "Success";

I convert jvm.lib to OMF lib for to use in my builder C++ Application :
COFF2OMF jvm.lib jvm2.lib

I added jvm2.lib to the library path to fixe link error about JNI_CreateJavaVM function.

Now my application compile without error.

But it crash when it call JNI_CreateJavaVM function.

I added the jvm.dll near my .exe

What is the pb in there steps??

How can fixe it?

Thx
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to use JNI to start JVM in builder C++ application  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 28, 2014 9:40 AM   in response to: SALHI Med in response to: SALHI Med
SALHI wrote:

I convert jvm.lib to OMF lib for to use in my builder C++ Application :

COFF2OMF jvm.lib jvm2.lib

Don't do that. Use IMPLIB instead to create a new .lib file from jvm.dll:

IMPLIB jvm2.lib jvm.dll


But it crash when it call JNI_CreateJavaVM function.

You are passing the wrong value in the first parameter to JNI_CreateJavaVM(),
so it is trying to write data to an invalid memory address. Try this instead:

JNIEnv *env1;
JavaVM* jvm1; // <-- note only one asterik
JavaVMInitArgs vm_args1;
JavaVMOption options1[1];
options1[0].optionString = "-Djava.class.path=D:\\Java Src\\TestStruct"; 
//Path to the java source code
vm_args1.version = JNI_VERSION_1_6; //JDK version. This indicates version 1.6
vm_args1.nOptions = 1;
vm_args1.options = options1;
vm_args1.ignoreUnrecognized = false;
int reAt = JNI_CreateJavaVM(&jvm1, (void**)&env1, &vm_args1); // <-- note 
the ampersand on the first parameter
if (reAt < 0)
    Label1->Caption = "failed";
else
    Label1->Caption = "Success";


Look at the example in the official JVM documentation:

http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02