Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed Java library in C

I'm going to write a Java server/client application, in which the client is not really a client (it doesn't have a main), but it's a library.

Aside, I have to develop a C module (a fuse driver) that needs to interact with the server, so it needs to invoke client's function.

I've founded many examples of C functions invoked from Java application, but no one of what I need.

Can you give me a suggest or some hints?

EDIT

because someone could not understand what i need, I want to be more clear: I have a server, and a program can interact with it only using a library, written in Java. The real client is written in C, and it has to be able to invoke the library's functions, so in C I have to call java method

like image 893
litiales Avatar asked Apr 13 '13 15:04

litiales


1 Answers

There are essentially two ways to link C and Java code; JNA and JNI.

JNA is typically used for trivial interfaces; binding shared libraries with appropriate signatures for calls from the JVM into C libraries. Some things are not feasible with JNA alone, especially calls to Java methods and direct modification of Java objects at the C side, where one would quickly end up with another layer to pass the modifications up and down. The point is, that the type map is restricted to primitives and some array (buffer/string) types: http://jna.java.net/javadoc/overview-summary.html#marshalling. This layer would most probably consist of less concise code, introducing additional overhead. But still, calls from C functions to Java methods are impossible with JNA alone.

If you need to call Java methods or twiddle around within the JVM's objects 'from below' in C, start with JNI.

From your question, I assume, that you probably want to execute your whole Java <> C facility from the C (native) side rather than from a JVM. In this case, you need to embed a JVM into your C/C++ application using JNI: https://stackoverflow.com/a/7506378/1175253

How to generate C headers for JNI implementation: How to generate JNI header file in Eclipse

Of course, you can (or rather should) implement JNI C functions with C++, it simplifies resource management by using RAII, etc.


Edit

Programming with JNI == playing with fire. Take care of global references at the C side, threads, array pinning, etc. Whether you actually need to use JNI greatly depends on what exactly you want to achieve.

One can retrieve any Java class as well as its methods and fields from the JNIEnv for invocation or modification respectively (just like Java's reflection). Invoking a native Java (JNI) method this way might be dangerous. Assuming, one locks a non-recursive mutex within the C implementation, a nested call could end up in a deadlock. So one typically invokes plain (simple) Java methods from C which neither are native nor call own, native methods themselves, even if just for the sake of proper design. The overhead introduced by JNI is negligible from what I experienced in my recent project.

like image 184
Sam Avatar answered Nov 05 '22 13:11

Sam