Results 1 to 1 of 1
  1. #1
    java_to_c is offline Member
    Join Date
    Jan 2008
    Rep Power

    Default JNI - passing and returning parameters by value

    EDIT - this should be in the advanced forum - i dunno how i ended up in this section - SORRY! can this be moved to the appropriate place?

    Hi all. I am by nature a C++ programmer and have dabbled in Java a little bit so i hope this is ok being in the advanced forum, especially since i cannot get anything definitive online about that.

    i am writing a .java application(web service or otherwise) that needs to have access to a C++ function through JNI. i know that primitives are passed always by value but how would i go about changing the value of the primitive inside the C++ function. can anyone give me some code to show me? anyway as a sidenote i am using g++ on Linux(Ubuntu).

    i already have the .java file compiling. this code is:

    Java Code:
    class HelloWorld
     public native void displayMessage(Double x);
            public static void main(String[] args)
                    Double x = 0.0;
                    HelloWorld hello = new HelloWorld();
    this produces my .class file. i then do:

    $javah - jni HelloWorld.

    this produces the C header needed for java and C++ to communicate with each other.

    the .h file:
    Java Code:
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class HelloWorld */
    #ifndef _Included_HelloWorld
    #define _Included_HelloWorld
    #ifdef __cplusplus
    extern "C" {
     * Class:     HelloWorld
     * Method:    displayMessage
     * Signature: (Ljava/lang/Double;)V
    JNIEXPORT void JNICALL Java_HelloWorld_displayMessage
      (JNIEnv *, jobject, jobject);
    #ifdef __cplusplus
    thus my c++ code should mimic this and i do this here:(HelloWorld.cpp)
    Java Code:
    #include <iostream>
    #include "HelloWorld.h"
    using namespace std;
    JNIEXPORT void JNICALL Java_HelloWorld_displayMessage(JNIEnv *env, jobject obj, jobject x)
      //i need to change the value of x, thus changing what i passed in from the java code. 
      cout << "hello world!, this is a test" << endl;
      //return (jfloat)3.0;
    as you can see from my comments i have already successfully returned a single value with no problem and i can print out statements just fine. Now i need to be able to change the value of the 'jobject x'. i know i could make a single double array or that i could just simply return one value but what if the function passes in 2 arrays and 3 primitives?

    As i understand it the object of x is passed in not the value which is hard to think about coming from C++ but i think i have that right. so how do i use jobject to change the value inside the object itself. what function member or members must i use? i would appreciate any and all help. thanks!!!

    p.s. i know this is a whopper for my first post. :rolleyes:
    Last edited by java_to_c; 01-31-2008 at 09:52 PM.

Similar Threads

  1. [SOLVED] Passing parameters in Eclipse
    By DonCash in forum Eclipse
    Replies: 2
    Last Post: 04-08-2008, 05:46 PM
  2. Replies: 0
    Last Post: 03-11-2008, 05:55 PM
  3. Returning a string (Partly Bold)
    By TampaTechGuy in forum New To Java
    Replies: 3
    Last Post: 02-03-2008, 10:54 AM
  4. Replies: 0
    Last Post: 11-10-2007, 05:47 PM
  5. Replies: 4
    Last Post: 07-31-2007, 06:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts