Schlagwort-Archive: export

Compile, Run And Develop Java in the Console, Setup for TDD with JUnit

When you want to learn Java it is good to know about the basics.


  1. Text editor
  2. Java installed

check if java is installed.

java -version

Should be displayed something like that:

java version "1.6.0_24"
Java(TM) SE Runtime Environment (build 1.6.0_24-b07-348-9M3406a)
Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02-348, mixed mode)

Creating Development Structure

expected we want to create a project folder and in it an src,bin,lib,test folder.
src = here you create source files (packages)
bin = Output folder
lib = for Libraries e.g. JUnit.jar
test = mirroed the package structure from src, here you develop your tests.

mkdir project
cd project
mkdir src
mkdir bin
mkdir lib
mkdir test

Write a first very simple program

cd src

type the following:

public class Main{
 public static void main(String[] args){
  System.out.println("Hello User");

Back to console compile! Just to see it runs. (You are still in folder src)


You should see now 2 Files and Main.class
To run the program just type

java Main

You should see

Hello User

Now delete the Main.class file.

Use Packages

It is recommend to use packages.
create a package (mkdir mypackage)
Java conventions says package names is always lowercase and have no special characters.
move into the package

mv mypackage/

Now compile (you are still in src)

javac mypackage/

You can run the program with 2 Ways

cd mypackage
java Main

Or back in folder src we have to set the classpath (-cp)

java -cp mypackage Main

Set output folder

delete mypackage/Main.class

javac -d ../bin mypackage/

Now the Main.class file should be in bin folder.
Run the program

cd ../bin
java Main

Or go to project folder and call the Main with classpath

cd ..
java -cp bin Main


Programmers are lazy so set the classpath.
To view the classpath type


If the output is Nothing then the variable isn’t set.
ensure that you are in project folder

export CLASSPATH=./bin

Now you can start the program from here with

java Main

Install JUnit

Download newest version from
Copy the download junit.jar file to your lib folder.
Add the jar to CLASSPATH

export CLASSPATH=$CLASSPATH:./lib/junit-4.9b2.jar

Test wether you reach the JUnitCore class

java org.junit.runner.JUnitCore

You should see something like this.

JUnit version 4.9b2
Time: 0,001
OK (0 tests)

For more information take a look at

Write a Simple Test

Go to test folder and create a Class SimpleTest with the following content

import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
public class SimpleTest{
    public void testEmptyCollection(){
        Collection collection = new ArrayList();

Go back to project folder, compile and run the test.

cd ..
javac -d bin test/
java org.junit.runner.JUnitCore SimpleTest

Should be output

JUnit version 4.9b2
Time: 0,006
OK (1 test)


The example is taken from