0

I would like to check whether a jar of mine is running on the users system, to then relaunch if it is closed.

I am aware of the command jps -l which makes it possible to check the current running jars. Only problem is that for that line to work, it requires the user to have a JDK installed. So I was then wondering whether or not there is an equivalent to the jps -l line, which doesn't need a JDK or anything, but just checks whether a specific jar is running.

In the past I have also used the line cmd /c tasklist for Windows and the line top -F -R -o cpu for Mac. To check whether an app or exe was running, but that doesn't really seem to be working. When running the tasklist line on Windows and I then check for an exe called "myApp", it doesn't find it. Even though it might be running. Otherwise this would have been a perfect method, to check for a running app, exe or jar.

Here is an example code of how I tried to use the tasklist command to check for a specific executable.

try {
            String procss;
            Process pRun = Runtime.getRuntime().exec("cmd /c tasklist");
            BufferedReader input = new BufferedReader(new InputStreamReader(pRun.getInputStream()));
            while ((procss = input.readLine()) != null) {
                if(!procss.contains("myApp"))
                {
                    //Runtime command to launch exe or app.
                }
            }
            input.close();
        } catch (Exception err) {
            err.printStackTrace();
        }

Basically I would like to just edit the code above, to have a command line, of which is able to actually check whether the exe, app or jar is running. Maybe there is an alternative to cmd /c tasklist and top -F -R -o cpu, which is able to get all processes running on a pc and not just .exe or .app

1 Answers1

4

On windows, you could use the wmic command to get the command line parameters a program was launched with.

For example, using wmic process where "name like '%java%'" get commandline,processid (basically just means "get the PID and command line arguments of process with a name like java") gives me this output for a test program:

616

"C:\Program Files\Java\jre1.8.0_111\bin\javaw.exe" -jar "A:\Programmering\Java\Pong\out\artifacts\Pong_jar\Pong.jar"

As you can see, you can get the location of the jar file which is running (which you could then use to check if it's your program). In this case, I just launched it by double clicking the jar file, you may get different outputs if you launch it in a different way, but there should always be something you can use to identify the java process (like a main class or jar file).

Community
  • 1
  • 1
Alvin L-B
  • 488
  • 2
  • 8
  • You input is very much appreciated, I am going to take a look at it and try to work it into my code. I was wondering whether you know a similar method for Mac as well? My program will be running on both Windows and Mac. –  Mar 11 '17 at 15:13
  • I'm not much of a mac guy, but a quick google search brought up this. Might be something to try?http://stackoverflow.com/questions/821837/how-to-get-the-command-line-args-passed-to-a-running-process-on-unix-linux-syste – Alvin L-B Mar 11 '17 at 15:39
  • Thank you very much, your own command method worked perfectly for Windows and for Mac I managed to create a workaround using the link you provided. –  Mar 11 '17 at 17:02