Hi,
Im currently in a situation to get all the application name that is currently running in windows but i can get only the processes that is currently running using the following code

Process p = Runtime.getRuntime().exec("tasklist.exe /v ");

Can anyone help me out in getting the application name...
Thanks in advance.

Dani AI

Generated

If by "application name" you mean the user-visible window title of each GUI app, is on the right track. One tweak that makes parsing much more reliable is to ask tasklist for CSV and filter to only rows that actually have a window. Then parse by header name instead of hard-coded offsets:

Process p = new ProcessBuilder(
    "cmd.exe", "/c",
    "tasklist /v /fo csv /fi \"WINDOWTITLE ne N/A\""
).redirectErrorStream(true).start();

try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
    String header = br.readLine();
    String[] h = header.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
    int nameIdx = -1, titleIdx = -1;
    for (int i = 0; i < h.length; i++) {
        String col = h[i].replaceAll("^\"|\"$", "");
        if (col.equalsIgnoreCase("Image Name")) nameIdx = i;
        if (col.equalsIgnoreCase("Window Title")) titleIdx = i;
    }
    for (String line; (line = br.readLine()) != null; ) {
        String[] c = line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
        String image = c[nameIdx].replaceAll("^\"|\"$", "");
        String title = c[titleIdx].replaceAll("^\"|\"$", "");
        System.out.printf("%s -> %s%n", image, title);
    }
}

Notes: background processes will not appear (no window), and titles can change at runtime. See Microsoft’s tasklist reference for the /v, /fo csv, and WINDOWTITLE filter options. tasklist documentation

If instead you want a friendly product name (e.g., "Microsoft Word" vs. WINWORD.EXE), you can have Java spawn PowerShell and enrich each titled process with the file’s version resource: Get-Process | Where-Object {$_.MainWindowTitle} | ForEach-Object { try { $p=$_; $path=$p.MainModule.FileName } catch {} ; $desc = if ($path) { (Get-Item $path).VersionInfo.FileDescription } else { $p.ProcessName } ; [pscustomobject]@{ PID=$p.Id; Title=$p.MainWindowTitle; Product=$desc } } | ConvertTo-Csv -NoTypeInformation. Accessing module metadata for processes you do not own may require an elevated console. Get-Process documentation FileVersionInfo class (FileDescription)

For maximum control without spawning shells, a Java/JNA approach can enumerate top-level windows and read their titles via the Win32 API (EnumWindows + GetWindowText). EnumWindows GetWindowText

Recommended Answers

All 4 Replies

You need to parse the output of the tasklist, something like this:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class AppList {
	
	public static void main(String[] args) {
		try {
			Process p = Runtime.getRuntime().exec("tasklist.exe /FO LIST");
			BufferedReader in
			   = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String str = in.readLine();
			while (str!=null) {
				if (str.startsWith("Image Name:")) {
					String appName = str.substring(11).trim();
					System.out.println(appName);
				}				
				str = in.readLine();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

Thank for ur reply.but im getting only the exe files and im need of the application anyother way to get the application name.

Thank for ur reply.but im getting only the exe files and im need of the application anyother way to get the application name.

What do you mean by application name? If it is the Window Title, then you can change lines:
Line 10:

Process p = Runtime.getRuntime().exec("tasklist.exe /v /FO LIST");

Line 15-16:

if (str.startsWith("Window Title:")) {
String appName = str.substring(13).trim();

Hi zloiadun,
Thanks for ur help and i got the window title for the currently running applications and thank u very much for ur idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.