Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > call java from c++: where to put class

Reply
Thread Tools

call java from c++: where to put class

 
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      08-14-2005
Dear friends,

I am using JNI to call java from C++. I use j2sdk1.4.1 and Visual C++
6.
Parameter setting in Visual C++ 6:

In Toolsí˙Options->Include Files:
1. C:\j2sdk1.4.1_02\include
2. C:\j2sdk1.4.1_02\include\win32

In Tools->Options->Library Files:
C:\j2sdk1.4.1_02\lib

set Path = C:\j2sdk1.4.1_02\jre\bin\client

All of these settings are correct. When I compiled the invoke.cpp, no
errors. But when I execute it, DOS concole window popped up and one
sentence appeared:
"Sorry, I can't find the class. Press any key to continue"

The "Sorry, I can't find the class" came from my invoke.cpp. This
means, this cpp can not find my java class, even when I put the java
class "Demo" into the same subdir as invoke.cpp. Where should I put the
Demo class?

Any hints? Thanks a lot!

The related codes(Demo.java, invoke.cpp) are enclosed here:

//Demo.java
************************************************
package jni.test;

public class Demo {

public static int COUNT = 8;
public String msg;
private int[] counts;
public Demo() {
this("╚▒╩í╣╣ďý║»╩ř");
}

public Demo(String msg) {
System.out.println("<init>:" + msg);
this.msg = msg;
this.counts = null;
}

public String getMessage() {
return msg;
}

public int[] getCounts(){
return counts;
}

public void setCounts(int[] counts){
this.counts = counts;
}

public void throwExcp()
throws IllegalAccessException{
throw new IllegalAccessException
("exception occur.");
}

}// end class Demo.

************************************************** ***************+




The c++ code is the following:
// invoke.cpp
************************************************** ******************
/*for C++,debugged with Visual C++ 6.0*/

#ifndef __cplusplus
#define __cplusplus
#endif

#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#pragma comment (lib,"C:\\j2sdk1.4.1_02\\lib\\jvm.lib")

int JStringToChar(JNIEnv *env, jstring str, LPTSTR desc, int desc_len)
{
int len = 0;
if(desc==NULL||str==NULL)
return -1;

wchar_t *w_buffer = new wchar_t[1024];
ZeroMemory(w_buffer,1024*sizeof(wchar_t));

wcscpy(w_buffer,env->GetStringChars(str,0));

env->ReleaseStringChars(str,w_buffer);
ZeroMemory(desc,desc_len);

len =
WideCharToMultiByte(CP_ACP,0,w_buffer,1024,desc,de sc_len,NULL,NULL);

if(len>0 && len<desc_len) desc[len]=0;
delete[] w_buffer;
return strlen(desc);
}

jstring NewJString(JNIEnv* env,LPTSTR str)
{
if(!env || !str) return 0;
int slen = strlen(str);
jchar* buffer = new jchar[slen];
int len = MultiByteToWideChar(CP_ACP,0,str,strlen(str),buffe r,slen);
if(len>0 && len < slen) buffer[len]=0;
jstring js = env->NewString(buffer,len);
delete [] buffer;
return js;
}


void main() {

JavaVM *jvm;
JNIEnv *env;

JavaVMInitArgs vm_args;
JavaVMOption options[3];

options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = "-Djava.classpath=.";
options[2].optionString = "";

vm_args.version = JNI_VERSION_1_4;
vm_args.nOptions = 3;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;

jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (res < 0) {
fprintf(stderr, "Can't create Java VM\n");
exit(1);
};
jclass cls = env->FindClass("Demo");

if (cls == 0) printf("Sorry, I can't find the class");

jmethodID get_main_id =
env->GetStaticMethodID(cls,"main","([Ljava/lang/StringV");
jclass string = env->FindClass("java/lang/String");
jobjectArray args = env->NewObjectArray(0,string, NULL);
env->CallStaticVoidMethod(cls,get_main_id,args);

jvm->DestroyJavaVM();
fprintf(stdout, "Java VM destory\n");
}//end main.

