How to cleanly shutdown Eclipse from Linux command line?

Maian picture Maian · Jun 17, 2011 · Viewed 23.6k times · Source

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

Answer

pidge picture pidge · Oct 13, 2012

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.