Forums Register Login

problem of C++ Call java with JNI

+Pie Number of slices to send: Send
I write a java class:
/***********************/
package Debug;
public class Client
{
public static void main(String[] args)
{
System.out.println("An aaaaaaaaaaa");
}
}
/************************/
I want to call it from Visual c++ with JNI.
for the "Package" problem, I can not call this
class. If i delete the line of "package Debug;",
i can call this class.
I Define USER_CLASSPATH "C:\\OpenORB-1.1.0\\zwq_tmp\\vc6++\\COMtest\\Debug\\" or define "C:\\OpenORB-1.1.0\\zwq_tmp\\vc6++\\COMtest\\" , they are both refuse to work.
Please help me. Thank you very much.
// Object1.cpp : CObject1
#include "stdafx.h"
#include "Object1.h"
#include <jni.h>
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else /* UNIX */
#define PATH_SEPARATOR ':'
#endif
#define USER_CLASSPATH "C:\\OpenORB-1.1.0\\zwq_tmp\\vc6++\\COMtest\\Debug\\" /* where Prog.class is */
//#define USER_CLASSPATH "." /* where Prog.class is */
// CObject1

STDMETHODIMP CObject1::get_helloword(char ** pVal)
{
// TODO: 在此添加实现代码
*pVal=(char *)malloc(30);
//*pVal="Zhao Wenqiang Hello Word";

JNIEnv *env;
// void **env;
JavaVM *jvm;
JDK1_1InitArgs vm_args;
jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
char classpath[1024];

/* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */
vm_args.version = 0x00010001;
JNI_GetDefaultJavaVMInitArgs(&vm_args);
/* Append USER_CLASSPATH to the end of default system class path */
sprintf(classpath, "%s%c%s",
vm_args.classpath, PATH_SEPARATOR, USER_CLASSPATH);
vm_args.classpath = classpath;
/* Create the Java VM */
res = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
}
fprintf(stderr, vm_args.classpath);
//cls = env->FindClass("zwq_module.Client");
cls = env->FindClass("Client");
if (cls == 0) {
fprintf(stderr, "\nCan't find Client class\n");
exit(1);
}

mid = env->GetStaticMethodID(cls, "Hello", "([Ljava/lang/String V");
if (mid == 0) {
fprintf(stderr, "Can't find Prog.main\n");
exit(1);
}
jstr = env->NewStringUTF(" from C!");
if (jstr == 0) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
args = env->NewObjectArray(1,
env->FindClass("java/lang/String"), jstr);
if (args == 0) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
// *HelloWordString=env->CallObjectMethod(cls, mid, args);
env->CallStaticVoidMethod(cls, mid, args);
jvm->DestroyJavaVM();

return S_OK;
}
STDMETHODIMP CObject1: ut_helloword(char * newVal)
{
// TODO: 在此添加实现代码
return S_OK;
}
zhaowq@sina.com
+Pie Number of slices to send: Send
Moving this to Distributed Java...
+Pie Number of slices to send: Send
But it's not really "distributed Java" question. Moving to "Other Java APIs"..
+Pie Number of slices to send: Send
Well I don't have any experience with JNI but I'll take a shot at this one anyway...


cls = env->FindClass("Client");


My gut feeling is that this should really be FindClass("Debug.Client"), since your Client class is in the Debug package. I don't know how JNI does things, but I do know that the java API refers to classes with their fully-qualified names. Even when you are using the simple class name like "Client" within a method, the package hierarchy is being implicitly prepended for you.
Also, as a matter of style, don't name your package "Debug" with a capital "D", use "debug" instead. Package names start with a lower-case letter in java.
+Pie Number of slices to send: Send
The problem is that JNI uses a lot of special characters to understand what object or class you are refering.

instead of use

cls = env->FindClass("Client");

use

cls = env->FindClass("Debug/Client");

"/" is the special character to diferentiate between packages, and you must use the complete name of a class to find it, like

cls = env->FindClass("java/util/Date");

is different than

cls = env->FindClass("java/sql/Date");
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3137 times.
Similar Threads
JNI : Creating a JVM
Undefined reference in JNI
JNI : undefined reference to JNI_CreateJavaVM()
Undefined reference in JNI
Invoking JVM through C++ code
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 22:58:06.