Bob
Bob

Reputation: 57

How does java program access operating System functions?

How does java program access operating System functions? Is that using JNI ?

Upvotes: 0

Views: 3171

Answers (2)

David Oliván
David Oliván

Reputation: 2725

JNI is used when your Java application needs to perform a call to native code (i.e. DLL in Windows).

The JVM itself is programmed in C/C++ so each implementation uses the OS API libraries available to make the SO calls (Win32 API for Windows).

One Java call could lead to an OS call (or many, or none, depends like I/O buffered by JVM), so the JVM call the OS with the parameters from Java and returns the result to the program.

If your application needs to access the OS, must be through the Java API that makes some OS features available to your program, like files, sockets, windows. Many OS features are not available for Java applications due to many reasons.

Some special features are available through external products or libraries like the serial (COM), parallel and bluetooth ports. These products/libraries come with a JAVA API available to your program and this API make the OS calls using JNI by using some native libraries included in the product/library.

If you need special features of the OS not available in Java or external products, consider creating a native library, create a JAVA API wrapping it and call it from the API using JNI. You (and other) application can use the API to access these features.

Upvotes: 5

duffymo
duffymo

Reputation: 308743

Better to start with java.lang.Runtime or, better yet, java.lang.Process.

Depending on what you're calling the operating system for, you might also be interested in the java.lang.management package.

Upvotes: 1

Related Questions