1414import com .sun .jna .Native ;
1515import java .io .IOException ;
1616import java .lang .reflect .Field ;
17+ import java .lang .reflect .InvocationTargetException ;
18+ import java .lang .reflect .Method ;
1719import java .util .ArrayList ;
1820import java .util .List ;
1921import java .util .regex .Pattern ;
@@ -35,10 +37,12 @@ class UnixProcessManager extends ProcessManager {
3537 private static final CLibrary C_LIBRARY ;
3638
3739 private static final Field PID_FIELD ;
40+ private static final Method PID_METHOD ;
3841
3942 static {
4043 CLibrary lib = null ;
4144 Field pidField = null ;
45+ Method pidMethod = null ;
4246 if (SystemInfo .isUnix ()) {
4347 try {
4448 lib = ((CLibrary ) Native .loadLibrary ("c" , CLibrary .class ));
@@ -53,11 +57,17 @@ class UnixProcessManager extends ProcessManager {
5357 .getDeclaredField ("pid" );
5458 pidField .setAccessible (true );
5559 } catch (Exception e ) {
56- LOG .error (e .getMessage (), e );
60+ // try with Java9
61+ try {
62+ pidMethod = Process .class .getDeclaredMethod ("pid" );
63+ } catch (NoSuchMethodException e1 ) {
64+ LOG .error (e1 .getMessage (), e1 );
65+ }
5766 }
5867 }
5968 C_LIBRARY = lib ;
6069 PID_FIELD = pidField ;
70+ PID_METHOD = pidMethod ;
6171 }
6272
6373 private static interface CLibrary extends Library {
@@ -173,6 +183,12 @@ int getPid(Process process) {
173183 } catch (IllegalAccessException e ) {
174184 throw new IllegalStateException ("Can't get process' pid. Not unix system?" , e );
175185 }
186+ } else if (PID_METHOD != null ) {
187+ try {
188+ return ((Long ) PID_METHOD .invoke (process )).intValue ();
189+ } catch (IllegalAccessException | InvocationTargetException e ) {
190+ throw new IllegalStateException ("Can't get process' pid. Not unix system?" , e );
191+ }
176192 } else {
177193 throw new IllegalStateException ("Can't get process' pid. Not unix system?" );
178194 }
0 commit comments