Sign in to follow this  
OpenGL_Guru

[java] Java/JNI - passing parameters(value)

Recommended Posts

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. what do i need to do here?

  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. i just need to know how to change the value of the object that is passed. thanks all in advance!

Share this post


Link to post
Share on other sites
In Java, boxed primitive types are immutable.

In other words - Java doesn't have references, unless you implement them yourself.

The following can't be done in Java:
void foo( double & x ) { x = 2; }


But you can create your own container:
public class MyDouble {
public double x;
};

public void foo(MyDouble x) {
x.x = 2;
};


But this isn't idiomatic Java. If you need to return something, use return. JNI is painful enough without trying to butcher Java itself.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
In Java, boxed primitive types are immutable.

In other words - Java doesn't have references, unless you implement them yourself.

The following can't be done in Java:
void foo( double & x ) { x = 2; }


But you can create your own container:*** Source Snippet Removed ***

But this isn't idiomatic Java. If you need to return something, use return. JNI is painful enough without trying to butcher Java itself.



is that code that you gave me need to be in my .java code or the c++ code?

sorry for the confusion but coming from C++ straight into java to this is really confusing and frustrating.

why in the world did they ever come to the design decision to just make arrays pass by ref?

[Edited by - OpenGL_Guru on January 31, 2008 9:31:43 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this