How to cleanly shutdown Eclipse from Linux command line?

24,417

Solution 1

Any added ShutdownHooks (more info here) should be executed by the JVM when terminated by SIGTERM. Therefore, I think the problem is the way Eclipse is programmed to deal with such signals.

As I don't know how the cleanup process is implemented in Eclipse, I can only assume that it is not called by any ShutdownHook (and rather by an Action or something similar).

Edit: pidge has provided an answer below however which details steps which should allow you to shutdown Eclipse cleanly from the command line.

Solution 2

I figured this out with the help of gigi's answer and another question. You're going to need the wmctrl and xdotool utilities from your package manager.

Unless you're running in a terminal emulator on the same display, you need to set the right display:

$ export DISPLAY=:0.0

Then (irrelevant windows elided from example):

# List windows
$ wmctrl -l
...
0x030000fa  0 kcirb Java - Eclipse

# Tell Eclipse window to close gracefully
$ wmctrl -c eclipse

# Darn, there's a confirmation dialog
$ wmctrl -l
...
0x030000fa  0 kcirb Java - Eclipse 
0x03003c2d  0 kcirb Confirm Exit 

# Send return key to the window
$ xdotool key --window 0x03003c2d Return

Worked for me on Ubuntu 12.04, at least.

EDIT: See Scarabeetle's answer for the tweaks you need to make it work from a script.

Solution 3

Not enough reputation to comment on pidge's answer above... It almost works, but I needed to wait for some Gnome3 animation to finish and then give focus to the "Confirm Exit" window:

export DISPLAY=:0.0        # Do this in main X session
wmctrl -c "Eclipse SDK"    # Close main window
sleep 1                    # Wait for animation
wmctrl -a "Confirm Exit"   # Give focus to the dialog
# Send a Return keypress to press the OK button
xdotool key --window $(xdotool search "Confirm Exit") Return

Solution 4

Try killing java process(es). Do ps -ea | grep java

Solution 5

Did you tried with wmctrl? wmtrl -l lists the windows and wmlctrl -c -P should close the window. Anyway you could have problems with the confirmation dialog of eclipse.

Share:
24,417
Maian
Author by

Maian

Updated on July 18, 2020

Comments

  • Maian
    Maian almost 4 years

    Is there a way to shutdown Eclipse cleanly from the command line, such that files and workspaces are saved? kill -3 doesn't do anything. kill -1 and kill -15 (default) causes Eclipse to exit abruptly with JVM termination popup. kill -9 does the same thing.

    The use case is that I'm working remotely on a machine with Eclipse loaded on it, and I want to save memory by closing Eclipse, but I want Eclipse to save its state first.

    I could use VNC or some alternative desktop sharing software, but that's really heavy-weight, and I'd much prefer a command line solution.

    EDIT: System info: RHEL5.1 64-bit using GNOME

  • Maian
    Maian about 13 years
    Installed it and twiddled with it a bit. Didn't work remotely (via ssh shell). Locally, it doesn't even list the eclipse window. Maybe I'm just using it wrong?
  • Maian
    Maian about 13 years
    Nope that didn't work - Eclipse exits abruptly (JVM termination msg pops up).
  • Paul Webster
    Paul Webster about 13 years
    This is right. A plugin developer could write a plugin to listen on a socket for a close command, and call IWorkbench.close(). Or eclipse and the launcher could be updated to support more than just the openFile action. But there's nothing that comes with eclipse by default.
  • pidge
    pidge over 11 years
    You need to set the DISPLAY environment variable export DISPLAY=:0.0 if you're running the command remotely. See my answer for a walkthrough.
  • rkyser
    rkyser over 10 years
    thanks! That worked perfect. One minor point of improvement xdotool search Exit is unnecessary as it returns the decimal form of 0x03003c2d. You can directly use the hex Window ID from wmctrl -l like xdotool key --window 0x03003c2d Return and skip the search step.
  • jorgeu
    jorgeu over 9 years
    today I just call $ kill pid
  • Andrew Rice
    Andrew Rice about 8 years
    kill -HUP pid works well for me. This gets eclipse to shutdown but still gives it a change to delete all its lock files etc.