document.write("");

windows jdk 1.8 获取 进程ID (ProcessBuilder)

1. 功能代码:

import lombok.extern.slf4j.Slf4j;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.Pointer;
/**
 * Project:*******
 * Date:2025/12/1
 * Time:17:06
 * Description:TODO
 *
 * @author *****
 * @version 1.0
 */
@Slf4j
public class TestI {
 
    public static void main(String[] args) {
        try {
            // 使用 ping 命令持续运行(Windows 环境)
            ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", "ping -t 127.0.0.1");
            Process process = processBuilder.start();

            long processId = getProcessId(process);
            log.info("Process ID: " + processId);
            log.info("检查进程信息的命令 wmic process where processid=" + processId + " get CommandLine");
            // 让主线程等待一段时间,以便观察进程是否存活
            Thread.sleep(30000); // 等待 30 秒
            // 手动终止进程(可选)
            process.destroy();
            log.info("进程已手动终止");
 
        } catch (Exception e) {
            log.error("Error", e);
        }
    }
    /**
     * 获取进程ID (兼容JDK 1.8)
     * @param process 进程对象
     * @return 进程ID
     */
    public static long getProcessId(Process process) {
        try {
            // 使用反射获取JDK 9+中的pid方法(如果有)
            java.lang.reflect.Method pidMethod = process.getClass().getMethod("pid");
            return (long) pidMethod.invoke(process);
        } catch (Exception e) {
            // JDK 1.8及更早版本的处理方式
            String className = process.getClass().getName();
            if (className.equals("java.lang.UNIXProcess")) {
                try {
                    java.lang.reflect.Field pidField = process.getClass().getDeclaredField("pid");
                    pidField.setAccessible(true);
                    return pidField.getLong(process);
                } catch (Exception unixException) {
                    log.error("无法从UNIXProcess获取PID", unixException);
                }
            } else if (className.equals("java.lang.ProcessImpl")) {

                try {
                    // 对于Windows,我们可以通过handle获取PID,但需要额外的JNI调用
                    java.lang.reflect.Field handleField = process.getClass().getDeclaredField("handle");
                    handleField.setAccessible(true);
                    // 获取windows 句柄
                    long handle = handleField.getLong(process);
                    Kernel32 kernel = Kernel32.INSTANCE;
 
                    WinNT.HANDLE winntHandle = new WinNT.HANDLE();
 
                    winntHandle.setPointer(Pointer.createConstant(handle));

                    return kernel.GetProcessId(winntHandle);
                } catch (Exception winException) {
                    log.error("无法从ProcessImpl获取进程ID", winException);
                }
            }
            // 如果无法通过反射获取PID,返回默认值-1
            return -1;
        }
    }
}

  

2. 需引入依赖

        <!-- JNA 核心库 -->
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>5.13.0</version>
        </dependency>

        <!-- JNA 平台扩展(用于 Windows API) -->
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna-platform</artifactId>
            <version>5.13.0</version>
        </dependency>

  

3. 可自行指定仓库

<repositories>
        <repository>
            <id>dzh-public</id>
            <name>dzh maven</name>
            <url>https://maven.aliyun.com/repository/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>dzh-public-plugin</id>
            <name>dzh nexus plugin</name>
            <url>https://maven.aliyun.com/repository/public/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

  

 

posted @ 2025-12-02 11:25  人间春风意  阅读(6)  评论(0)    收藏  举报