Search code examples
javacjava-native-interfacejconsole

How to set main class displayed by `jps` when using JNI?


How do I set the name of the main class displayed by jps or JConsole when starting a Java process via JNI?

In C, I get a class and main method like so:

jclass main_class = (*env)->FindClass(env, java_main_class);
...
jmethodID main_method = (*env)->GetStaticMethodID(env, main_class, "main", "([Ljava/lang/String;)V");

And start Java like this:

(*env)->CallStaticVoidMethod(env, main_class, main_method, main_args);

My process works well, but when I run jps I do not see the Main class. For example:

$ jps -l
30056 
30065 sun.tools.jps.Jps

I also see a blank string when looking for the process in JConsole.

However, I do see my args with jps:

$ jps -lv
30068  -Dfile.encoding=UTF-8 -Xmx500m ...
30070 sun.tools.jps.Jps -Dapplication.home=

Solution

  • Set the system property sun.java.command

    For example: -Dsun.java.command=com.example.Main

    I found this by searching through the source code for the java executable that is distributed with the JDK.