Solved

JNI - Use c++ objects in java ???

Posted on 2001-07-04
3
378 Views
Last Modified: 2011-10-03

Hi,

i want to call a c++ - object from a java application
using JNI.
Unfortunately i haven't found really good documentation.

Does anybody have source code examples ???

My c++ class looks like this:
-----------------------------------
class License
{
 private:
   
    String license_key; // License key
    int max_user;       // Maximal user
    String client;      // Name of client
    String date;        // Date of license creation
    String expire;      // Date of expiration
    bool valid;         // Key is valid ?
   
 public:
   
    /* Constructor
     */
    License();
    License(String clientName, int maxUser,
            String expireDate);
    ~License();

    /** Getter and setter methods
     */
     String getKey();
     .....

The java - class now has to get a License instance
and call some methods


PLEASE any IDEA and source code examples ???


Many thanks
katjas
0
Comment
Question by:katjas
3 Comments
 
LVL 3

Accepted Solution

by:
stimpyjcat earned 150 total points
ID: 6256876
It's rather tedious to map a java object onto a C++ object through JNI, since JNI is set up to be callable from C, which doesn't support objects.

It's best to simplify your interface so that you only have a few operations to map out.

Otherwise, if you *really* want to map one-to-one, the java class will need to have a native method which allocates the underlying C++ object and stores it in the java object, e.g.

public class License {
     private long objectPointer;
     protected native void createLicense();
}

which you then need to implement.  Use javah to generate a header file describing all the native functions (C interface) you need to implement.

The functions will look something like this:
JNIEXPORT
Java_your_package_FunctionName(JNIEnv* env, [args...]);

The JNIEnv is an object you can use to get and set things in the java VM from C++ code.  the args will be Java types, which have operators to convert to/from C code basic types.

You'd create your C++ object and store it in a private field of the java object.  Then in each of the native get/set functions you implement, you retrieve the C++ object, perform the operations, and convert the results into java objects.  There are sufficient examples on the Sun JNI tutorial site.
0
 
LVL 1

Expert Comment

by:viswa081400
ID: 6257945
0
 
LVL 3

Expert Comment

by:exorcist
ID: 6265236
Hi,

try following procuct that builds a bridge between java and c++ to use c++ objects like java objects. Thats what you wanted, right?

http://alphaworks.ibm.com/tech/j2c++
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now