我在CentOS服务器上安装了JRE的64位linux发行版(1.6_34)。我拉下源代码并深入研究java.net.SocketInputStream
类。
该类有一个名为socketRead0
的方法
/**
* 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
方法时执行的本机源代码?提前感谢!
发布于 2013-04-19 18:11:08
正如在其他答案中所解释的,native
方法实际上是通过JNI调用的C函数。包含C函数的库文件通常与System.loadLibrary
一起加载,并且遵循Java命名方案(前缀为Java_
,后跟包、类和方法名,带有下划线而不是点)的导出将自动链接到这些native
Java方法。
但是,其他答案没有提到将C函数链接到native
方法的第二种方法:RegisterNatives。此接口可用于提供C实现,而无需调用System.loadLibrary
,使用JNI命名方案,甚至导出这些函数。
发布于 2013-04-19 05:51:12
在OpenJDK中,由于某种原因,它是here,我找不到它的实现。通常搜索本机文件夹。你的文件会在里面
发布于 2013-04-18 21:43:13
native
方法表示该方法不是Java代码,而是本机代码-a.k.a。机器码-通常用C/C++编写。对这些函数的调用是通过JNI进行的。
您应该搜索它加载所使用的本地库的位置,如下所示
System.loadLibrary("nameOfLibrary");
此本机库应导出用于Java的函数,如
JNIEXPORT returnType JNICALL Java_ClassName_methodName
https://stackoverflow.com/questions/16092959
复制