0

I am working on a program, this program needs to be able to see if the "finder" for Mac or "File Explorer" for Windows has been opened. I am trying to do this by using a tutorial. This code I have now gotten, does seem to work for listing all the processes running.

But the issue is that it is not the current running ones, meaning it will list processes of which have been running at 2 pm even though it is 3 am or alike. Which means if I were to check if the listed processes contain the Finder or the File Explorer it would return true no matter what as it is like a system app which runs all the time and can't be closed like a regular downloaded .exe, the only way to kind of close it is by restarting the finder or explorer. This does initially make the process list update with the finder or explorer in it. But usually when opening the finder or explorer as a user, you don't really restart the app every single time, you just open the explorer or the finder, which doesn't trigger a new runtime process which kind of makes it a bit difficult to check if it has just been opened or not.

I would like to know if there is a way of modifying my code to make it possible to check if the process of finder or explorer has just been launched. It only has to be triggered when it has just been launched and not if it is already running. I have listed the code of which I am using below here.

package readPList;

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

public class MyProcReader {
    public static void main(String[] args) {
        try {
            String process;

            Process p = Runtime.getRuntime().exec("ps -few");
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((process = input.readLine()) != null) {
                System.out.println(process); 

            }
            input.close();
        } catch (Exception err) {
            err.printStackTrace();
        }
    }
}

For mac I use the line "ps -few" in the "getRuntime()" line and for Windows I use the line System.getenv("windir") +"\\system32\\"+"tasklist.exe"

2 Answers2

0

For Mac, Another way I suggest is to use AppleScript which allows you to check if an application is running. You can integrate Applescript in java.

Please find in following links: check if application is running in applescript, see the update section of answer.

Community
  • 1
  • 1
py563
  • 15
  • 5
  • I appreciate the input, I am going to check it out. Is it like a plug-in, like an extended library or do I have to install something specific, to make the Applescript "reachable" by Java? –  Jan 28 '17 at 01:34
  • No, AppleScript [link](https://en.wikipedia.org/wiki/AppleScript) is a scripting language provided by Apple more like VBA Scripts in Windows. Please do upvote answer if it seems useful after your research. Thanks – py563 Jan 28 '17 at 02:06
0

The new Java 9 provides ProcessHandle API (which does a native call):

ProcessHandle.allProcesses().forEach(p -> System.out.println(p.pid()));
user218046
  • 623
  • 6
  • 20