0

I wrote a java swing application and it has some simple specifications. These are the specifications:

  1. From very beginning of the application it has a privilege to add to the system tray.
  2. The application never exit when Main JFrame is closed. It means, it is still running.

Now my problem is how to check it is still running or not and if it is running, how could I run Timer inside it?

Do you, Java experts have any ideas about this problem?

Terance Wijesuriya
  • 1,928
  • 9
  • 31
  • 61
  • 2
    If your swing application is the only java application running on that host and if you are running on windows you can check in the task manager or with `ps` on unix/linux system's if there is a java process that is running. You can also launch jconsole on the host and see what are the local java process apart from jconsole itself. – redoc Apr 29 '16 at 10:50
  • 2
    You could use a file lock (check file existing and create on start), but it is not reliable when the app crashes. Unfortunately there is no simple way to check if the app is running without using some OS-specific calls. http://stackoverflow.com/questions/660205/start-java-program-only-if-not-already-running - here are some examples. – ike3 Apr 29 '16 at 10:53
  • @redoc :- I want do this with java code. – Terance Wijesuriya Apr 29 '16 at 11:00
  • 1
    You might try using the inbuilt functionality of the [`SingleInstanceService` of the JNLP API](https://docs.oracle.com/javase/7/docs/technotes/guides/javaws/developersguide/examples.html#SingleInstanceService) (available to desktop apps. launched using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info)). OTOH I don't know how well JWS would integrate with the requirement for entry in the system tray.. – Andrew Thompson Apr 29 '16 at 11:19
  • 2
    *"Can you provide me an example code ?"* Did you follow the link? More generally, don't ask for code or examples on SO. It is a Q&A Site, not a help desk. – Andrew Thompson Apr 29 '16 at 11:22
  • What do you mean by checking from javacode if swing application is still running? Do you want run 2 applications? – tak3shi Apr 29 '16 at 11:24
  • No I want run only one application.@tak3shi – Terance Wijesuriya Apr 29 '16 at 11:26
  • Well, then what do you want check? If a thread is still working or what? – tak3shi Apr 29 '16 at 11:27
  • 2
    If you only want a "single" instance of the application to ever be running there are a number of things you could do. You could open a socket on a specific port, if you can do this, then no other instances of the application are running; you could use a lock file, which if present, means another instance of the application is running – MadProgrammer Apr 29 '16 at 11:34
  • @tak3shi : I want to check the application is still running on background . – Terance Wijesuriya Apr 29 '16 at 11:35
  • I have added some specific thing on my post . Please look at it ? – Terance Wijesuriya Apr 29 '16 at 12:03
  • Start a new Thread and use Thread.sleep method. – tak3shi Apr 29 '16 at 12:11

0 Answers0