Is it possible to call the JVM's built-in native code, i.e. the code that various class in java.lang and java.io call? In other words, can you bypass the built-in java API to access various system-level calls such as file-system access? 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.
3 Answers
No you can't. It's designed that way on purpose; you would override the API contracts if you could.
In any event, the standard library wrapper code is very slight and with JIT compilers you shouldn't notice any speed impact.
Furthermore, the implementation of those methods are not part of the API spec. What is "native" for one implementation of Java doesn't have to be for another.
2 Comments
toluju
Thanks for the clarification. I am not really motivated by speed, the bigger motivation here is to abstract certain basic functionality in a more uniform manner than is provided by the default java API. Sun seems aware of some of these shortcomings, hence the confusing iterations from java.io to java.nio to java.nio2.
Jason Cohen
You bet! Your critique of Java's API is fair enough! I suggest defining your own API and using the standard library behind the scenes. It's enough to just shield your code from the horrors; the rest (platform behavior, speed) won't be a problem.
If you want Native IO use the NIO classes.
1 Comment
toluju
To my knowledge the NIO classes refer to "New IO", not "Native IO". They are still Java wrappers around the underlying native implementation.