************************************************** *********************
//end of invoke.cpp

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      08-15-2005
On 14 Aug 2005 13:36:57 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> package jni.test;
> public class Demo {


[...]

> jclass cls = env->FindClass("Demo");
> if (cls == 0) printf("Sorry, I can't find the class");


You spelled the name of the class wrong. Try "jni.test.Demo" or remove
the package declaration. Note too that the class should be in
../jni/test/Demo.class if your classpath points to "." as it does in
the code you've posted.

> options[0].optionString = "-Djava.compiler=NONE";
> options[1].optionString = "-Djava.classpath=.";
> options[2].optionString = "";


> vm_args.nOptions = 3;


I see only two meaningful arguments here. Why the empty string?

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      08-15-2005
On 15 Aug 2005 09:00:30 +0200, Gordon Beaton wrote:
> You spelled the name of the class wrong. Try "jni.test.Demo"


Sorry, that should have been: "jni/test/Demo" instead.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      08-15-2005
Gordon Beaton wrote:
> On 14 Aug 2005 13:36:57 -0700, (E-Mail Removed) wrote:
> > jclass cls = env->FindClass("Demo");

> [...]
> You spelled the name of the class wrong. Try "jni.test.Demo"


Should be:

"jni/test/Demo"

JNI names....

--- chris


 
Reply With Quote
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      08-15-2005
Thanks a lot. I removed "package Jni.test;" in the Demo.java and
compile it again, and it works.
( options[2].optionString = "-verbose: jni" )
But new question comes:
When the c++ codes run, a DOS console poped up, and the words like this
appears:

__________________________________________________ _________________________________
....
[Dynamic-linking native method java.lang.Thread start0 ... JNI]
....
[Dynamic-linking native method java.lang.classLoader defineClass1 ...
JNI]
---------------------------------------------------------------------------------------------------------------------

Then a new Windows from Win2000 popped up and showed:
"Test3_14Aug.exe has encountered a problem and needs to close.We are
sorry from the inconvenience.
Please tell Microsoft about this problem.
We have created an error report that you can send to us...."

Then the program was forced to stop.
Is this the problem of win2000, or the bug in invoke.cpp/Demo.java?
Thanks in advance.
tony

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      08-15-2005
On 15 Aug 2005 06:39:55 -0700, (E-Mail Removed) wrote:
> "Test3_14Aug.exe has encountered a problem and needs to close.We are
> sorry from the inconvenience.
> Please tell Microsoft about this problem.
> We have created an error report that you can send to us...."
>
> Then the program was forced to stop.


In the code you've posted, Demo doesn't have a main() method and you
don't check the return value from GetStaticMethodID() before
attempting to invoke it.

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      08-16-2005
Thank you very much. You are right. Once I put main() method in it, it
works. Thanks.
Tony

 
Reply With Quote
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      08-16-2005
But the new problem about package comes:

In my Java source code:
************************************************** ******
import com.tony.package1.classA;
....
public static void main(String[] args) {
System.out.println("This is a test");
String [] str = new classA.method1();
}
....
************************************************** *******

When I invoke this java class from C++ codes in Visual C++, the "This
is a test" can be shown in DOS shell, then it shows:
" An unexpected error has been detected by HotSpot Virtual Machine:
Internal Error (57437914380392184392743120947000FF),
pid = 3288, tid = 3292.
...."

So I thought this is the problem of path setting. I set the path as
E:\codes_Tony. This dir is where I put my package com.tony.package1.*.
But the same error apprears.
I still guess that this is the problem of path setting. But how?
Thanks a lot.
Tony

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      08-16-2005
On 16 Aug 2005 07:36:40 -0700, (E-Mail Removed) wrote:
> I set the path as
> E:\codes_Tony. This dir is where I put my package com.tony.package1.*.
> But the same error apprears.


If E:\codes_Tony is in your classpath, then com.tony.package1.ClassA
should be in E:\codes_Tony\com\tony\package1\ClassA.class.

See also:
http://www.yoda.arachsys.com/java/packages.html

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      08-16-2005
Exactly. E:\codes_Tony is in my classpath, and com.tony.package1.ClassA
is in E:\codes_Tony\com\tony\package 1\ClassA.class.
But why the error appears:
" An unexpected error has been detected by HotSpot Virtual Machine:
Internal Error (57437914380392184392743120947 000FF),
pid = 3288, tid = 3292.
..."
To do with this error, any hints?
Thanks in advance.
Tony

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: c++ class design: where to put debug purpose utility class? Phlip C++ 36 07-02-2008 02:30 PM
Re: c++ class design: where to put debug purpose utility class? Phlip C++ 2 07-01-2008 02:49 AM
class design: where to put debug purpose utility class? 1230987za@gmail.com C++ 8 06-28-2008 05:14 AM
why does the following with Queue, q.put('\x02', True) not put itin the queue? Gabriel Rossetti Python 3 04-25-2008 03:41 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments