How to implement a single instance Java application?

后端 未结 17 986
北荒
北荒 2020-11-22 04:32

Sometime I see many application such as msn, windows media player etc that are single instance applications (when user executes while application is running a new applicatio

17条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 04:34

    
    public class SingleInstance {
        public static final String LOCK = System.getProperty("user.home") + File.separator + "test.lock";
        public static final String PIPE = System.getProperty("user.home") + File.separator + "test.pipe";
        private static JFrame frame = null;
    
        public static void main(String[] args) {
            try {
                FileChannel lockChannel = new RandomAccessFile(LOCK, "rw").getChannel();
                FileLock flk = null; 
                try {
                    flk = lockChannel.tryLock();
                } catch(Throwable t) {
                    t.printStackTrace();
                }
                if (flk == null || !flk.isValid()) {
                    System.out.println("alread running, leaving a message to pipe and quitting...");
                    FileChannel pipeChannel = null;
                    try {
                        pipeChannel = new RandomAccessFile(PIPE, "rw").getChannel();
                        MappedByteBuffer bb = pipeChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1);
                        bb.put(0, (byte)1);
                        bb.force();
                    } catch (Throwable t) {
                        t.printStackTrace();
                    } finally {
                        if (pipeChannel != null) {
                            try {
                                pipeChannel.close();
                            } catch (Throwable t) {
                                t.printStackTrace();
                            }
                        } 
                    }
                    System.exit(0);
                }
                //We do not release the lock and close the channel here, 
                //  which will be done after the application crashes or closes normally. 
                SwingUtilities.invokeLater(
                    new Runnable() {
                        public void run() {
                            createAndShowGUI();
                        }
                    }
                );
    
                FileChannel pipeChannel = null;
                try {
                    pipeChannel = new RandomAccessFile(PIPE, "rw").getChannel();
                    MappedByteBuffer bb = pipeChannel.map(FileChannel.MapMode.READ_WRITE, 0, 1);
                    while (true) {
                        byte b = bb.get(0);
                        if (b > 0) {
                            bb.put(0, (byte)0);
                            bb.force();
                            SwingUtilities.invokeLater(
                                new Runnable() {
                                    public void run() {
                                        frame.setExtendedState(JFrame.NORMAL);
                                        frame.setAlwaysOnTop(true);
                                        frame.toFront();
                                        frame.setAlwaysOnTop(false);
                                    }
                                }
                            );
                        }
                        Thread.sleep(1000);
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                } finally {
                    if (pipeChannel != null) {
                        try {
                            pipeChannel.close();
                        } catch (Throwable t) {
                            t.printStackTrace();
                        } 
                    } 
                }
            } catch(Throwable t) {
                t.printStackTrace();
            } 
        }
    
        public static void createAndShowGUI() {
    
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 650);
            frame.getContentPane().add(new JLabel("MAIN WINDOW", 
                        SwingConstants.CENTER), BorderLayout.CENTER);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    
    

提交回复
热议问题