Solved

Can't GetMethodID of constructor from C++

Posted on 1997-08-17
1
1,939 Views
Last Modified: 2011-10-03
I am trying to instantiate a new Java object from C++ using the JNI.  I've read extensively on the JNI and have compiled and run all the example code in the tutorial.

Now, I'm trying to do something more complicated and am failing miserable.  My C++ program is able to initialize VM args and load the Java VM.  Next I am attempting to execute the following code segment:

 //This statement succeeds, although the pointer returned
 //is always 0x00000002 which seems wrong to me.
 jniENV->FindClass("myClass");

 //This next statement fails (returns NULL).  I've tried
 //many variations using "myClass" and "<init>" as the
 //method name as well as several different signatures,
 //specifically the signature I get from javap -s -p myClass
 jmethodID mid = jniENV->GetMethodID(cls, "<init>", "()V");

 //This next statement I've never made it to
 jobject obj = jniENV->NewObject(cls, mid);

Also, I've tested the Java code and it works to my satisfaction.  My problem is only that I am unable to load and run it starting from a C++ application.

Any advice would be greatly appreciated.
0
Comment
Question by:jerrym
1 Comment
 

Accepted Solution

by:
Arjan092097 earned 100 total points
Comment Utility
I think I understand your problem. Probably the JNI cant find the  class 'myClass' because your classpath is not correct. There are two solutions for that:
I:  use the complete path name in the findClass function:
    cls = JNIEnv->FindClass("path/classfile")
    where path is the path where the classfile is to be found
II: set the Javaclasspath to the path where your class files are  
    to be found.

I think your GetMethodID is alright. For the default constructor the 'method name' is indeed <init> and the signature is "()V".

So I think that solves your problem and you can now create an instance of myClass from C.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now