?
Solved

Can't GetMethodID of constructor from C++

Posted on 1997-08-17
1
Medium Priority
?
2,010 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 200 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

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 first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month12 days, 3 hours left to enroll

752 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