Skip to content

Commit 1992953

Browse files
author
Thidas Senavirathna
committed
[FEATURE] Tasklist Command #68
1 parent 602f187 commit 1992953

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/main/java/com/mycmd/App.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,6 @@ private static void registerCommands(Map<String, Command> commands) {
102102
commands.put("clearhistory", new ClearHistoryCommand());
103103
commands.put("alias", new AliasCommand());
104104
commands.put("unalias", new UnaliasCommand());
105+
commands.put("tasklist", new TasklistCommand());
105106
}
106107
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.Command;
4+
import com.mycmd.ShellContext;
5+
6+
import java.io.IOException;
7+
import java.lang.management.ManagementFactory;
8+
import java.lang.management.RuntimeMXBean;
9+
10+
/**
11+
* TasklistCommand - displays information about running processes.
12+
* Usage: tasklist
13+
*
14+
* Notes:
15+
* - Shows Java process information and system processes if available
16+
* - Limited to what Java can access without platform-specific tools
17+
*/
18+
public class TasklistCommand implements Command {
19+
20+
@Override
21+
public void execute(String[] args, ShellContext context) throws IOException {
22+
System.out.println();
23+
System.out.printf("%-40s %10s %15s%n", "Image Name", "PID", "Memory Usage");
24+
System.out.println("=" .repeat(70));
25+
26+
// Get current Java process information
27+
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
28+
String jvmName = runtimeMXBean.getName();
29+
String[] parts = jvmName.split("@");
30+
String pid = parts.length > 0 ? parts[0] : "Unknown";
31+
32+
// Get memory info
33+
Runtime runtime = Runtime.getRuntime();
34+
long usedMemory = (runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024); // MB
35+
36+
System.out.printf("%-40s %10s %12s MB%n", "java.exe", pid, usedMemory);
37+
38+
// Try to get system processes using ProcessHandle (Java 9+)
39+
try {
40+
ProcessHandle.allProcesses()
41+
.limit(20) // Limit to first 20 processes
42+
.forEach(process -> {
43+
ProcessHandle.Info info = process.info();
44+
long processPid = process.pid();
45+
String command = info.command().orElse("Unknown");
46+
47+
// Extract just the executable name from full path
48+
String execName = command;
49+
if (command.contains("/") || command.contains("\\")) {
50+
int lastSlash = Math.max(command.lastIndexOf('/'), command.lastIndexOf('\\'));
51+
execName = command.substring(lastSlash + 1);
52+
}
53+
54+
// Truncate long names
55+
if (execName.length() > 40) {
56+
execName = execName.substring(0, 37) + "...";
57+
}
58+
59+
System.out.printf("%-40s %10d %15s%n", execName, processPid, "N/A");
60+
});
61+
} catch (Exception e) {
62+
// ProcessHandle not available or error accessing processes
63+
System.out.println("\n[Additional process information not available]");
64+
}
65+
66+
System.out.println();
67+
}
68+
69+
@Override
70+
public String description() {
71+
return "Display a list of currently running processes.";
72+
}
73+
74+
@Override
75+
public String usage() {
76+
return "tasklist";
77+
}
78+
}

0 commit comments

Comments
 (0)