I am currently trying to display a pop-up message when a user tries to run the jar more than once. My current code is below
public static void main(String[] args){
new ServerSocket(65535, 1, InetAddress.getLocalHost());
JOptionPane.showMessageDialog(frame, "Hello World \n");
}
expected output when user runs jar more than once:
Your jar application is already running . . .
My question is how can we display a message telling the user that the jar application is already running, because ServerSocket () will prevent the application from starting, so the message “already running” that I installed after that will not work.
ANSWER:
try{
new ServerSocket(65535, 1, InetAddress.getLocalHost());
JOptionPane.showMessageDialog(frame, "Hello World \n");
}
catch(BindException ex){
JOptionPane.showMessageDialog
(frame, "Your jar application is already running . . . \n");
System.exit(0);
}
source
share