Jack
Jack

Reputation: 1606

Maven compile with compiler option --add-exports

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.

Upvotes: 2

Views: 3584

Answers (1)

Nicolai Parlog
Nicolai Parlog

Reputation: 51140

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. :)

Upvotes: 2

Related Questions