Reputation: 107
I would like to know if it is possible to access Win32_OperatingSystem class in Java code, I have seen example of VB or C#, but I have not seen any example in Java
Upvotes: 0
Views: 76
Reputation: 14782
It's possible, but it isn't as easy in Java as in other languages.
Several languages have a so called FFI (Foreign function interface) included in their standard library. For example, in Python, you have ctypes/cdll modules. Using it, you can open any DLL and call any function. You don't need to write any C code, you just write some Python, and it's enough.
In Java, the traditional way to do it is via JNI (Java Native Interface). But it's a lot more complicated. To keep it short, you must:
Some third partie libraries have tried to simplify this complicated process, so to make it as easy as it is in Python and other languages having a good FFI library. For example, JNA (Java Native Access). But it isn't included in the standard library of Java.
Java language implementers knows that the traditional JNI is old, slow and complicated. A new FFI library has been released and included in the standard library, see Foreign functions and memory API, but it's fairly new (since Java 22). In particular, it isn't yet included in the latest LTS release (Java 21). So if you want to use it right now, you must upgrade to a non LTS version (probably not recommended for production) or activate experimental features (of course even less recommended). If you want to have it in a LTS release, you will ahve to wait until Java 25.
So you have a broad idea of the whole story.
Now for win32 specifically, there are probably already JNI wrappers available out there. You should make a search.
Before trying to call native win32 directly, or using a third partie library doing so, you should perhaps also have a look at all what the standard library provides. Everything isn't covered 100%, but a lot is covered in one way or another and there are great chances that you have little to no or negligible benefit in trying to call win32 directly instead of what Java provides. If you have something specific in mind, maybe we can point you to something you could have missed.
Upvotes: 3