简体   繁体   English

Java jar 只运行一次

[英]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: NewJFrame 主要:

   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见: http : //i.stack.imgur.com/5LPjc.png

My English bad sorry.我的英语不好抱歉。

I solved :D我解决了:D

NewJFrame main change it: NewJFrame 主要改变它:

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");
                }
            }
        });
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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