1

Hello I'm working on a program which uses JNA 4.5.1. I need to know whether a specific program is running or not. Here is my problem:

hwnd = User32.INSTANCE.FindWindow
       (null, "Session Dev/Prod - [32 x 80]");
if (hwnd == null) {
    System.out.println("Session Dev/Prod is not running");
    Process p = null;
    try {
        p = Runtime.getRuntime()
                .exec("rundll32 
                 url.dll,FileProtocolHandler C: 
                 /ProgramData/Microsoft/Windows/Start 
                 Menu/Programs/IBM Personal 
                 Communications/TNHost");
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        p.waitFor();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}
else{

    System.out.println("Host Already open");
    User32.INSTANCE.ShowWindow(hwnd, User32.SW_MAXIMIZE );        
    User32.INSTANCE.SetForegroundWindow(hwnd);
}

The Problem is that the Window-Title changes depending on the monitor size.

hwnd = User32.INSTANCE.FindWindow(null, "Session Dev/Prod - [32 x 80]");

The title is always "Session Dev/Prod" + the size which changes.

I need to find the window which starts with "Session Dev/Prod". Does anyone know how to do this. Or is there an other way to find out whether a program is running or not? I've tried to do it with Regex as parameter but the function accepts just a String.

Thank you

k_ssb
  • 6,024
  • 23
  • 47
  • 2
    Possible duplicate of [How to get list of all window handles in Java (Using JNA)?](https://stackoverflow.com/questions/8717999/how-to-get-list-of-all-window-handles-in-java-using-jna) – jhamon May 16 '18 at 09:21
  • Actually a duplicate of this post [https://stackoverflow.com/questions/27839666/java-jna-find-partial-window-title](https://stackoverflow.com/questions/27839666/java-jna-find-partial-window-title) but it just redirects to the other one – jhamon May 16 '18 at 09:22

1 Answers1

0

I had the task once to check whether a program was running or not (and if so kill it) and solved it like this:

public static void isProcessRunning(String processName) throws IOException, InterruptedException {
 ProcessBuilder processBuilder = new ProcessBuilder("tasklist.exe");
 Process process = processBuilder.start();
 // handle errors: process.getErrorStream();

 BufferedReader reader = new BufferedReader(new  InputStreamReader(process.getInputStream()));
 String line;
 while ((line = reader.readLine()) != null) {
  if (line.startsWith(processName)) {
    System.out.println("Yes program is running");
    break;
  }
  // else {
  // System.out.println("No program is not running");
  // }
}

}

To find out the name of your task call tasklist in the commandline and look for the name. If it is really 'Session Dev/Prod - [32 x 80]' then you can use 'Session Dev/Prod' as a string... But note that this is a windows solution. For linux you have to use something like ps -ef

Lonzak
  • 9,334
  • 5
  • 57
  • 88
  • @romantenger You are welcome. Please also see http://stackoverflow.com/help/someone-answers – Lonzak May 16 '18 at 14:29