Results 1 to 1 of 1
- 01-31-2008, 08:49 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 1
- Rep Power
- 0
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:
this produces my .class file. i then do:Java Code: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); } }
$javah - jni HelloWorld.
this produces the C header needed for java and C++ to communicate with each other.
the .h file:
thus my c++ code should mimic this and i do this here:(HelloWorld.cpp)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" { #endif /* * Class: HelloWorld * Method: displayMessage * Signature: (Ljava/lang/Double;)V */ JNIEXPORT void JNICALL Java_HelloWorld_displayMessage (JNIEnv *, jobject, jobject); #ifdef __cplusplus } #endif #endif
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?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 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 08:52 PM.
Similar Threads
-
[SOLVED] Passing parameters in Eclipse
By DonCash in forum EclipseReplies: 2Last Post: 04-08-2008, 04:46 PM -
Need help returning data from database
By dyn03 in forum JDBCReplies: 0Last Post: 03-11-2008, 04:55 PM -
Returning a string (Partly Bold)
By TampaTechGuy in forum New To JavaReplies: 3Last Post: 02-03-2008, 09:54 AM -
is synchronization on method passing local variables as parameters needed
By reddzer in forum Java ServletReplies: 0Last Post: 11-10-2007, 04:47 PM -
selectSingleNode not returning element...
By schu777 in forum XMLReplies: 4Last Post: 07-31-2007, 05:19 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks