Index: trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java
===================================================================
--- trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java	(revision 9917)
+++ trunk/src/org/openstreetmap/josm/actions/AddImageryLayerAction.java	(revision 9918)
@@ -6,4 +6,5 @@
 
 import java.awt.Dimension;
+import java.awt.GraphicsEnvironment;
 import java.awt.GridBagLayout;
 import java.awt.event.ActionEvent;
@@ -108,13 +109,15 @@
             formats.setToolTipText(tr("Select image format for WMS layer"));
 
-            if (1 != new ExtendedDialog(Main.parent, tr("Select WMS layers"), new String[]{tr("Add layers"), tr("Cancel")}) { {
-                final JScrollPane scrollPane = new JScrollPane(tree.getLayerTree());
-                scrollPane.setPreferredSize(new Dimension(400, 400));
-                final JPanel panel = new JPanel(new GridBagLayout());
-                panel.add(scrollPane, GBC.eol().fill());
-                panel.add(formats, GBC.eol().fill(GBC.HORIZONTAL));
-                setContent(panel);
-            } }.showDialog().getValue()) {
-                return null;
+            if (!GraphicsEnvironment.isHeadless()) {
+                if (1 != new ExtendedDialog(Main.parent, tr("Select WMS layers"), new String[]{tr("Add layers"), tr("Cancel")}) { {
+                    final JScrollPane scrollPane = new JScrollPane(tree.getLayerTree());
+                    scrollPane.setPreferredSize(new Dimension(400, 400));
+                    final JPanel panel = new JPanel(new GridBagLayout());
+                    panel.add(scrollPane, GBC.eol().fill());
+                    panel.add(formats, GBC.eol().fill(GBC.HORIZONTAL));
+                    setContent(panel);
+                } }.showDialog().getValue()) {
+                    return null;
+                }
             }
 
@@ -141,12 +144,20 @@
             return ret;
         } catch (MalformedURLException ex) {
-            JOptionPane.showMessageDialog(Main.parent, tr("Invalid service URL."),
-                    tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
+            if (!GraphicsEnvironment.isHeadless()) {
+                JOptionPane.showMessageDialog(Main.parent, tr("Invalid service URL."),
+                        tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
+            }
+            Main.error(ex, false);
         } catch (IOException ex) {
-            JOptionPane.showMessageDialog(Main.parent, tr("Could not retrieve WMS layer list."),
-                    tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
+            if (!GraphicsEnvironment.isHeadless()) {
+                JOptionPane.showMessageDialog(Main.parent, tr("Could not retrieve WMS layer list."),
+                        tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
+            }
+            Main.error(ex, false);
         } catch (WMSImagery.WMSGetCapabilitiesException ex) {
-            JOptionPane.showMessageDialog(Main.parent, tr("Could not parse WMS layer list."),
-                    tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
+            if (!GraphicsEnvironment.isHeadless()) {
+                JOptionPane.showMessageDialog(Main.parent, tr("Could not parse WMS layer list."),
+                        tr("WMS Error"), JOptionPane.ERROR_MESSAGE);
+            }
             Main.error("Could not parse WMS layer list. Incoming data:\n"+ex.getIncomingData());
         }
Index: trunk/src/org/openstreetmap/josm/gui/HelpAwareOptionPane.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/HelpAwareOptionPane.java	(revision 9917)
+++ trunk/src/org/openstreetmap/josm/gui/HelpAwareOptionPane.java	(revision 9918)
@@ -245,5 +245,5 @@
             doShowOptionDialog(parentComponent, title, options, defaultOption, helpTopic, buttons, pane);
         }
-        return (Integer) pane.getValue();
+        return pane.getValue() instanceof Integer ? (Integer) pane.getValue() : JOptionPane.OK_OPTION;
     }
 
