JNI - Istanzieren einer C++ classes aus einem Java-Programm

Bitte aktiviere JavaScript!
Status
Nicht offen für weitere Antworten.
F

free9897

Hallo,
ich möchte via JNI von einem Java Programm auf eine C++ Classe zugreifen: das heißt diese istanzieren, im Speicher halten und verschiedene Methoden aufrufen - kurz, wie ein ganz normale Object zu benutzen.

Ich habe im Internet nachgesehen und beispielsweise im Java Tutorial und auf Webseiten wird immer nur von Native Method calling gesprochen und nicht von C++-Objekt-Istanzierung.

(http://java.sun.com/docs/books/jni/)

Habe ich da etwas falsch verstanden? Kann mir jemand helfen?

danke
ciao
 
A

Anzeige




Schau mal hier —> (hier klicken)
afaik:
Man kann nur gewöhnliche Prozeduren von Java aus aufrufen. Aber du könntest sie so bauen, dass du ihnen immer die Adresse des schon erzeugen Objekts mitgibst, dessen Methoden du dann aufrufst. Und natürlich auch eine, die das C++ Objekt konstruiert und dir die Adresse gibt, und eine, die es wieder zerstört.
 
G

Guest

Here is how I handled the problem:

1. Java class

The sample program is a easy gum-automat: you can throw money in, push a button to get a gum and get the rest of the money: here is the Java code, which maps all the methods defined later in the C++ Object

AutomatProxy.java

Code:
class AutomatProxy {
    
private long objref;

	private native long automatProxy(int a, int b) ;
	private native void throwInProxy(long l, int b) ;
	private native int getPriceProxy(long l) ;
	private native int moneyBackProxy(long l) ;
	private native int pushButtonProxy(long l);

  	public AutomatProxy() {}

	public void start() {

int NumItems = 2;
int PriceItem = 3;
	System.out.println("Building of Automat: items in automat: "+ NumItems+ ", Price: "+ PriceItem);
objref = automatProxy(NumItems,PriceItem); //reference to object

	System.out.println(" - I put in 10 cent");
throwInProxy(objref,8);

	System.out.println();
	System.out.print("I push the button: ");
  	int b = pushButtonProxy(objref);
  	if(b==1)	        
  		System.out.println("Got a gum!!!");
  	else
 		System.out.println("Didn't get any gum :-(");

	System.out.println();
	System.out.print("I push the button: ");
b = pushButtonProxy(objref);
 	if(b==1)
   		System.out.println("Got a gum!!!");
else
   		System.out.println("Didn't get any gum :-(");

	System.out.println();
	System.out.print("I push the button: ");
  	b = pushButtonProxy(objref);
  	if(b==1)	        
  		System.out.println("Got a gum!!!");
  	else
  		System.out.println("Didn't get any gum :-(");

 	System.out.println();
	System.out.print("I push the button to get the remaining money back: ");
b = moneyBackProxy(objref);
	System.out.println("Got "+b +" cent back");
	}

	public static void main(String[] args) {
AutomatProxy test = new AutomatProxy();
test.start();
 	}

	static {System.loadLibrary("interface");}
}
2. Native Code and C++ Object definition

The following code includes the native methods called by the Java class and the C++ object definition of the Automat object as well as its method implementation.

When automatProxy (_AutomatProxy_automatProxy)is called a Automat-C++-Object is created. The reference to this object is then returned to the Java Class which stores it in the variable objref. Any time a native method is called objref is passed to the native method which uses it to access the methods of the Automat-Object. Note that the Automat-Object in this way is obviously only created ONES (in automatProxy).

interface.cpp

#include <jni.h>
#include<iostream>
using namespace std;
#include "AutomatProxy.h"

class Automat
{
private:
int itemCount;
int itemPrice;

public:
Automat(int, int);
int insertedMoney;
void throwIn(int);
int getPrice();
int moneyBack();
bool pushButton();
bool isEmpty();
};


void Automat::throwIn(int money)
{ insertedMoney+=money;}

int Automat::moneyBack(){
int temp=insertedMoney;
insertedMoney=0;
return temp;
}

bool Automat::pushButton()
{
if(itemPrice>insertedMoney)
{
return false;
}
if(itemCount==0)
{
return false;
}
--itemCount;
insertedMoney-=itemPrice;
return true;
}

bool Automat::isEmpty()
{ return itemCount==0;}

int Automat::getPrice()
{ return itemPrice;}

Automat::Automat(int items, int price)
{ itemCount=items;
insertedMoney=0;
itemPrice=price;
}


JNIEXPORT jlong JNICALL Java_AutomatProxy_automatProxy
(JNIEnv *env, jobject c, jint a , jint b)
{ Automat* temp = new Automat(a,b);
cout << "\n -----> istanziato new automat with parameters: items "<<a<<", price "<<b;
return (long) temp;
}

JNIEXPORT void JNICALL Java_AutomatProxy_throwInProxy
(JNIEnv *env, jobject c, jlong d, jint i)
{ Automat* a = (Automat*) d;
a->throwIn(i);
cout << "\n -----> " <<a->insertedMoney<< " cent in the automat";
}

JNIEXPORT jint JNICALL Java_AutomatProxy_getPriceProxy
(JNIEnv *, jobject,jlong d)
{ Automat* a = (Automat*) d;
return a->getPrice();}

JNIEXPORT jint JNICALL Java_AutomatProxy_moneyBackProxy
(JNIEnv *, jobject,jlong d)
{ Automat* a = (Automat*) d;
return a->moneyBack();
}

JNIEXPORT jint JNICALL Java_AutomatProxy_pushButtonProxy
(JNIEnv *, jobject,jlong d)
{ Automat* a = (Automat*) d;
int i = a->pushButton();
cout << "\n -----> Money left in Automat " <<a->insertedMoney<< " cent - ";
return i;
}
3. Compiling

Compile the Java-class as following:

javac AutomatProxy.java

javah –jni AutomatProxy

Compile the C++-class as following (Using Microsoft Visual C++): (change path paths)

cl /EHsc -Ic:\JBuilderX\jdk1.4\include -Ic:\JBuilderX\jdk1.4\include\win32 -LD d:\automat\interface.cpp -Fed:\automat\interface.dll


4. Running the program

Run the program

java AutomatProxy

The output looks like that:

Building of Automat: items in automat: 2, Price: 3

-----> istanziato new automat with parameters: items 2, price 3 - I put in 10 cent

-----> 8 cent in the automat
I push the button:
-----> Money left in Automat 5 cent - Got a gum!!!

I push the button:
-----> Money left in Automat 2 cent - Got a gum!!!

I push the button:
-----> Money left in Automat 2 cent - Didn't get any gum :-(

I push the button to get the remaining money back: Got 2 cent back


REF:
http://www.ordix.de/ORDIXNews/2_2001/db_1.html
http://www.ordix.de/ORDIXNews/3_2001/db_1.html
http://www.haertfelder.com/jni.html
 
A

Anzeige




Vielleicht hilft dir das hier weiter: (klicke hier)
Status
Nicht offen für weitere Antworten.
Passende Stellenanzeigen aus deiner Region:

Neue Themen

Oben