簡體   English   中英

使用java獲取正在運行的應用程序的名稱

[英]Getting name of the running application with java

我認為標題解釋了問題,事實上,如果我有一個主機和一個運行應用程序的端口 ,我們如何用java確定名稱。

public String getApplicationName(String host,int port) {//some code} 

主機和運行應用程序的端口

只有操作系統才能知道現在正在運行的應用程序。 因此,無法獲得有關編寫OS specific邏輯的進程的信息。

例如,對於Windows它看起來像:

Process proc = Runtime.getRuntime().exec ("tasklist.exe");
InputStream procOutput = proc.getInputStream ();
if (0 == proc.waitFor ()) {
    // TODO scan the procOutput for your data
} 

對不起,我在之前的評論中得出結論。 我想從命令中獲取進程列表可能很有用。 再次OS特定...

try {
    String line;
    Process p = Runtime.getRuntime().exec("ps -e");
    BufferedReader input =
            new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((line = input.readLine()) != null) {
        System.out.println(line); //
//Run you pattern matcher here to parse data - host & port etc.
    }
    input.close();
} catch (Exception err) {
    err.printStackTrace();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM