Using JUnitCore to run a Selenium/Appium test creates a failure initializationError (org/openqa/selenium/Capabilities) when reading test class from a jar

I wanted to add a section into our application which allows users to upload a jar containing JUnit tests. I want to be able to run these jar's tests within my java application. I can get the Class to be loaded from the Jar but when it goes to run the test I get an error

Failure: initializationError(com.sample.Test): org/openqa/selenium/Capabilities


If I run the same test with putting the Test class within the classpath and call it via the class directly it works correctly and runs the test within Appium

JUnitCore junit = new JUnitCore();
Result result =;

When I go to load the same class from the jar it fails.

Does anyone know why this would be happening? Maven is setup to import all the Selenium/Appium jars correctly. If I run the test directly within my application there is no issue just when the class is taken from the jar.

 import org.openqa.selenium.remote.DesiredCapabilities; 


   // not on class path File jarLocation = new File("C:/Work/testcases-0.0.1.jar");
   File jarLocation = new File("lib/testcases-0.0.1.jar");  // on classpath

    try {
        JarInputStream testJarFile;
        testJarFile = new JarInputStream(new FileInputStream(jarLocation));

        while (true) {
            JarEntry testJar = testJarFile.getNextJarEntry();
            if (testJar == null) {
            if ((testJar.getName().endsWith(".class"))) {
                String file = testJar.getName();
                String classname = file.replace('/', '.').substring(0, file.length() - 6);
                    Class<?> c = Class.forName(classname);
                    JUnitCore junit = new JUnitCore();
                    Result result =;
                    for (Failure failure : result.getFailures()) {
              "Failure: " + failure.toString());
                catch (Throwable e) 
                    Logger.error("WARNING: failed to instantiate " + classname + " from " + file);
                    Logger.error("ERROR: Exception " + e);
    } catch (Exception e) {
        // TODO Auto-generated catch block

Test Class

  public class Test{

      public void test() throws MalformedURLException {
          //Grab file - device info
          File appDir = new File("lib");
          File app = new File(appDir, "deviceinfo.apk");
          //Setup as android device
          DesiredCapabilities cap = new DesiredCapabilities();
          cap.setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);
          cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Device");
          //Run app
          cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "20");  //only wait 20 sec
          cap.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
          AndroidDriver driver = new AndroidDriver(new URL(""), cap);
          //Close app