This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Calling Java from C++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Calling Java from C++" Watch "Calling Java from C++" New topic
Author

Calling Java from C++

Si Bell
Greenhorn

Joined: Aug 16, 2001
Posts: 1
Hi,
Has anyone had much success with this? I'm trying to compile a C++ file in Visual C++ 6.0 that will access a function on a Java object. I'm using JDK 1.3.
By finding some online examples and attempting to follow them, I've got code like:
#include <jni.h>
void test()
{
JavaVM *jvm; /* denotes a Java VM */
JNIEnv *env; /* pointer to native method interface */

JDK1_1InitArgs vm_args; /* JDK 1.1 VM initialization arguments */

vm_args.version = 0x00010001; /* New in 1.1.2: VM version */
/* Get the default initialization arguments and set the class
* path */
JNI_GetDefaultJavaVMInitArgs(&vm_args);
// vm_args.classpath = ...;

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

/* invoke the Main.test method using the JNI */
jclass cls = env->FindClass("Main");
jmethodID mid = env->GetStaticMethodID(cls, "test", "(I)V");
env->CallStaticVoidMethod(cls, mid, 100);
}
However, trying to compile this gives:
error C2664: 'JNI_CreateJavaVM' : cannot convert parameter 2 from 'struct JNIEnv_ ** ' to 'void ** '
Every example I can find seems to create the JVM in the same way - has anyone any ideas how I can get round this?
Kind Regards,
Si
Corey Cole
Greenhorn

Joined: Sep 04, 2001
Posts: 5
First thing to do is to check in src.jar.
Sun and IBM both provide the c source for java.exe
so you can see how they do it.
Next, your CreateJavaVM call should look like
JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
That'll fix your compilation problem.
If you're using a Java2 VM (and I don't know why you wouldn't)
forget about JDK1_1InitArgs. Here's how I initialize my
JVM (Windows, which is why I use BOOL...):
JNIEnv *env = 0;
JavaVM *jvm = 0;
BOOL initJVM() {
#define numOptions 2
JavaVMInitArgs vm_args;
JavaVMOption options[numOptions];
options[0].optionString = "-Djava.compiler=NONE";
//options[1].optionString = "-verbose:class";
options[1].optionString = "-Xcheck:jni";
jint ret;
vm_args.version = JNI_VERSION_1_2;
vm_args.options = options;
vm_args.nOptions = numOptions;
vm_args.ignoreUnrecognized = TRUE;
ret = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (ret < 0) {
cout << "Can't create JVM! Error: " << ret << endl;
return FALSE;
}
return TRUE;
}
Beyond that, you've got my sympathies. I'm on my second day
of trying to work around an InstantiationException when I pass
raw bytes to JNI's DefineClass function...
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Helo Si!
try:
JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);
because C++ is more pickier w/ casting
------------------
Antti Barck
It Solutions Consultant -- NSD Oy
Sun Certified Programmer for the Java� 2 Platform
 
Don't get me started about those stupid light bulbs.
 
subject: Calling Java from C++
 
Similar Threads
how to invoke HttpRequest from a C code?
other languages calling java commands
Undefined Reference Trying to invoke Java from C++
JNI : undefined reference to JNI_CreateJavaVM()
Invoking JVM through C++ code