Java. Prevent Jar App from multiple running with pop up message "already running". (using ServerSocket()) -


currently trying display pop message when user trying run jar more 1 time. current code below

public static void main(string[] args){    new serversocket(65535, 1, inetaddress.getlocalhost());// if there 1                                                           // running jar, prevent                                                           // program execute    joptionpane.showmessagedialog(frame, "hello world \n"); //display hello world message                                                            // when run  } 

expectation output when user run jar more 1 time :

your jar application running . . . 

my question is, how can display message tell user jar application running because serversocket() prevent application running, "already running "pop message put after not run.

answer:

 try{        new serversocket(65535, 1, inetaddress.getlocalhost());        joptionpane.showmessagedialog(frame, "hello world \n");     }  catch(bindexception ex){         joptionpane.showmessagedialog              (frame, "your jar application running . . . \n");         system.exit(0);     } 

you need catch bindexception, , display 'already running' popup, , exit, in catch block.

you need catch other ioexceptions may arise.

you need store serversocket static variable won't garbage-collected, close it.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -