Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

732 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