1

So I finally switched to Java 15, and found out that my old code is not compiling anymore. I use classes from package sun.jvmstat.monitor and class LocalVmManager to retrieve the pid of all JVM running in the system: this is working on Java8, but no more on Java15 (I think it doesn't work since Java9).

Thanks to IntelliJ I discovered that I need to pass the following options to javac: --add-exports jdk.internal.jvmstat/sun.jvmstat.perfdata.monitor.protocol.local=ALL-UNNAMED --add-exports jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED --add-exports jdk.internal.jvmstat/sun.jvmstat.monitor.event=ALL-UNNAMED

And in fact with this options I'm able to compile via command line. But I also wanted to compile my application via mvn compile. How can I specify options to compiler in the pom.xml ?

I tried the following with no luck:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>15</release>
                <compilerArgs>
                    <arg>--add-exports</arg>
                    <arg>jdk.internal.jvmstat/sun.jvmstat.perfdata.monitor.protocol.local=ALL-UNNAMED</arg>
                    <arg>--add-exports</arg>
                    <arg>jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED</arg>
                    <arg>--add-exports</arg>
                    <arg>jdk.internal.jvmstat/sun.jvmstat.monitor.event=ALL-UNNAMED</arg>
                </compilerArgs>
                <fork>true</fork>
            </configuration>
        </plugin>

In the meantime I'm going to change my code and read the pid of running JVMs by scanning the /proc/ directory.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Jack
  • 1,488
  • 11
  • 21
  • 2
    As [is documented](https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html#compilerArgs), `` as you have them written will work. Can you edit your question to explain further what isn't working? Finally, if 9 is your baseline https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/Process.html#pid() is the simple way to do what you're trying to do. – Laird Nelson Nov 20 '20 at 22:43

1 Answers1

2

I have good news for you, you don't need LocalVmManager anymore. This will do for your current JVM:

ProcessHandle.current().pid()

To access the PIDs of other processes:

ProcessHandle
    // returns Stream<ProcessHandle>
    .allProcesses()
    .filter(/* filter to only find JVM processes */)
    .map(ProcessHandle::pid)
    .collect(toList());

The ProcessHandle API was added in Java 9 and gives you a lot of information about OS processes - check the method info() as well. :)

Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255
  • 3
    Not what I've asked for – Jack Jan 20 '21 at 15:20
  • 1
    I overlooked that you needed the PIDs of all JVM processes - just added that to the answer. I know you asked for access to `LocalVmManager`, but given there's a supported API, you should not used internals. There's no guarantee they work in other JVMs/OSs or even on your JVM/OS in the next minor release. – Nicolai Parlog Jan 21 '21 at 08:11
  • 1
    My comment was to your first answer where you only replied how to retrieve current process pid – Jack Jan 21 '21 at 11:28
  • Makes sense. Once you've tried this and it works, I'll delete my end of this conversation because it doesn't add anything and may indeed be confusing to later visitors. – Nicolai Parlog Jan 21 '21 at 12:27