Solved

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

Posted on 2001-07-04
3
399 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

810 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