3

I a code in java that looks like the following:

class MyClass
{
public void MyFunc(ISomeListener listener);
}

I want to call this method from a c++ program and pass to it the required parameter. Note that the parameter is an implementation of an interface. Thus I need to define AND create java object from c++ and pass it to the method. How can I do this?

P.S. If it helps, the original problem is that the java program is an interface to a hardware (like a driver). I'm trying to create a DLL from this java code so that it can be used in any language. So I thought I'd wrap the code using C/C++ and then make a DLL out of that.

atoMerz
  • 7,534
  • 16
  • 61
  • 101
  • 1
    In my opinion, that approach is fundamentally flawed. Java can not have any kind of low level interfacing with hardware unless some native lib is used though jni. The real and usual way to achieve that is to wrap the native counterpart in java, not the other way arround. You are going 360º to return to the starting point. I don't know the exact problem, but it seems odd. –  Apr 10 '13 at 11:53
  • [You can call Java from C++ using JNI](http://stackoverflow.com/questions/819536/how-to-call-java-function-from-c). Did I understand you correctly that you want to implement a Java interface in C++ and then call a Java function and pass the C++ implementation to it? I wonder if that's even possible. Especially considering that C++ doesn't even have interfaces. – Philipp Apr 10 '13 at 11:55
  • @Yn5an3, you maybe right. But the application is already implemented and it would not be cost effective to re-implement it. – atoMerz Apr 10 '13 at 11:56
  • @Philipp, I have used JNI and successfully run java code from C. You understood correctly, that's why I'm asking the question here to see if it's possible(how?). – atoMerz Apr 10 '13 at 11:57
  • It would probably be much easier to directly use, in your C++ code, the same native library that your Java code is using. – Perception Apr 10 '13 at 12:06
  • JNI allows you to instantiate any Java objects defined in your Java files, from within C++; these can later be passed as arguments to further Java functions called from within C++. I have done this extensively. The details are a bit tricky, so I'm just noting that it's possible and you need to study the JNI documentation: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/index.html – Dan Nissenbaum Apr 10 '13 at 13:04

1 Answers1

0

I managed to solve the problem by defining the Listener class in java. The listener has a native method with the desired callback signature.

public class MyImpl implements MyInterface
{
  @override
  public native int callback(int i);
}

Compiled this using javac. Then using javah to produce the function declaration to be used with JNI. In the C program I defined the function and loaded Impl class and registered callback into it using RegisterNatives.

P.S. The question of how to actually define a class using JNI remains unanswered.

EDIT:
There's a downside to using this method. I will have to register callbacks in class scope not object scope.

atoMerz
  • 7,534
  • 16
  • 61
  • 101
  • 1
    If you want to define classes at run-time, then you can use the [JavaCompiler class](http://www.accordess.com/wpblog/an-overview-of-java-compilation-api-jsr-199/). It's trivial to invoke this through JNI; it just requires a bit of code. You're far better off defining all the classes you need up-front and just loading the jar of them, rather than dynamically constructing them. – Anya Shenanigans Apr 13 '13 at 13:22