?
Solved

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

Posted on 2001-07-04
3
Medium Priority
?
425 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
stimpyjcat earned 450 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

765 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