简体   繁体   中英

Java jar run only once

Singleton using:

    public class Singleton {

       private static Singleton singleton = new Singleton( );

       /* A private Constructor prevents any other 
        * class from instantiating.
        */
       private Singleton(){ }

       /* Static 'instance' method */
       public static Singleton getInstance( ) {
      return singleton;
   }
   /* Other methods protected by singleton-ness */
   protected static void demoMethod( ) {
       new NewJFrame().setVisible(true);
   }
}

NewJFrame main:

   public static void main(String args[]) { 
        /* Set the Nimbus look and feel */
         * For details see ht
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
//                new NewJFrame().setVisible(true);
                Singleton tmp = Singleton.getInstance( );
                tmp.demoMethod();
            }
        });
       }

I want one jar.

Not want another jar. See: http://i.stack.imgur.com/5LPjc.png

My English bad sorry.

I solved :D

NewJFrame main change it:

public static void main(String args[]) { 
        /* Set the Nimbus look and feel */
         * For details see ht
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
//              new NewJFrame().setVisible(true);
                ServerSocket once;
                Singleton singleton;

                try {
                    once = new ServerSocket(1334);
                    Singleton.getInstance().demoMethod();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(null, "Already Open");
                }
            }
        });
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM