Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Passing strings to constructor C++ to Java

843829Jan 29 2004 — edited Jan 29 2004
I'm trying to instantiate a Java class from my C++ code. If I try to call my constructor that takes 2 strings, the constructor is located and gets called, but when I try to access the parameters, an exception is thrown. If I call another constructor that takes, say an int, it works fine. Here is a code snipet: (note, error checking removed)
  jmethodID mid = env->GetMethodID(cls, "<init>", "(Ljava/lang/String;Ljava/lang/String;)V"); 
  jobjectArray argsArray = (jobjectArray)env->NewObjectArray(2, 
                            env->FindClass("java/lang/String"), 
                            env->NewStringUTF("")); 

  const char *names[2] = { "first", "second" }; 
  for( int i=0; i<2; i++) 
    env->SetObjectArrayElement(argsArray, i, env->NewStringUTF(names)); 

obj = env->NewObject(cls, mid, argsArray);

So, in my constructor, I print out that I got there, but when I try to print out the first arg, it throws an exception.
Like I said, using primatives as args works. I also tried passing an object array of on item, same problem.
Funny thing, if I do something similar and try calling my "main" with an array of strings, it works.

Can anyone please help?
Thanks,
Alfredo
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 26 2004
Added on Jan 29 2004
1 comment
128 views