26

I'm not a java expert or a eclipse expert. Currently I'm working on a project and i need to debug/test often. I use the eclipse run Button for it. But when I don't close the Program eclipse/java opens it again(second window). It's an gui app with swing jframe.

Is there a function in eclipse or java to terminate the previous build and opens the new one when you run it?

arnoapp
  • 2,416
  • 4
  • 38
  • 70
  • 2
    There is the stop button with a red square image. Click on it to stop/kill the application. – Luiggi Mendoza Dec 08 '12 at 15:34
  • 3
    @LuiggiMendoza That only stops the current process. You need to terminate the java process via task manager (at least, this is how I've always done it). But frankly, just add an onclose to your window: `frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)` – FThompson Dec 08 '12 at 15:36
  • 1
    @Vulcan it worked for me even with multi threaded applications that I run on Eclipse (it even works to stop/kill the web application server). – Luiggi Mendoza Dec 08 '12 at 15:37
  • @LuiggiMendoza This isn't about multithreaded applications, this is about running multiple applications simultaneously. – FThompson Dec 08 '12 at 15:38
  • 2
    I know the stop button and its exit_on_close. But I would like to do this automatically when press run in eclipse(if it's possible) – arnoapp Dec 08 '12 at 15:40
  • Following is a related post.May be it will help you :- [1]: http://stackoverflow.com/questions/8630627/how-to-restrict-eclipse-rcp-application-to-a-single-instance – Richa Jun 11 '13 at 08:49

7 Answers7

14

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

[X] Terminate and Relaunch while launching

Terminate and Relaunch while launching

Robert Mikes
  • 1,179
  • 8
  • 19
8

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).

Community
  • 1
  • 1
Vishal Dhawani
  • 267
  • 4
  • 7
  • 1) works but unfortunately I cant set the same debug key to terminate&relaunch, just have to remember hit the right key :( hehe, if we could algorithmically macro eclipse commands it could work I think – Aquarius Power Feb 02 '15 at 19:24
4

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: Console view

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

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.

Community
  • 1
  • 1
2

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

before launching, press ctrl-F2.

This shortcut terminates the currently running/debugged application.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
1

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

Community
  • 1
  • 1
user1889970
  • 726
  • 1
  • 8
  • 12
1

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();
    }
}
Dash
  • 11
  • 1
  • I actually prefer your approach for my usecases, I have created a gist [Highlander](https://gist.github.com/hrgdavor/d81a231c274e225447117902a633bdb9) with same logic but using TCP port (it adds only 10-15 ms delay to an app). I like this because I just press F11 or CTRL+F11 to run latest app. I got used to it so much with run-jetty plugin – Davor Hrg Mar 10 '17 at 12:22
-3

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

judge
  • 15
  • 4