File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

problem of C++ Call java with JNI

Zhao Wenqiang

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 ':'
#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;
/* Append USER_CLASSPATH to the end of default system class path */
sprintf(classpath, "%s%c%s",
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");
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");

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

return S_OK;
STDMETHODIMP CObject1: ut_helloword(char * newVal)
// TODO: 在此添加实现代码
return S_OK;
Valentin Crettaz
Gold Digger

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

[Blog] [Blogroll] [My Reviews] My Linked In
Mapraputa Is
Leverager of our synergies

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

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.

SCJP 1.4
Mando Mu�oz

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");


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");
I agree. Here's the link:
subject: problem of C++ Call java with JNI
It's not a secret anymore!