tags:

views:

309

answers:

3

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.

+6  A: 

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.

Jason Cohen
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.
toluju
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.
Jason Cohen
A: 

If you want Native IO use the NIO classes.

Gandalf
To my knowledge the NIO classes refer to "New IO", not "Native IO". They are still Java wrappers around the underlying native implementation.
toluju
A: 

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.

Tom Hawtin - tackline