JNI and interface classes

O

Oyvind Idland

I am trying to get an object-handle from a Java interface,
without luck. It works well for "normal" classes, but now
I seem to be out of luck.

The Java code implementation of the interface looks like
this:


package myClasses;

public interface someInterface{
public void someMethod(String str1, String str2);
}



The following lines works well (C++):

cls = env->FindClass("myClasses/someInterface");
mid = env->GetMethodID(cls, "someMethod", "(Ljava/lang/String;Ljava/lang/String;)V");

Since there is no constructor here, I try to create the object-handle
like this:

obj = env->AllocObject(cls);

...which fails. I guess I my thinking may be wrong here.


Any suggestions is welcome :)


Oyvind
 
C

Chris Uppal

Oyvind said:
I am trying to get an object-handle from a Java interface,
without luck. It works well for "normal" classes, but now
I seem to be out of luck. [...]
..which fails. I guess I my thinking may be wrong here.

It is. You /can't/ create instances of interfaces -- the concept has no
meaning.

I'm not trying to be rude, but I really do think you need to go back to basics
and find out about Java interfaces, what they mean, and what they are used for.

-- chris
 
T

Thomas Fritsch

Oyvind said:
I am trying to get an object-handle from a Java interface,
without luck. It works well for "normal" classes, but now
I seem to be out of luck.

The Java code implementation of the interface looks like
this:

package myClasses;

public interface someInterface{
public void someMethod(String str1, String str2);
}

The following lines works well (C++):

cls = env->FindClass("myClasses/someInterface");
mid = env->GetMethodID(cls, "someMethod",
"(Ljava/lang/String;Ljava/lang/String;)V");

Since there is no constructor here, I try to create the object-handle
like this:
obj = env->AllocObject(cls);

..which fails. I guess I my thinking may be wrong here.
It fails for the same reason why the Java code
new someInterface()
would fail: because an interface has no contructor.
Any suggestions is welcome :)
You definitely need a concrete class implementing the interface.
public class SomeClass implements someInterface {
public SomeClass() {
// initialize something
}
public void someMethod(String str1, String str2) {
// do something
}
}
Only then you are able to create a new object of this class, both via
Java or via JNI.
 
O

Oyvind Idland

Thomas said:
You definitely need a concrete class implementing the interface.
public class SomeClass implements someInterface {
public SomeClass() {
// initialize something
}
public void someMethod(String str1, String str2) {
// do something
}
}
Only then you are able to create a new object of this class, both via
Java or via JNI.

Thanks alot, this is the answer I was looking for.

Oyvind
 
R

Roedy Green

obj = env->AllocObject(cls);

..which fails. I guess I my thinking may be wrong here.

For a moment imagine there were no C++ involved. You can't instantiate
an Interface. You need a factory or a constructor to create an
instance of some concrete class that implements the interface.

The same is true when you add the complication of JNI.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top