Java How to get the PID from a process?

xiaoxiao2021-02-28  79

文章出自:http://golesny.de/p/code/javagetpid A node from http://bugs.sun.com/view_bug.do?bug_id=4244896

RuntimeMXBean rtb = ManagementFactory.getRuntimeMXBean(); String processName = rtb.getName(); Integer pid = tryPattern1(processName); private static Integer tryPattern1(String processName) { Integer result = null; /* tested on: */ /* - windows xp sp 2, java 1.5.0_13 */ /* - mac os x 10.4.10, java 1.5.0 */ /* - debian linux, java 1.5.0_13 */ /* all return pid@host, e.g 2204@antonius */ Pattern pattern = Pattern.compile("^([0-9]+)@.+$", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(processName); if (matcher.matches()) { result = new Integer(Integer.parseInt(matcher.group(1))); } return result; }

on Unix

if(process.getClass().getName().equals("java.lang.UNIXProcess")) { /* get the PID on unix/linux systems */ try { Field f = process.getClass().getDeclaredField("pid"); f.setAccessible(true); pid = f.getInt(p); } catch (Throwable e) { } }

on Windows

Download the jna.jar on Suns JNA Site

if (process.getClass().getName().equals("java.lang.Win32Process") || process.getClass().getName().equals("java.lang.ProcessImpl")) { /* determine the pid on windows plattforms */ try { Field f = p.getClass().getDeclaredField("handle"); f.setAccessible(true); long handl = f.getLong(p); Kernel32 kernel = Kernel32.INSTANCE; W32API.HANDLE handle = new W32API.HANDLE(); handle.setPointer(Pointer.createConstant(handl)); pid = kernel.GetProcessId(handle); } catch (Throwable e) { } }

Kernel32.java

import com.sun.jna.Native; /* https://jna.dev.java.net/ */ public interface Kernel32 extends W32API { Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class, DEFAULT_OPTIONS); /* http://msdn.microsoft.com/en-us/library/ms683179(VS.85).aspx */ HANDLE GetCurrentProcess(); /* http://msdn.microsoft.com/en-us/library/ms683215.aspx */ int GetProcessId(HANDLE Process); } W32API.java (shortened) /* Copyright (c) 2007 Timothy Wall, All Rights Reserved * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ import java.util.HashMap; import java.util.Map; import com.sun.jna.FromNativeContext; import com.sun.jna.Pointer; import com.sun.jna.PointerType; import com.sun.jna.win32.StdCallLibrary; import com.sun.jna.win32.W32APIFunctionMapper; import com.sun.jna.win32.W32APITypeMapper; /** Base type for most W32 API libraries. Provides standard options * for unicode/ASCII mappings. Set the system property w32.ascii * to true to default to the ASCII mappings. */ public interface W32API extends StdCallLibrary, W32Errors { /** Standard options to use the unicode version of a w32 API. */ Map UNICODE_OPTIONS = new HashMap() { { put(OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE); put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.UNICODE); } }; /** Standard options to use the ASCII/MBCS version of a w32 API. */ Map ASCII_OPTIONS = new HashMap() { { put(OPTION_TYPE_MAPPER, W32APITypeMapper.ASCII); put(OPTION_FUNCTION_MAPPER, W32APIFunctionMapper.ASCII); } }; Map DEFAULT_OPTIONS = Boolean.getBoolean("w32.ascii") ? ASCII_OPTIONS : UNICODE_OPTIONS; public class HANDLE extends PointerType { @Override public Object fromNative(Object nativeValue, FromNativeContext context) { Object o = super.fromNative(nativeValue, context); if (INVALID_HANDLE_VALUE.equals(o)) return INVALID_HANDLE_VALUE; return o; } } /** Constant value representing an invalid HANDLE. */ HANDLE INVALID_HANDLE_VALUE = new HANDLE() { { super.setPointer(Pointer.createConstant(-1)); } @Override public void setPointer(Pointer p) { throw new UnsupportedOperationException("Immutable reference"); } }; }

W32Errors.java

/* Copyright (c) 2007 Timothy Wall, All Rights Reserved * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ public interface W32Errors { int NO_ERROR = 0; int ERROR_INVALID_FUNCTION = 1; int ERROR_FILE_NOT_FOUND = 2; int ERROR_PATH_NOT_FOUND = 3; }
转载请注明原文地址: https://www.6miu.com/read-72194.html

最新回复(0)