简体   繁体   English

调用内置的Java本机方法

[英]Calling built-in java native methods

Is it possible to call the JVM's built-in native code, ie the code that various class in java.lang and java.io call? 是否可以调用JVM的内置本机代码,即java.lang和java.io中的各个类调用的代码? In other words, can you bypass the built-in java API to access various system-level calls such as file-system access? 换句话说,您可以绕过内置的Java API来访问各种系统级调用,例如文件系统访问吗? I know I could do this by building my own native code library and calling that via JNI, but it would be elegant to not need an extra native library for functionality already built into the JVM. 我知道我可以通过构建自己的本机代码库并通过JNI进行调用来做到这一点,但是如果不需要额外的本机库来实现JVM中已经内置的功能,那将是很优雅的。

No you can't. 不,你不能。 It's designed that way on purpose; 它是故意设计的。 you would override the API contracts if you could. 如果可以的话,您将覆盖API合同。

In any event, the standard library wrapper code is very slight and with JIT compilers you shouldn't notice any speed impact. 无论如何,标准的库包装器代码都很少 ,对于JIT编译器,您不会注意到任何速度影响。

Furthermore, the implementation of those methods are not part of the API spec. 此外,这些方法的实现也不是API规范的一部分。 What is "native" for one implementation of Java doesn't have to be for another. Java的一种实现的“本机”不必是另一种。

You can, of course, use reflection to call the methods providing the code is trusted. 当然,如果代码受信任,则可以使用反射来调用方法。 However, the non-public APIs are liable to change between updates and implementations, so pretty much pointless and absolutely non-elegant. 但是,非公共API可能会在更新和实现之间进行更改,因此几乎没有意义,而且绝对不雅致。

如果要本机IO,请使用NIO类。

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

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