How to Run JUnit Class using CMD or Terminal

Developers usually use IDE to make and run Java Applications now. But for learning purposes, some still prefer running Java application through cmd or terminal. To run a Java Class with main method is very simple, it was the first thing we learned when we started learning Java. But do you know how to run JUnit Test Class from CMD or terminal?

How to Run JUnit Class using CMD or Terminal

In this article, we will teach you how to run JUnit test case through cmd or terminal.


  • JUnit jar: Download
  • Hamcrest-core : Download
  • Download above jars and store it in a folder or if you already have it in your maven repository you can use that.

Compiling JUnit Test through CMD Syntax:

javac -cp <-- Path to JUnit Jar -->;.

In above query for compiling, we first specify the path to Junit Jar in classpath (-cp) and give the Java class that needs to be compiled.

Running JUnit Test through CMD Syntax:

java -cp <-- Path to JUnit Jar -->;<-- Path to Hamcrest-core Jar -->;. org.junit.runner.JUnitCore FileName

In above query for running, we specify the path to Junit Jar and hamcrest jar in classpath (-cp) and then "org.junit.runner.JUnitCore" Class which actually triggers JUnit Test Class to run.

Simple JUnit Test Class:

import static org.junit.Assert.*; import java.util.LinkedList; import org.junit.*; public class JUnitTest { private LinkedList<String> list; @Before public void beforeClassMethod() { list=new LinkedList<String>(); System.out.println("@BeforeClass: beforeClassMethod executed"); } @Test public void emptyLinkedList() { assertTrue(list.isEmpty()); System.out.println("@Test: EmptyList"); } @Test public void oneItemCollection() { list.add("A"); assertEquals(1, list.size()); System.out.println("@Test: OneItemList"); } @After public void afterClassMethod() { System.out.println("@AfterClass: afterClassMethod executed"); } }

Running Test Class through CMD:

  1. For Compiling:
    javac -cp C:\Users\ss\Desktop\junit\4.12\junit-4.13.jar;.
  2. Running Code:
    java -cp C:\Users\ss\Desktop\junit-4.13.jar;C:\Users\ss\Desktop\hamcrest-core-1.3.jar;. org.junit.runner.JUnitCore JUnitTest

In above command, after -cp just specify the path where your jar file is stored.


org.junit.runner.JUnitCore JUnitTest JUnit version 4.13 .@BeforeClass: beforeClassMethod executed @Test: EmptyList @AfterClass: afterClassMethod executed .@BeforeClass: beforeClassMethod executed @Test: OneItemList @AfterClass: afterClassMethod executed Time: 0.016 OK (2 tests)

Get Answer to Your Problem

Facing any Problem. Don't Hesitate to Ask Us. We will try to solve your problem.

Ask Question Now

Play 2048 Game Online

Search Tags

    Run JUnit through terminal

    Compile JUnit Test using cmd

    how to run Junit test case using command prompt