/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.cucumber;

import com.intellij.TestCaseLoader;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Ref;
import com.intellij.testFramework.TestRunnerUtil;
import com.intellij.util.ui.UIUtil;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.Resource;
import cucumber.runtime.io.ResourceLoaderClassFinder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Dennis.Ushakov
 */
public class CucumberMain {
  static {
    // Radar #5755208: Command line Java applications need a way to launch without a Dock icon.
    System.setProperty("apple.awt.UIElement", "true");
  }

  public static void main(String[] args) throws IOException {
    int exitStatus;
    try {
      exitStatus = run(args, Thread.currentThread().getContextClassLoader());
    }
    catch (Throwable e) {
      exitStatus = 1;
    }
    System.exit(exitStatus);
  }

  public static int run(final String[] argv, final ClassLoader classLoader) throws IOException {
    final Ref<Throwable> errorRef = new Ref<>();
    final Ref<Runtime> runtimeRef = new Ref<>();

    try {
      TestRunnerUtil.replaceIdeEventQueueSafely();
      UIUtil.invokeAndWaitIfNeeded((Runnable)() -> {
        try {
          RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList(Arrays.asList(argv)));
          MultiLoader resourceLoader = new MultiLoader(classLoader) {
            @Override
            public Iterable<Resource> resources(String path, String suffix) {
              Iterable<Resource> resources = super.resources(path, suffix);
              if (TestCaseLoader.shouldBucketTests() && ".feature".equals(suffix)) {
                List<Resource> filteredResource = new ArrayList<>();
                resources.forEach(it -> {
                  if (TestCaseLoader.matchesCurrentBucket(it.getPath())) {
                    filteredResource.add(it);
                  }
                });
                return filteredResource;
              }
              return resources;
            }
          };
          ResourceLoaderClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
          Runtime runtime = new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
          runtimeRef.set(runtime);
          runtime.run();
        }
        catch (Throwable throwable) {
          errorRef.set(throwable);
          Logger.getInstance(CucumberMain.class).error(throwable);
        }
      });
    }
    catch (Throwable t) {
      errorRef.set(t);
      Logger.getInstance(CucumberMain.class).error(t);
    }

    final Throwable throwable = errorRef.get();
    if (throwable != null) {
      throwable.printStackTrace();
    }
    System.err.println("Failed tests :");
    for (Throwable error : runtimeRef.get().getErrors()) {
      error.printStackTrace();
      System.err.println("=============================");
    }
    return throwable != null ? 1 : 0;
  }
}

@@ -793,24 +793,111 @@
 RunnerUtil;%0A
+import com.intellij.util.ReflectionUtil;%0Aimport com.intellij.util.lang.UrlClassLoader;%0A
 import com.i
@@ -1153,16 +1153,48 @@
 eption;%0A
+import java.net.URLClassLoader;%0A
 import j
@@ -1531,35 +1531,16 @@
 %5B%5D args)
- throws IOException
  %7B%0A    i
@@ -1574,30 +1574,105 @@
     
-exitStatus = run(args,
+UrlClassLoader loader = new UrlClassLoader(Thread.currentThread().getContextClassLoader());%0A     
  Thr
@@ -1683,33 +1683,33 @@
 currentThread().
-g
+s
 etContextClassLo
@@ -1709,25 +1709,185 @@
 ClassLoader(
-)
+loader);%0A      exitStatus = (Integer)loader.loadClass(CucumberMain.class.getName()).getMethod(%22run%22, String%5B%5D.class, ClassLoader.class).invoke(null, args, loader
 );%0A    %7D%0A   

use UrlClassLoader for better similarity with JUnit tests