30

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

7 Answers 7

43

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.

1
  • 3
    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.
    – rkyser
    Dec 19, 2013 at 21:18
7

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
3

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.

3
  • 1
    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. Jun 20, 2011 at 14:30
  • today I just call $ kill pid
    – jorgeu
    Mar 4, 2015 at 17:43
  • 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. May 18, 2016 at 8:10
3

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

0
1

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.

2
  • 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
    Jun 17, 2011 at 21:54
  • 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.
    – pidge
    Oct 13, 2012 at 6:36
0

Did you try kill -HUP (kill -1)? -- that's the canonical way to tell a process that whoever was interacting with it has gone away and it should clean up appropriately

1
  • Nope that didn't work - Eclipse exits abruptly (JVM termination msg pops up).
    – Maian
    Jun 17, 2011 at 21:55
0

The answer to this question was helpful to me in a similar issue: Eclipse hanging, how to kill it properly?

After I killed the eclipse process the Eclipse window kept there until I killed the java process (I didn't have a javaw process as in the answer above. I had only one "java" process that when killed fixed the problem).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.