如何从Java获取Windows进程说明? [英] How to get Windows process Description from Java?

查看:138
本文介绍了如何从Java获取Windows进程说明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是获取Windows中当前正在运行的进程列表的代码.

Here is the code to get list of currently running process in windows.

  import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.Tlhelp32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.win32.W32APIOptions;
import com.sun.jna.Native; 

public class ListProcesses {
    public static void main(String[] args) {
        Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);

        Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();          
      WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
        try  {
            while (kernel32.Process32Next(snapshot, processEntry)) {             
                System.out.println(processEntry.th32ProcessID + "\t" + Native.toString(processEntry.szExeFile)+"\t"+processEntry.readField(""));
            }

        }

        finally {
            kernel32.CloseHandle(snapshot);
        }
    } 
}

但是我无法在输出中获得流程/服务的描述.请提供解决方案以获取每个运行过程的流程描述.预先感谢.

But I am unable to get description of the process/service in output.Kindly provide solution to get process description of each running proceess. Thanks in advance.

推荐答案

除了加载和调用Kernel32之外,您还可以在使用Runtime来执行本机进程的Windows中简单地使用以下代码片段:

Instead of loading and invoking Kernel32 you could simply use the following code snippet in windows which uses the Runtime to execute a native process:

public List<String> execCommand(String ... command)
{
    try 
    {
        // execute the desired command
        Process proc = null;
        if (command.length > 1)
            proc = Runtime.getRuntime().exec(command);
        else
            proc = Runtime.getRuntime().exec(command[0]);

        // process the response
        String line = "";
        List<String> output = new ArrayList<>();
        try (BufferedReader input = new BufferedReader(new InputStreamReader(proc.getInputStream())))
        {
            while ((line = input.readLine()) != null) 
            {
                output.add(line);
            }
        }
        return output;
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return Collections.<String>emptyList();
}

,然后执行调用 Windows管理信息命令行的命令

List<String> output = execCommand("wmic.exe PROCESS where name='"+processName+"'");

processName应该包含您尝试从中获取信息的正在运行的应用程序或exe的名称.

processName should contain the name of the running application or exe you try to get information from.

然后,返回的列表将包含正在运行的应用程序的状态信息的行输出.第一个条目将包含各个字段的标题信息,而随后的条目将包含有关所有匹配进程名称的信息.

The returned list will then contain the line output of the status information of the running application. The first entry will contain header-information for the respective fields while the following entries will contain information on all matching process names.

有关WMIC的更多信息:

Further infos on WMIC:

  • MSDN
  • Product documentation
  • Generating HTML output for WMI

HTH

这篇关于如何从Java获取Windows进程说明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