Is there a way using Java that I can gain a list of all active processes running on a Mac?
I can do so in Windows using the code below to return the Task List, but that throws an exception on a Mac. I want my app to stop if certain applications are also running.
Any ideas? Thanks.
Windows Code:
Process p = Runtime.getRuntime().exec("tasklist.exe /nh");
BufferedReader input = new BufferedReader
(new InputStreamReader(p.getInputStream()));
//while there are more processes in the task manager list
while ((line = input.readLine()) != null) {
//insert code here for each task running
}
String line;
String sysUserName=System.getProperty("user.name");
Process p = Runtime.getRuntime().exec("tasklist /fi \"username eq"+sysUserName+"\""); // for windows
// Process p = Runtime.getRuntime().exec("ps -u "+sysUserName+""); // for mac
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line); //<-- Parse data here.
}
input.close();
tasklist.exe does not exist on Mac. Use something like ps -eaf
Related
I am new to java and i am calling a Python script from java using processbuilder and trying read python output in java.
ProcessBuilder pb = new ProcessBuilder(Arrays.asList("python","PyScript.py",""+path));
Process p = pb.start();
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null)
{
System.out.println(line);
logger.debug("Value of python output is"+line);
System.out.println("in while loop");
}
readline is getting null. when i run through command prompt its running fine.
I am writing a java code to print current process that is on top of all process.
I write this:-
String process;
Process p = Runtime.getRuntime().exec("tasklist.exe /fo csv /nh");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((process = input.readLine()) != null) {
System.out.println(process);
}
input.close();
it prints all running processes but i want only one process that is on top of all other process and that is currently visible.
You might replace code
while ((process = input.readLine()) != null) {
System.out.println(process);
}
with this,
process = input.readLine();
System.out.println(process);
I have a function in java which is being executet on my raspberry pi and should send a signal to toggle the targeted sockets state to on / off.
Thats my current function:
public static void rcswitch(int housecode,int unitcode, int onoff) throws InterruptedException, IOException {
String housestring = Integer.toString(housecode);
String unitstring = Integer.toString(unitcode);
String onoffstring = Integer.toString(onoff);
ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", "sudo /home/pi/rcswitch-pi/send", housestring, unitstring, onoffstring);
Process proc = builder.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
}
However, it doesn't seem like the terminal is receiving the command as it does not output anything. It should show something like "command received" and execute it then. When I normally execute the /send command in the terminal it works just fine. In eclipse it just works fine and throws the expected error.
Thanks for your answers :)
It is most likely that an error has occured while executing the command. Keep in mind that Process#getInputStream() does not include standard error stream of the process. You should use Process#getErrorStream(). Something like:
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String line = null;
while((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
I'm trying to run a .bat file and get the output. I can run it but I can't get the results in Java:
String cmd = "cmd /c start C:\\workspace\\temp.bat";
Runtime r = Runtime.getRuntime();
Process pr = r.exec(cmd);
BufferedReader stdInput = new BufferedReader(
new InputStreamReader( pr.getInputStream() ));
String s ;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
The result is null. No idea why I get this. Note that I'm using Windows 7.
Using "cmd /c start [...]" to run a batch file will create a sub process instead of running your batch file directly.
Thus, you won't have access to its output.
To make it work, you should use:
String cmd = "C:\\workspace\\temp.bat";
It works under Windows XP.
You need to start a new thread that would read terminal output stream and copy it to the console, after you call process.waitFor().
Do something like:
String line;
Process p = Runtime.getRuntime().exec(...);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
input.close();
Better approach will be to use the ProcessBuilder class, and try writing something like:
ProcessBuilder builder = new ProcessBuilder("/bin/bash");
builder.redirectInput();
Process process = builder.start();
while ((line = reader.readLine ()) != null) {
System.out.println ("Stdout: " + line);
}
BufferedReader stdInput = new BufferedReader(new
InputStreamReader( pr.getErrorStream() ));
instead use
BufferedReader stdInput = new BufferedReader(new
InputStreamReader( pr.getInputStream ));
Okay, I have tried a dozen different ways and no success. I want to execute a custom exe and grab the output. It runs fine from the command prompt. I get the "dir" to work fine, but not custom.exe. Here is the code:
List<String> command = new ArrayList<String>();
command.add("cmd"); // Even removed these two lines
command.add("/c"); // aka hail mary coding.
//command.add("dir");
command.add("custom.exe"); // even tried "c://custom.exe"
String line;
Process p = new ProcessBuilder(command).start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
I get no output at all. If I place it in a batch file, i get output. I have a feeling it has something to do with %PATH%. Back at it...
EDIT--> So turns out that the output from this custom exe goes to error, so to see what is happening i have the code:
List<String> command = new ArrayList<String>();
command.add(System.getenv("ProgramFiles(x86)") + "\\mydir\\custom.exe";
String line;
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process p = pb.start();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line);
}
And it works like a hot damn. :)
You don't need the lines
command.add("cmd");
command.add("/c");
That would only be required for a batch file. I would rather specify the full path to the executable.
Maybe the output is on stderr? Try replacing p.getInputStream() with p.getErrorStream().