--- old/src/jdk.jlink/share/classes/jdk/tools/jimage/JImageTask.java	2018-02-09 16:13:26.056281896 +0100
+++ new/src/jdk.jlink/share/classes/jdk/tools/jimage/JImageTask.java	2018-02-09 16:13:25.820281463 +0100
@@ -380,9 +380,7 @@
     private void iterate(JImageAction jimageAction,
             ModuleAction moduleAction,
             ResourceAction resourceAction) throws IOException, BadArgs {
-        if (options.jimages.isEmpty()) {
-            throw TASK_HELPER.newBadArgs("err.no.jimage");
-        }
+        validateOptions();
 
         for (File file : options.jimages) {
             if (!file.exists() || !file.isFile()) {
@@ -433,6 +431,25 @@
                 }
             }
         }
+    }
+
+    private void validateOptions() throws BadArgs {
+        if (options.jimages.isEmpty()) {
+            throw TASK_HELPER.newBadArgs("err.no.jimage");
+        }
+
+        if (options.task == Task.EXTRACT) {
+            File extractTargetDir = new File(options.directory);
+            if (extractTargetDir.exists()) {
+                if (extractTargetDir.isDirectory()) {
+                    if (extractTargetDir.listFiles().length > 0) {
+                        throw TASK_HELPER.newBadArgs("err.not.an.empty.dir", options.directory);
+                    }
+                } else {
+                    throw TASK_HELPER.newBadArgs("err.not.a.dir", options.directory);
+                }
+            }
+        }
     }
 
     private boolean run() throws Exception, BadArgs {
--- old/src/jdk.jlink/share/classes/jdk/tools/jimage/resources/jimage.properties	2018-02-09 16:13:26.603282902 +0100
+++ new/src/jdk.jlink/share/classes/jdk/tools/jimage/resources/jimage.properties	2018-02-09 16:13:26.365282464 +0100
@@ -94,6 +94,7 @@
 err.not.a.task=task must be one of <extract | info | list | verify>: {0}
 err.missing.arg=no value given for {0}
 err.not.a.dir=not a directory: {0}
+err.not.an.empty.dir=output directory must be empty: {0}
 err.not.a.jimage=not a jimage file: {0}
 err.no.jimage=no jimage provided
 err.option.unsupported={0} not supported: {1}
--- old/test/jdk/tools/jimage/JImageCliTest.java	2018-02-09 16:13:27.140283889 +0100
+++ new/test/jdk/tools/jimage/JImageCliTest.java	2018-02-09 16:13:26.901283450 +0100
@@ -97,6 +97,7 @@
                     String.format("Output contains error, output=[%s]\n", output));
             assertFalse(output.contains("Exception"),
                     String.format("Output doesn't contain a stacktrace, output=[%s]\n", output));
+            System.out.print(output);
             return this;
         }
 
@@ -105,10 +106,16 @@
 
     protected final void runTests() throws Throwable {
         if (getImagePath() != null) {
+            Method beforeTestMethod = getClass().getDeclaredMethod("beforeTest");
             for (Method m : getClass().getDeclaredMethods()) {
                 if (m.getName().startsWith("test")) {
+                    if (beforeTestMethod != null) {
+                        System.out.printf("Invoking %s\n", beforeTestMethod.getName());
+                        beforeTestMethod.invoke(this);
+                    }
                     System.out.printf("Invoking %s\n", m.getName());
                     m.invoke(this);
+                    System.out.println();
                 }
             }
         } else {
--- old/test/jdk/tools/jimage/JImageExtractTest.java	2018-02-09 16:13:27.746285003 +0100
+++ new/test/jdk/tools/jimage/JImageExtractTest.java	2018-02-09 16:13:27.466284489 +0100
@@ -26,10 +26,11 @@
  * @summary Tests to verify jimage 'extract' action
  * @library /test/lib
  * @modules jdk.jlink/jdk.tools.jimage
- * @build jdk.test.lib.Asserts
+ * @build jdk.test.lib.Asserts JImageCliTest
  * @run main/othervm JImageExtractTest
  */
 
+import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -46,6 +47,18 @@
 import static jdk.test.lib.Asserts.assertTrue;
 
 public class JImageExtractTest extends JImageCliTest {
+    public void beforeTest() throws IOException {
+        Path cwd = new File(".").toPath();
+        System.out.printf("cleaning working dir [%s]... \n", cwd.toAbsolutePath());
+        for (File f : new File(".").listFiles()) {
+            if (f.isDirectory()) {
+                jdk.test.lib.util.FileUtils.deleteFileTreeWithRetry(f.toPath());
+            } else {
+                f.delete();
+            }
+        }
+    }
+
     public void testExtract() throws IOException {
         jimage("extract", getImagePath())
                 .assertSuccess()
@@ -55,6 +68,16 @@
         verifyExplodedImage(Paths.get("."));
     }
 
+    // do a second round to check that workspace initialization works ok
+    public void testExtractSecondRound() throws IOException {
+        jimage("extract", getImagePath())
+                .assertSuccess()
+                .resultChecker(r -> {
+                    assertTrue(r.output.isEmpty(), "Output is not expected");
+                });
+        verifyExplodedImage(Paths.get("."));
+    }
+
     public void testExtractHelp() {
         for (String opt : Arrays.asList("-h", "--help")) {
             jimage("extract", "--help")
@@ -82,9 +105,17 @@
                 .assertShowsError();
     }
 
+    public void testExtractEmptyFile() throws IOException {
+        Path tmp = Files.createTempFile(Paths.get("."), getClass().getName(), "empty_file");
+        jimage("extract", "--dir", "some_directory", tmp.toString())
+                .assertFailure()
+                .assertShowsError();
+    }
+
     public void testExtractNotAnImage() throws IOException {
         Path tmp = Files.createTempFile(Paths.get("."), getClass().getName(), "not_an_image");
-        jimage("extract", tmp.toString())
+        Files.write(tmp, "This is not an image".getBytes());
+        jimage("extract", "--dir", "some_directory", tmp.toString())
                 .assertFailure()
                 .assertShowsError();
     }
