aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes problem of C++ Call java with JNI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "problem of C++ Call java with JNI" Watch "problem of C++ Call java with JNI" New topic
Author

problem of C++ Call java with JNI

Zhao Wenqiang
Greenhorn

Joined: Jul 18, 2002
Posts: 1
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
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Moving this to Distributed Java...


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
But it's not really "distributed Java" question. Moving to "Other Java APIs"..
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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.


Rob
SCJP 1.4
Mando Mu�oz
Greenhorn

Joined: Jun 01, 2004
Posts: 2
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");
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem of C++ Call java with JNI