Forum Stats

  • 3,838,876 Users
  • 2,262,407 Discussions


How to call class os functions defined in share/vm/runtime/os.hpp?

user1130419 Member Posts: 3
edited Jul 10, 2014 5:27PM in Java Native Interface (JNI)

HI. I haven't coded in C for some time, and no experience with JNI

I dabbled a bit in JNI and creating native functions.

I would like to know, how can I call Java VM os functions from my own libraries without much hassle? e.g. os::javaTimeMillis(), or os::active_processor_count() ?

#include "HelloWorld.h"

JNIEXPORT void JNICALL Java_HelloWorld_sayHi
  (JNIEnv *env, jclass clazz) {

long x = os::javaTimeMillis(); // HOW TO DO THAT?

Including src/share/vm/runtime/os.hpp seems overkill with lot of dependencies?

Could I create a custom header file copy-pasting parts from os.hpp to make some functions known?

And I don't want to java-call the native java methods that mapped to these os functions, but call C functions directly.


  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge

    Two parts

    1. How do you write C code.

    2. How do you use JNI to interface between java and C code.

    1 is independent from 2 but 2 requires 1 because you can't do it if you don't know how to write C code.

    If you want to do JNI then you start by writing C code and debugging the C code.  No JNI at all in that process.

    After you complete that then you write wrappers around the C code which are JNI.

    JNI code tends to make Java unstable because bugs in JNI code will cause the VM to crash.  So it is often better to find an executable that does what one wants and then use Java Runtime.exec to manage that executable.  That is going to be true even if one is writing the executable (like in C.)

    To learn C you should start with a C site.

This discussion has been closed.