Solved

Can't GetMethodID of constructor from C++

Posted on 1997-08-17
1
1,989 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 74
spring jars download 1 54
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 65
Java Eclipse Loop 3 31
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

730 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