Solved

Can't GetMethodID of constructor from C++

Posted on 1997-08-17
1
2,002 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

696 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