0

I try to start several JUnitTests from a simple Java Application. Therefore I implemented a little GUI with a start button and a log field. Now here comes the Problem:

Everey time I start my App from Eclipse all works fine but if I try to pack my maven-project and start this jar file it throws an no class found Exception for JUnitCore.

I think maybe there is something wrong with the classpaths but I got confused while reading the solutions I found with google...

Could anyone help me please?

Thanks upwards

Edit: The Exception in detail:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
        at com.selenium.tool.gui.DefaultGui$1.actionPerformed(DefaultGui.java:155)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$200(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
        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 sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 37 more

Edit: and the dependencies of my project:

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.42.2</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.7</version>
    </dependency>

    <dependency>
        <groupId>org.apache.sling</groupId>
        <artifactId>org.apache.sling.junit.core</artifactId>
        <version>1.0.8</version>
    </dependency>

Maybe the following warning is also interesting for you:

[WARNING] Some problems were encountered while building the effective model for com.lsy.selenium:com.selenium:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-jar-plugin is missing. @ line 44, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
Pascal
  • 74
  • 8
  • how are you 'packing' your maven project? using any plugin? – Adi Aug 21 '14 at 14:02
  • Unit tests should not be running from your application jar. They are not shipped as part of the application. Unit tests are usually run by Maven after compilation and before packaging. –  Aug 21 '14 at 14:19
  • The reason why I decide to let the tests run by my Application is that they are selenium tests of our intranet site. Because of that they don't test any java applications so they are not tests of my maven project but the project is the testing! – Pascal Aug 21 '14 at 15:35
  • What is the class that is not found? Have you included JUnit as a compile-scope dep as opposed to a test-scope? Usually when this type of error occurs (works in Eclipse but not deployed) is that Eclipse doesn't respect Maven scope properly. Eclipse includes test-scope jars even when running the application. – John B Aug 21 '14 at 15:39
  • I added the detailed exception and the dependencies in my question. Do you need some further information? – Pascal Aug 21 '14 at 15:51

2 Answers2

1

Change

   <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
</dependency>

to

   <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>compile</scope>
</dependency>

Probably somewhere in your project hierarchy you have specified that junit is test scope.

John B
  • 32,493
  • 6
  • 77
  • 98
0

Okay I got a solution.

There were two problems. First some dependency is missing:

    <dependency>
        <groupId>xml-apis</groupId>
        <artifactId>xml-apis</artifactId>
        <version>1.4.01</version>
    </dependency>

And the second which is important was that I don't realize that the libraries were not included while packing the jar. But they have to be packed too! In the following article you could find the way to include the libs when packing the jar.

How can I create an executable jar with dependencies using Maven?

Thanks for your support!

Community
  • 1
  • 1
Pascal
  • 74
  • 8