Java标准库如何与本机代码通信?
他们使用 Mono 的 PInvoke 之类的东西吗?或者它更像是在运行时启动之前注册的内部调用? java 是否有一个用于处理本机调用(如 mscorlib.dll)的基础库?如果我想用 C 代码调用 JVM,它的库会查找 .so/.dll 文件吗?如果我静态链接所有 JRE 本机库,这对 Java 标准库有影响吗?
Do they use something like Mono's PInvoke? Or is it more like internal calls registered before the runtime is started? Does java have a base library for handling native calls like mscorlib.dll? If I want to invoke a JVM in C code will it libraries look for the .so/.dll files? Does it make a difference to Java standard libraries if I statically link all of the JRE natives libraries?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
他们使用 JNI(正如公开记录的那样)来调用特定平台的本机共享库。
至于从 C 代码调用 JVM,JVM 使用共享库(DLL、SO 等)。快速搜索 JDK 6 源代码并没有发现任何 System.loadLibrary() 的核心本机支持(例如 Object、String 等中的本机方法)。这对我来说意味着这些方法的本机代码(从 JRE/bin 目录的内容来看似乎位于 DLL 中)是由 java.exe(以及 Windows 中的 javaw.exe)显式链接的。
当我上次查看这些内容时,从 C 代码调用 JVM 的要求是 JNI 中详细记录的部分 - 我强烈建议您参考该 doco 以进一步进行。我们甚至成功地为 IBM AS/400 Java 1.1 JVM 编写了一个本机 C 包装器/加载器。
They use JNI, exactly as it is publicly documented, to invoke native shared libraries for the specific platform.
As far as invoking a JVM from C code, the JVM uses shared libraries (DLL, SO, etc). A quick search of the JDK 6 source code does not reveal any System.loadLibrary() for the core native support (like native methods in Object, String, etc). That suggests to me that the native code for these methods, which appears to be in DLL's judging from the contents of the JRE/bin directory, is explicitly linked by java.exe (and javaw.exe in Windows).
When I last looked at this stuff, the requirements for invoking a JVM from C code was a well documented part of JNI - I strongly suggest you refer to that doco to proceed further. We even went so far as successfully writing a native C wrapper/loader for the IBM AS/400 Java 1.1 JVM.
他们使用 Java 本机接口 (JNI)。
我从未从 C 调用过 JVM,所以我对此一无所知。
They use the Java Native Interface (JNI).
I've never called the JVM from C, so I don't know about that.
这里有一个关于如何从 C 程序内部启动 JVM 的示例:
http ://www.inonit.com/cygwin/jni/inspirationApi/c.html
There is an example here about how to start a JVM from inside your C program:
http://www.inonit.com/cygwin/jni/invocationApi/c.html
Java Native Access (JNA) 使 Java 程序可以轻松访问本机共享库(Windows 上的 DLL)除了 Java 代码之外,无需编写任何内容”(引自其主页)。
个人到目前为止还没有尝试过。
"Java Native Access (JNA) provides Java programs easy access to native shared libraries (DLLs on Windows) without writing anything but Java code" (quotation from their homepage).
Personally never tried it so far.