Stop Eclipse/Java running multiple instances

故事扮演 提交于 2019-12-03 22:14:18
Vishal Dhawani

Try the following ways:

  1. If you are in debug perspective, you can Right click running instance and click "Terminate and Relaunch" (You can bind short-cut for this from Window -> Preferences -> General -> Keys) This worked out well for me.

  2. This link says you can bind "Terminate & Relaunch" in any prespective. You can give it a try but the same did not work out for me.

  3. Use this plugin https://bitbucket.org/mantis78/relaunch-plugin (Not tried).

In Eclipse Neon go to Window -> Preferences -> Run/Debug -> Launching and in the Launch Operation section check:

[X] Terminate and Relaunch while launching

Vishnu Prasad Kallummel

In eclipse when you Run/Debug an application a new instance is created. There are two options:

  1. When you run an application it opens up the UI/result in the console. Hence you have to stop the existing application by click on one of these:

  2. In debug mode you have to do the same.

Or else you have to handle this in the code. Check this thread: How to restrict Eclipse-RCP application to a single instance?

Hope this is helpful.

Just to add another, fastest (for me) solution:

before launching, press ctrl-F2.

This shortcut terminates the currently running/debugged application.

user1889970

I feel the issue is not with eclipse but the way you have implemented your application. Please refer the following url for details. The above solutions though valid will be problematic if your app is run outside eclipse. As every time a user launches and closes you app a new jvm instance will be launched .

Exit on close button

Really late on this one, but hey, better late than never. Use start() when initializing everything. If you're using a JFrame, use start(Jframe frame) so it automatically handles when the program is close manually. If you're not, call onClose() when you're program is manually exited otherwise it won't start the next time.

package your.package;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;

public class ReLaunch {
    private static boolean relaunchClose = false;
    private static File runCheck = new File("run.check");
    private static File deleteCheck = new File("delete.check");

    /**
     * Use ReLaunch to stop multiple instances
     */
    public static void start() {
        //Check to see if application is already running
        if(runCheck.exists()) {
            //Mark a request to exit the existing application
            try { deleteCheck.createNewFile(); } catch (IOException e) { e.printStackTrace(); }
            //Wait until the existing application terminates itself
            while(runCheck.exists());
            //Remove the delete request so current application doesn't terminate
            if(deleteCheck.exists()) deleteCheck.delete();
        }

        //Mark that this application is currently running
        try { runCheck.createNewFile(); } catch (IOException e) { e.printStackTrace(); }

        //Creates a new thread that checks if a new application is requesting to run
        new Thread(new Runnable() {
            public void run() {
                //Wait until delete request shows up
                while(!deleteCheck.exists());
                //Proof that the application is closed to be re-launched
                relaunchClose = true;
                //Unmarks this application as running
                if(runCheck.exists()) runCheck.delete();
                //Terminated so new application can run
                System.exit(0);
            }
        }).start();
    }

    /**
     * Same as start, but automatically handles when the frame is closed by the user by marking that no applications are running
     * @param frame JFrame which needs to be closed
     */
    public static void start(JFrame frame) {
        start();
        //Listen for when the window is manually closed
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent event) {
                onClose();
            }
        });
    }

    /**
     * Marks this application as not running if it is exited manually
     */
    public static void onClose() {
         if(!relaunchClose) runCheck.delete();
    }
}

just press the stop button . or close the session by exit button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!