繁体   English   中英

Java jar 只运行一次

[英]Java jar run only once

单例使用:

    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 主要:

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

我要一罐。

不想要另一个罐子。 见: http : //i.stack.imgur.com/5LPjc.png

我的英语不好抱歉。

我解决了:D

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