1

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.

1
  • You can but you shouldn't need to. What is you aim in bypassing them? Commented May 27, 2009 at 20:17

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
0

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.

Comments

-1

If you want Native IO use the NIO classes.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.