package redis.embedded.util;

import com.google.common.io.Files;
import com.google.common.io.Resources;
import org.apache.commons.io.FileUtils;
import redis.embedded.exceptions.*;

import java.io.File;
import java.io.IOException;

public class JarUtil {

    public static File extractExecutableFromJar(String executable) throws IOException {
        File tmpDir = Files.createTempDir();
        tmpDir.deleteOnExit();

        File command = new File(tmpDir, executable);
        FileUtils.copyURLToFile(Resources.getResource(executable), command);
        command.deleteOnExit();
        if (!command.setExecutable(true)) {
            //should not ever happen
            throw new RedisBuildingException("Cannot make file " + command.getName() + " executable.");
        }

        return command;
    }
}

@@ -222,16 +222,104 @@
 ception;
+%0Aimport java.net.URL;%0A%0Aimport static com.google.common.base.Preconditions.checkArgument;
 %0A%0Apublic
@@ -516,21 +516,30 @@
  File co
-mmand
+piedExecutable
  = new F
@@ -575,92 +575,134 @@
     
-FileUtils.copyURLToFile(Resources.getResource(executable), command);%0A        command
+URL resource = getResource(executable);%0A%0A        FileUtils.copyURLToFile(resource, copiedExecutable);%0A        copiedExecutable
 .del
@@ -710,24 +710,25 @@
 teOnExit();%0A
+%0A
         if (
@@ -730,21 +730,30 @@
  if (!co
-mmand
+piedExecutable
 .setExec
@@ -874,21 +874,30 @@
 e %22 + co
-mmand
+piedExecutable
 .getName
@@ -950,13 +950,1532 @@
 n co
-mmand
+piedExecutable;%0A    %7D%0A%0A    /**%0A     * Imported from guava to introduce a default %7B@link ClassLoader%7D.%0A     * %3Cp%3E%0A     * Returns a %7B@code URL%7D pointing to %7B@code resourceName%7D if the resource is%0A     * found using the %7B@linkplain Thread#getContextClassLoader() context class%0A     * loader%7D. In simple environments, the context class loader will find%0A     * resources from the class path. In environments where different threads can%0A     * have different class loaders, for example app servers, the context class%0A     * loader will typically have been set to an appropriate loader for the%0A     * current thread.%0A     * %3Cp%3E%0A     * %3Cp%3EIn the unusual case where the context class loader is null, the class%0A     * loader that loaded this class (%7B@code Resources%7D) will be used instead.%0A     *%0A     * @throws IllegalArgumentException if the resource is not found%0A     * @see Resources#getResource(String)%0A     */%0A    private static URL getResource(String resourceName) %7B%0A        URL url = null;%0A        ClassLoader loader = Thread.currentThread().getContextClassLoader();%0A%0A        // Try in the current context class loader%0A        if (loader != null) %7B%0A            url = loader.getResource(resourceName);%0A        %7D%0A%0A        // If nothing, try in this class ClassLoader%0A        if (url == null) %7B%0A            loader = JarUtil.class.getClassLoader();%0A            url = loader.getResource(resourceName);%0A        %7D%0A%0A        // Not found ?%0A        checkArgument(url != null, %22resource %25s not found.%22, resourceName);%0A%0A        return url
 ;%0A  

Modify the default class loader used to extract from jar