Solved

Can't GetMethodID of constructor from C++

Posted on 1997-08-17
1
1,969 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
ID: 1225222
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 92
arguments to jar 5 26
runtime exception 2 28
JAVA API design with micro service cloud in mind 1 18
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

803 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