wood burning stoves 2.0*
The moose likes Java in General and the fly likes Java/C++ and JNI question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java/C++ and JNI question" Watch "Java/C++ and JNI question" New topic
Author

Java/C++ and JNI question

Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
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.


Leandro Melo
SCJP 1.4, SCWCD 1.4
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yes.


[Jess in Action][AskingGoodQuestions]
Mark Wuest
Ranch Hand

Joined: Jun 07, 2003
Posts: 88
Originally posted by Leandro Melo:

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:

Invoking the Java Virtual Machine

Has most of what you need to invoke it along with one simple example of accessing a static java method. The rest of what you need to know (accessing other members) is in this tutorial:

Interacting with Java from the Native Side

'hope this helps!

Mark
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
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 ]
Mark Wuest
Ranch Hand

Joined: Jun 07, 2003
Posts: 88
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 ]
 
Don't get me started about those stupid light bulbs.
 
subject: Java/C++ and JNI question
 
Similar Threads
JNI - how do I access a C strucure from Java?
Suggestions for providing service interfaces to POJO client applications
Call a local shared library from browser?
GWT in Action:Qurie about Java Script
what is JNI and what are the advantages of JNI?