java.lang.NoClassDefFoundError exception while running cucumber test using JUnit
<p>I'm trying to run a cucumber test with JUnit and I'm getting <code>java.lang.NoClassDefFoundError</code> exception.</p>

<p><strong>JUnit code:</strong></p>

<pre><code>package ctest;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
features = "Feature"
,glue={"stepDefinition"}
)

public class Runner {

}
</code></pre>

<p><strong>Exception:</strong></p>

<pre><code>java.lang.NoClassDefFoundError: gherkin/formatter/Formatter
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
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)
at cucumber.runtime.formatter.PluginFactory$1.&lt;init&gt;(PluginFactory.java:53)
at cucumber.runtime.formatter.PluginFactory.&lt;clinit&gt;(PluginFactory.java:52)
at cucumber.runtime.RuntimeOptions.&lt;init&gt;(RuntimeOptions.java:69)
at cucumber.runtime.RuntimeOptionsFactory.create(RuntimeOptionsFactory.java:25)
at cucumber.api.junit.Cucumber.&lt;init&gt;(Cucumber.java:55)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: gherkin.formatter.Formatter
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)
... 34 more
</code></pre>

<p>I have added the below Cucumber jar files to Eclipse and added the cucumber plugin to eclipse from thi link - <a href="http://cucumber.github.com/cucumber-eclipse/update-site" rel="noreferrer">http://cucumber.github.com/cucumber-eclipse/update-site</a>.</p>

<p><a href="https://i.stack.imgur.com/QjHjk.png" rel="noreferrer"><img src="https://i.stack.imgur.com/QjHjk.png" alt="enter image description here"></a></p>

<p>I found some similar questions in SE and tried the suggestions mentioned there but in vain. I tried downgrading gherkin to a lower version as suggested in the below links. But that doesn't fix the problem.</p>

<ul>
<li><a href="https://stackoverflow.com/questions/32378974/noclassdeffounderror-while-setting-up-cucumber-tests">NoClassDefFoundError while setting up cucumber tests</a></li>
<li><a href="https://stackoverflow.com/questions/32319920/exception-in-thread-main-java-lang-noclassdeffounderror-gherkin-formatter-for">Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: gherkin/formatter/Formatter</a></li>
</ul>

<p>Answers to below questions have suggested to add the same version of cucumber jar files. But since the jar files that are available in the Maven repositry itself doesn't have the same version, I'm not sure how/where can I download the same version of all cucumber jar files.</p>

<ul>
<li><a href="https://stackoverflow.com/questions/25291480/cucumber-exception-java-lang-classnotfoundexception-cucumber-io-resourceloader">Cucumber Exception: java.lang.ClassNotFoundException: cucumber.io.ResourceLoader</a></li>
<li><a href="https://stackoverflow.com/questions/18513603/how-to-solve-noclassdeffounderror-in-cucumber-test">how to solve NoClassDefFoundError in cucumber test</a></li>
</ul>

<p>Any help to fix this issue is much appreciated.</p>