Question about C++ class instantiation and JNI

From what I read here

http://mindprod.com/jgloss/jni.html

and here

http://www.javaworld.com/javaworld/javatips/jw-javatip17.html

it seems that you cannot instantiate (call a constructor) a C++ class from a Java class. You need to have a C++ class that does it for you, and then the Java class maintains the pointer.

This article referenced above was written about JDK 1.0 and I cannot find any newer articles on the topic. Is this info still accurate? Is there anywhere I can find a more recent article that talks about this topic for JDK 1.6? Most of the JNI articles I've found talk straight C.

Secondly, I'm not sure if I am going to attempt to write my own JNI or use a toolkit. what is the best toolkit? I looked at SWIG but having to maintain all these extra input files seems like a waste of time to me.

Thanks!

[869 byte] By [squiddoga] at [2007-10-3 6:07:28]
# 1

> From what I read here

> http://mindprod.com/jgloss/jni.html

> and here

> http://www.javaworld.com/javaworld/javatips/jw-javatip

> 17.html

>

> it seems that you cannot instantiate (call a

> constructor) a C++ class from a Java class. You need

> to have a C++ class that does it for you, and then

> the Java class maintains the pointer.

Yep.

>

> This article referenced above was written about JDK

> 1.0 and I cannot find any newer articles on the

> topic. Is this info still accurate? Is there

> anywhere I can find a more recent article that talks

> about this topic for JDK 1.6? Most of the JNI

> articles I've found talk straight C.

JNI hasn't changed much since 1.2. You still have to store the pointer on the Java side. I do it like this:

abstract class CppPeer {

protected long pointer;

protected CppPeer(long ptr) {

pointer = ptr;

}

public long getPtr() {

return pointer;

}

}

This is simply a placeholder for the pointer which can be returned from

the C++ code. I'm in the process of porting a C++/COM interface with

100 objects and about 2000 functions to Java and I'm using this

approach. Each C++ object has a corresponding Java object, and each C++

method has a corresponding Java method, and a native method to act as a

go-between:

public class WindowHandle extends CppPeer {

public WindowHandle(long ptr) {

super(ptr);

}

}

Anytime you need to pass the information back to the C++ side, you

simply pass the 'pointer' value:

public class HandleUser {

public void useHandle(WindowHandle wh) {

useHandle (wh.getPtr());

}

private native void useHandle(long ptr);

}

On the native side you cast to the appropriate type:

JNIEXPORT void JNICALL Java_HandleUser_useHandle(JNIEnv *env, jobject

obj,jlong ptr) {

WindowHandle *handle = reinterpret_cast<WindowHandle*>(ptr);

//do stuff with handle pointer

}

>

> Secondly, I'm not sure if I am going to attempt to

> write my own JNI or use a toolkit. what is the best

> toolkit? I looked at SWIG but having to maintain all

> these extra input files seems like a waste of time to

> me.

I haven't used any of the various toolkits. Usually the C/C++ APIs I've 'ported' are so un-object oriented that if I use a toolkit I would have to make a second layer of Java just to make the API work in a way that makes sense to Java folks.

Jim S.

Niceguy1a at 2007-7-15 0:50:32 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Hi

I am new to JNI and Foxpro, I have a DLL created from foxpro. Created as below :

DEFINE CLASS person AS CUSTOM OLEPUBLIC PROCEDURE GetName(d) SET EXCLUSIVE off USE

...

..

Is it possible you instantiate class 'person' and call GetName() method in it using JNI ?

4400666a at 2007-7-15 0:50:32 > top of Java-index,Java HotSpot Virtual Machine,Specifications...