首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java的本机方法是在哪里定义的?

Java的本机方法是在哪里定义的?
EN

Stack Overflow用户
提问于 2013-04-19 04:58:32
回答 3查看 1.9K关注 0票数 3

我在CentOS服务器上安装了JRE的64位linux发行版(1.6_34)。我拉下源代码并深入研究java.net.SocketInputStream类。

该类有一个名为socketRead0的方法

代码语言:javascript
运行
复制
/** 
 * Reads into an array of bytes at the specified offset using
 * the received socket primitive. 
 * @param fd the FileDescriptor
 * @param b the buffer into which the data is read
 * @param off the start offset of the data
 * @param len the maximum number of bytes read
 * @param timeout the read timeout in ms
 * @return the actual number of bytes read, -1 is
 *          returned when the end of the stream is reached. 
 * @exception IOException If an I/O error has occurred.
*/
private native int socketRead0(FileDescriptor fd, 
        byte b[], int off, int len, int timeout)
        throws IOException;

在哪里可以找到在执行此SocketInputStream#socketRead0方法时执行的本机源代码?提前感谢!

EN

回答 3

Stack Overflow用户

发布于 2013-04-20 02:11:08

正如在其他答案中所解释的,native方法实际上是通过JNI调用的C函数。包含C函数的库文件通常与System.loadLibrary一起加载,并且遵循Java命名方案(前缀为Java_,后跟包、类和方法名,带有下划线而不是点)的导出将自动链接到这些native Java方法。

但是,其他答案没有提到将C函数链接到native方法的第二种方法:RegisterNatives。此接口可用于提供C实现,而无需调用System.loadLibrary,使用JNI命名方案,甚至导出这些函数。

票数 2
EN

Stack Overflow用户

发布于 2013-04-19 13:51:12

在OpenJDK中,由于某种原因,它是here,我找不到它的实现。通常搜索本机文件夹。你的文件会在里面

票数 1
EN

Stack Overflow用户

发布于 2013-04-19 05:43:13

native方法表示该方法不是Java代码,而是本机代码-a.k.a。机器码-通常用C/C++编写。对这些函数的调用是通过JNI进行的。

您应该搜索它加载所使用的本地库的位置,如下所示

代码语言:javascript
运行
复制
System.loadLibrary("nameOfLibrary");

此本机库应导出用于Java的函数,如

代码语言:javascript
运行
复制
JNIEXPORT returnType JNICALL Java_ClassName_methodName
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16092959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档