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:
class HelloWorld
{
public native void displayMessage(Double x);
static
{
System.loadLibrary("HelloWorldImp");
}
public static void main(String[] args)
{
Double x = 0.0;
HelloWorld hello = new HelloWorld();
hello.displayMessage(x);
System.out.println(x);
}
}
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:
/* 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" {
#endif
/*
* Class: HelloWorld
* Method: displayMessage
* Signature: (Ljava/lang/Double;)V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayMessage
(JNIEnv *, jobject, jobject);
#ifdef __cplusplus
}
#endif
#endif
thus my c++ code should mimic this and i do this here

HelloWorld.cpp)
#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.
