简体   繁体   English

在C中嵌入Java库

[英]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. 我打算编写一个Java服务器/客户端应用程序,其中客户端实际上不是客户端(它没有主服务器),但它是一个库。

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. 另外,我必须开发一个需要与服务器交互的C模块(保险丝驱动程序),因此需要调用客户端的功能。

I've founded many examples of C functions invoked from Java application, but no one of what I need. 我已经创建了许多从Java应用程序调用的C函数的例子,但没有我需要的。

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. 因为有人无法理解我需要什么,我想要更清楚:我有一个服务器,程序只能使用用Java编写的库与它交互。 The real client is written in C, and it has to be able to invoke the library's functions, so in CI have to call java method 真正的客户端是用C编写的,它必须能够调用库的功能,所以在CI中必须调用java方法

There are essentially two ways to link C and Java code; 基本上有两种方法可以链接C和Java代码; JNA and JNI. JNA和JNI。

JNA is typically used for trivial interfaces; JNA通常用于简单的接口; binding shared libraries with appropriate signatures for calls from the JVM into C libraries. 绑定共享库以及从JVM到C库的调用的适当签名。 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. 有些东西单独使用JNA是不可行的,特别是在C方面调用Java方法和直接修改Java对象,在那里人们会很快得到另一层来上下传递修改。 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 . 关键是,类型映射仅限于基元和一些数组(缓冲区/字符串)类型: 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. 但是,仅使用JNA就不可能从C函数调用Java方法。

If you need to call Java methods or twiddle around within the JVM's objects 'from below' in C, start with JNI. 如果你需要在C语言中调用Java方法或在'下面'的JVM对象中旋转,那么从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. 从您的问题,我假设您可能希望从C(本机)端而不是从JVM执行整个Java <> C工具。 In this case, you need to embed a JVM into your C/C++ application using JNI: https://stackoverflow.com/a/7506378/1175253 在这种情况下,您需要使用JNI将JVM嵌入到C / C ++应用程序中: https//stackoverflow.com/a/7506378/1175253

How to generate C headers for JNI implementation: How to generate JNI header file in Eclipse 如何为JNI实现生成C头: 如何在Eclipse中生成JNI头文件

Of course, you can (or rather should) implement JNI C functions with C++, it simplifies resource management by using RAII, etc. 当然,您可以(或者更应该)使用C ++实现JNI C函数,它通过使用RAII等简化了资源管理。


Edit 编辑

Programming with JNI == playing with fire. 用JNI编程==玩火。 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. 处理C侧的全局引用,线程,数组固定等。是否实际需要使用JNI在很大程度上取决于您想要实现的目标。

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). 可以从JNIEnv中分别检索任何Java类及其方法和字段以进行调用或修改(就像Java的反射一样)。 Invoking a native Java (JNI) method this way might be dangerous. 以这种方式调用本机Java(JNI)方法可能很危险。 Assuming, one locks a non-recursive mutex within the C implementation, a nested call could end up in a deadlock. 假设,在C实现中锁定非递归互斥锁,嵌套调用最终会陷入死锁。 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. 因此,人们通常从C调用普通(简单)Java方法,这些方法既不是原生的也不是自己的本地方法,即使只是为了正确的设计。 The overhead introduced by JNI is negligible from what I experienced in my recent project. JNI引入的开销与我在最近的项目中所经历的开销相差无几。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM