I have a java application ( index.java
) that if I execute it I navigate through its different JFrame
or JDialog
without problems, etc.
My doubt/question/query is: is there any method to not allow opening during the process of index.java
another index.java
? And in case of trying to reopen it (if it is open) show a message: "It is in use".
I hope I have explained myself.
Code:
public class Index extends javax.swing.JFrame {
public Index() {
initComponents();
//Fondo del JFrame en color gris.
getContentPane().setBackground(Color.gray);
//Código para confirmar el cierre de la ventana principal.
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent evt){
if (JOptionPane.showConfirmDialog(rootPane, "¿Desea salir de la aplicación?", "Salir de la aplicación",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
System.exit(0);
}
});
//Situamos el JDialog en el centro de la pantalla.
setLocationRelativeTo(null);
//No redimensionable.
setResizable(false);
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Index.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Index.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Index.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Index.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Index().setVisible(true);
}
});
}
}
Not directly from the java side, if the instances run in two different virtual machines. However you can fix the issue with a lock file.
in the main method you start with:
An alternative to simply avoiding a second instance of your application would be to use a
ServerSocket
to guarantee on the side that there aren't two instances running, and also pass arguments from subsequent launches to the same application:Pseudo code idea
I don't have java right now but I could find the following example on this page :
The following text is not created by me you can visit the source here
example about a usage: http://www.w3api.com/wiki/Java:FileChannel.tryLock()
hello you can do it using the junique-1.0.4 library The JUnique library can be used to prevent a user from running more instances of the same Java application at the same time. JUnique implements shared locks and communication channels between all JVM instances launched by the same user. you can search the internet how to use it.