Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Under Linux, Call java from C++

Reply
Thread Tools

Under Linux, Call java from C++

 
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      10-04-2005
a new question: how to call just one java method.
In the codes that I showed at the beginning, the c++ codes called the
main() method of java codes. Now the question is, how to call the
normal(not main()) method:
I tried like this (do_it is the java static void method that I would
like to call from C++):
....
jmethodID get_main_id;
jmethodID get_do_it_id;

if(cls != NULL)
{
get_main_id =
env->GetStaticMethodID(cls,"main","([Ljava/lang/StringV");
get_do_it_id =
env->GetStaticMethodID(cls,"do_it","([Ljava/lang/StringV");

if(get_main_id != NULL )
{
jclass string = env->FindClass("java/lang/String");
jobjectArray args = env->NewObjectArray(0,string, NULL);
env->CallStaticVoidMethod(cls, get_main_id, args);
}

if(get_do_it_id!= NULL )
{
jclass string = env->FindClass("java/lang/String");
jobjectArray args = env->NewObjectArray(0,string, NULL);
env->CallStaticVoidMethod(cls, get_do_it_id, args);
}
}
....

These codes can be compiled and linked. But when I run it, there is
following error:
__________________________________________________ ___________________________________
Unexpected Signal : 11 occurred at PC=0x402B420F
Function=(null)+0x402B420F
Library=/usr/lib/jvm/java-1.4.2-sun-1.4.2.05/jre/lib/i386/client/libjvm.so

NOTE: We are unable to locate the function name symbol for the error
just occurred. Please refer to release documentation for possible
reason and solutions.
Current Java thread:
....
__________________________________________________ _____________________________

I guess that I used the wrong method in JNIEnv to call Java method?

 
Reply With Quote
 
 
 
 
tony_lincoln@yahoo.com
Guest
Posts: n/a
 
      10-04-2005
If I put the same parameter list in do_it() as in main(), that is,
String[] args, it can work.
But if the argument list in do_it() is not same as in main(), what is
the correct code?

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      10-04-2005
On 4 Oct 2005 02:22:19 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> If I put the same parameter list in do_it() as in main(), that is,
> String[] args, it can work.
> But if the argument list in do_it() is not same as in main(), what is
> the correct code?


Regardless of what arguments do_it() needs (i.e. either the same as
main() or different), you need to pass the correct signature to
GetStaticMethodId(), and you need to create the correct type of
argument for the call. Be more specific if this doesn't answer the
question.

/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
 
      10-04-2005
On 4 Oct 2005 02:11:57 -0700, (E-Mail Removed) wrote:
> Unexpected Signal : 11 occurred at PC=0x402B420F


If either of the calls to GetStaticMethodID() fails (leaving NULL in
the correspoding id variable) then you are not free to continue
calling JNI functions until you clear the pending exception.

/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
 
      10-04-2005
(E-Mail Removed) wrote:

> But in my library headers there is only:
> #define JNI_VERSION_1_1 0x00010001
> #define JNI_VERSION_1_2 0x00010002
> There is no definition for JNI_VERSION_1_4 at all. As my current java
> version is 1.4, I guess that I should add this new definition into the
> header.


No definitely not, it means that you are looking at a version of jni.h that
does not belong to your jdk1.4.2 installation. Find the right jni.h (it should
be in <jdk>/include/jni.h) and include that.

BTW, the numbers correspond to the version of the JNI spec, and do not relate
to the version number of the JDK in use. For instance, in JDK1.5.0, the most
recent value of JNI version is still JNI_VERSION_1_4.

-- chris


 
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
Tomcat 5.5+ On a Mac, Under Eclipse, Under OS X Edward V. Berard Java 4 04-04-2006 05:14 AM
Why does Rpy/R-plot work under PythonWin, but not under commandline/IDLE? Bo Peng Python 1 01-09-2006 07:05 AM
help : my jar file is not running under linux terminal , but it runs under JbuilderX ide bronby Java 1 07-15-2005 07:23 AM
[newbie]How to install python under DOS and is there any Wxpython can be installed under dos? john san Python 19 02-18-2005 12:05 PM
Java application developped under Linux running ridiculously slow under Windows hshdude Java 12 11-04-2004 05:49 PM



Advertisments