Du verwendest einen veralteten Browser. Es ist möglich, dass diese oder andere Websites nicht korrekt angezeigt werden. Du solltest ein Upgrade durchführen oder ein alternativer Browser verwenden.
JNI - Istanzieren einer C++ classes aus einem Java-Programm
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.
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.
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();
};