Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have installed JUnit on my Ubuntu 11.04

sudo apt-get install junit

Then created a test file like:

import org.junit.*;

public class TestBasicLinkedList {
  @Test public void testCanAdd(String[] args) {
    BasicLinkedList<Integer> list = new BasicLinkedList();
    list.add(new BasicListNode<Integer>(1));
    assertTrue(list.size() == 1);

Then tried running in terminal

java org.junit.runner.JUnitCore


Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
Could not find the main class: org.junit.runner.JUnitCore. Program will exit.

How do I use JUnit? Is there a better shorter way of using it than the long command java org.junit.runner.JUnitCore Perhaps something like junit * maybe?

share|improve this question
up vote 6 down vote accepted

In general, JUnit (just like any Java library) needs to be on your classpath. I don't know where exactly Ubuntu stores junitX.jar, but you probably want something like this:

java -cp .:/usr/share/java/junit4-4.10.jar TestBasicLinkedList

You'll need to compile TestBasicLinkedList first.

share|improve this answer
/usr/share/java/junit.jar links to the latest junit3 version, while /usr/share/java/junit4.jar links to the latest junit4 version. – daniel kullmann Apr 11 '12 at 7:12

To get you up and running, this is a basic test case:

import junit.framework.TestCase;

public class TestBasicLinkedList extends TestCase {
  public void testTrue() {

The test class should extend TestCase, and test methods should start with "test", accept no arguments and return void.

To compile:

javac -cp /usr/share/java/junit4.jar

To run:

java -cp /usr/share/java/junit4.jar:. org.junit.runner.JUnitCore TestBasicLinkedList

This is for JUnit 4; JUnit 3 uses different classes for executing tests (try junit.textui.TestRunner).

share|improve this answer
Thanks .. java -cp /usr/share/java/junit.jar:. junit.textui.TestRunner TestBasicLinkedList works for JUnit3. This was helpful. – jaycp Dec 18 '14 at 21:57
Great explanation to ensure that basics are running! Thx a ton – Boris Däppen Oct 24 '15 at 13:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.