Adrian Roth
Adrian Roth

Reputation: 23

Load C++ class by name from Java

Let's assume I have a framework written in Java and some C++ code which does resource intensive work. — The framework initializes a processing chain based on a database configuration. The processing units (of this chain) are written in C++. Every unit implements the following interface:

class IModule {
public:
    virtual ~IModule() {};
    virtual bool setConfig(ModConfig* config) = 0;
    virtual map<string*,string*>* getStatus() = 0;
};

I want that developers are able to implement the interface IModule in C++ and make a database entry containing the name of the class. The Java framework then automatically loads that class. — The objective must not be to write additional binding code neither in C++ nor in Java. As you can see in the interface the method setConfig() receives an object of type ModConfig. Meaning that it must be possible to make an instance of that C++ object in Java.

I evaluated the following technologies: - JNA: C only -> needs addition binding code - JNIEasy: maps object to object directly - SWIG: maps object to object directly - BridJ: maps object to object directly - JNI: maybe a solution for the problem?

Summary: - Load C++ class by name from Java. - Instantiate C++ object in Java.

I don't expect anybody to provide me with code. Just point me in the right direction (technology).

Thanks in advance

Upvotes: 2

Views: 521

Answers (1)

Your question is deeply operating system specific (or I misunderstood it). I don't understand what loading a C++ class at runtime means to you (it certainly is not possible in pure C++11, you need operating system support).

You could load a dynamically linkable shared object on Posix systems with dlopen then get a symbol's address in it with dlsym. Don't forget to declare extern "C"the C++ functions you want to find.

If you are concerned about C++ classes, then look perhaps also in Qt's Qlibrary and QPluginLoader for inspiration.

Read more about plug-ins.

Upvotes: 1

Related Questions