Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

830 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