Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to integrate C++ CLASS in a java program II

843829Mar 28 2003 — edited Mar 31 2003
if I write a very simple java program such as this one:

class Calculus {
public native int displayCalculus(int x);

static{
System.loadLibrary("calculus");
}
/** Creates a new instance of Class */
public static void main(String[] args)throws Exception {
int x;
int y=0;
String inputString = new String();
char Car;
Calculus c = new Calculus();

System.out.println("give x");
Car=(char)System.in.read();
while(Car >= '0' && Car <= '9'){
inputString = inputString + Car;
Car = (char)System.in.read();
}

x = Integer.parseInt(inputString);



y = c.displayCalculus(x);

System.out.println("new result: "+ y);

}

}

*****************************
this program asks the user to give a number, and then takes a C++ method "displayCalculus". I type javah -jni Calculus , it creates Calculus.h:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Calculus */

#ifndef IncludedCalculus
#define IncludedCalculus
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Calculus
* Method: displayCalculus
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_Calculus_displayCalculus
(JNIEnv *, jobject, jint);

#ifdef __cplusplus
}
#endif
#endif


***********************
then I create the program in C++:
#include "Calculus.h"
#include <jni.h>
#include <iostream.h>


JNIEXPORT int JNICALL
Java_Calculus_displayCalculus(JNIEnv *env,jobject obj,jint x){

x=10+(x);
return x;
}



*************
finally I create the shared library libcalculus.so
g++ -G -I /usr/local/include/ -I/usr/local/include/solaris CalculusImp.cc -o libcalculus.so

then java Calculus....

THIS WORKS WELL
****************************

BUT if I try to do the same thing with an already existing program in C++ such as this one:

myclass.h:
*******
#ifndef MYCLASS
#define MYCLASS

/*void myclass {

/*public:

myclass( int );

void message( char * );


};*/

void WriteMessage( char );




#endif

***********************
and myclass.cpp:

#include <iostream.h>

#include "myclass.h"

myclass::myclass( int x ) {

cout << "Class created with " << x << " parameter" << endl;

}


void myclass:message( char *message ) {

cout << "Message: " << message << endl;

}

***************
it's not that easy. I don't know, there must be some special lines that I should write... but I don't know how to declare them in the java program...

Please help me I've just spent too much time on this.
Thank you very much for your answers

Ph A




Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 28 2003
Added on Mar 28 2003
3 comments
302 views