Hi. I have a problem which from what I've been seeing I'd need to use JNI. I took a look and sun's JNI page and read some stuff from the site. Before I get too deep in there, I'd like to ask a question so others can tell me if it's worth to keep on reading JNI stuff (maybe it won't solve my problem at all). There's this one system all developed in ANSI C/C++. I don't have access to this system. Right now, I need to start working on a subapplication of this system (actually, I'm going to provide libraries to the other system). I'm planning on writting everything in Java. I can see that JNI's goal is exactly to provide the interface Java/C++ I need. The point here is that I will not be working on the C++ side, only on the Java side. This means, that I cannot count that the C++ developers (from the other system) would like to write "special" code to access my Java library (they want to keep up with their usua C++ code). So, I need to provide a Java library with a "C++ interface" (wich can be accessed by ANSI C++ code). Is this possible with JNI??? Thanks.
This means, that I cannot count that the C++ developers (from the other system) would like to write "special" code to access my Java library (they want to keep up with their usua C++ code). So, I need to provide a Java library with a "C++ interface" (wich can be accessed by ANSI C++ code). Is this possible with JNI???
Unless there's some secret magic I missed out on, *someone* will have to write some "special" code in C++ to invoke the jvm and access your classes and methods. If they aren't going to do it, you will have to. If you are not a C++ developer, I suggest that you recruit one of them to do it with you. Hey, you could tell everyone else the two of you are doing Extreme Programming!
That said, I found jni to be more intuitive than originally feared and working through a tutorial told me all I needed to know. This one:
I've also worked a lot with c++. What it I want is simple (I thinks that's why Ernest replied me with a simple yes). I want to buil a library in Java and make a "C++ interface" for this (I wouldn't call it a wrapper, but the idea is close). Then distribute it under a DLL (or something else) for example. Thanks for the tutorial tips btw. [ May 25, 2005: Message edited by: Leandro Melo ]
Joined: Jun 07, 2003
Here's what I might do. I've never created a Java library to be used by others in C++, so I was content w/o the wrappers and just using the "special" jni methods from jni.h/libjava.so (or, in your case, javai.lib). But your coworkers don't want "special", so:
1 - a static isJVMStarted checked on entry to any method. If false, then call some method that starts the JVM (with JNI_GetDefaultJavaVMInitArgs() and JNI_CreateJavaVM()) and sets it to true.
2 - c++ methods that are wrappers (heh - you said it first!) around FindClass(), GetXXXMethodID(), NewXXX(), and CallXXXMethod() as needed. These could (should?) be in classes that map to your Java classes.
3 - some way to shut 'er down and make sure some jvm isn't left hanging by calling DestroyJavaVM().
** - I could make an argument against 1, and for a startJVM() and stopJVM() method that they have to call before using your library and after they're finished, respectively. C++ folks (as you remember) are used to managing resources like this, so it shouldn't throw them a curve.
(Obviously, your problem was more interesting to me than what I'm *supposed* to be working on!)
Mark [ May 25, 2005: Message edited by: Mark Wuest ]