2

I have made my java application as Single instance application. I have implemented File Lock system for the same.

If the application is already running, I want to show the running application to front. How do I achieve that? How can I acess the running process of that application and show it?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Tvd
  • 4,463
  • 18
  • 79
  • 125
  • 1
    possible duplicate of [Restrict multiple instances of an application in java](http://stackoverflow.com/questions/6134694/restrict-multiple-instances-of-an-application-in-java) – Harry Joy Dec 09 '11 at 13:23

2 Answers2

1

What you are asking for is OS dependent but you can always have your own implementation to do this. You application can listen on a certain port for a bring-to-front command that you can send from the second instance of your application.

void main(String[] args){
   if(applicationAlreadyRunning){
     // Send bring-to-front message to running instance on a known port
     // and exit.
   }
}

When a bring-to-front message is received you can do:

public void BringToFrontCommandReceived(){
    java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    myMainFrame.toFront();
                    myMainFrame.repaint();
                }
            });

}
GETah
  • 20,922
  • 7
  • 61
  • 103
  • *"listen on a certain port"* Of course, if using sockets for 'to front', you might also drop the entire file lock and simply check for a listener on that port as the 'have I already run?' check. – Andrew Thompson Dec 10 '11 at 02:15
1

JWS not only provides an x-plat SingleInstanceService, but it also (from memory) pops the application toFront() on newActivation(String[]). Of course if it is not automatic, you can call it explicitly.

Here is a demo. of the SIS.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433