0

I wrote a Java application which uses URLClassLoader to load a JUnit test case produced by Selenium IDE, as below:

    public void run() {
    System.out.println("JUnit thread started");

    System.out.println("My classy: "+classy);
    JUnitCore junit = new JUnitCore();
    Result result = junit.run(classy);

classy is created as below:

    Class classy = null;

    String url2 = "file://" + junitfile.replace("\\","/");

    URL url = new URL(url2);

    URLClassLoader loader = new URLClassLoader(new URL[]{url});


    try {

        classy = loader.loadClass(classToBeLoaded);

Everything works when using Eclipse. However, when the Java program is exported as runnable Jar and I make the Java application use a JUnit class (e.g test.Test1), this is the stack trace I get:

test.Test1
JUnit thread started
My classy: class test.Test1
Failure count: 1
initializationError(test.Test1): org/openqa/selenium/WebDriver
java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
initializationError(test.Test1)
java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
org/openqa/selenium/WebDriver
java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at org.junit.internal.builders.SuiteMethodBuilder.hasSuiteMethod(SuiteMe
thodBuilder.java:18)
        at org.junit.internal.builders.SuiteMethodBuilder.runnerForClass(SuiteMe
thodBuilder.java:10)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilde
r.java:59)
        at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForC
lass(AllDefaultPossibilitiesBuilder.java:26)
        at org.junit.runner.Computer.getRunner(Computer.java:40)
        at org.junit.runner.Computer$1.runnerForClass(Computer.java:31)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilde
r.java:59)
        at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)

        at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
        at org.junit.runners.Suite.<init>(Suite.java:80)
        at org.junit.runner.Computer.getSuite(Computer.java:28)
        at org.junit.runner.Request.classes(Request.java:75)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
        at malerisch.JUnitPanel$ThreadJUnit.run(JUnitPanel.java:315)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 19 more

The org.openqa.selenium.WebDriver class is included in the jar so I don't understand why it doesn't find it.

Any help is appreciated and if you need more info, happy to include it.

Cheers,

Roberto

  • and org.openqa.selenium.WebDriver is in classpath? – villanueva.ricardo Aug 30 '13 at 21:26
  • Hi Ricardo, Thanks for the comment. Another part of the same Java application makes use Selenium web driver and it works without any issues, even as runnable Jar. The issue occurs when I "import" a compiled JUnit test case (exported via Selenium IDE) and then attempt to execute it via the Java application. It seems that the class "test.Test1" in my example is loaded without using the same classpath of my Java application... do you think that is the case? – user2676412 Aug 30 '13 at 21:39

1 Answers1

0

Right click on project in Package Explorer, go to Properties, go to Libraries tab, click on 'Add Library' button, select JUnit, click Next >. You should be able to handle it from there.

source: java.lang.NoClassDefFoundError in junit

Community
  • 1
  • 1
  • Thanks again - did that again and now I got a different stack trace: test.Test1 JUnit thread started My classy: class test.Test1 Failure count: 1 initializationError(test.Test1): org/openqa/selenium/NoAlertPresentException java.lang.NoClassDefFoundError: org/openqa/selenium/NoAlertPresentException initializationError(test.Test1) java.lang.NoClassDefFoundError: org/openqa/selenium/NoAlertPresentException org/openqa/selenium/NoAlertPresentException java.lang.NoClassDefFoundError: org/openqa/selenium/NoAlertPresentException – user2676412 Aug 30 '13 at 22:07
  • Sounds like you need to wrap a try/catch around the part of your test which handles alerts. But you should probably make a separate question out of it, as it is a different error. – Dingredient Aug 30 '13 at 22:15
  • not sure, i think the issue is the still same - re-running it a few times I got back to the stack trace pasted in the original post even though I added the JUnit library as suggested by Ricardo. I think I am still missing to pass the right classpath to test.Test1 when loaded but I am not sure where that should be added/defined. – user2676412 Aug 30 '13 at 22:26