Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-31-2008, 09:49 PM
Member
 
Join Date: Jan 2008
Posts: 1
java_to_c is on a distinguished road
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:

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); } }
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:
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
thus my c++ code should mimic this and i do this hereHelloWorld.cpp)
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.

Last edited by java_to_c : 01-31-2008 at 09:52 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing parameters in Eclipse DonCash Eclipse 2 04-08-2008 05:46 PM
Need help returning data from database dyn03 Database 0 03-11-2008 05:55 PM
Returning a string (Partly Bold) TampaTechGuy New To Java 3 02-03-2008 10:54 AM
is synchronization on method passing local variables as parameters needed reddzer Java Servlet 0 11-10-2007 05:47 PM
selectSingleNode not returning element... schu777 XML 4 07-31-2007 06:19 PM


All times are GMT +3. The time now is 09:09 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org