When you want to learn Java it is good to know about the basics.
Requirements
- Text editor
- 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 vim Main.java |
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)
javac Main.java ls |
You should see now 2 Files Main.java 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 Main.java into the package
mv Main.java mypackage/ |
Now compile (you are still in src)
javac mypackage/Main.java |
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/Main.java |
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 |
Set CLASSPATH
Programmers are lazy so set the classpath.
To view the classpath type
echo $CLASSPATH |
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 https://github.com/KentBeck/junit/downloads
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 http://junit.sourceforge.net/doc/faq/faq.htm#started_2
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{ @Test public void testEmptyCollection(){ Collection collection = new ArrayList(); assertTrue(collection.isEmpty()); } } |
Go back to project folder, compile and run the test.
cd .. javac -d bin test/SimpleTest.java java org.junit.runner.JUnitCore SimpleTest |
Should be output
JUnit version 4.9b2 . Time: 0,006 OK (1 test) |
Annotation
The example SimpleTest.java is taken from http://junit.sourceforge.net/doc/faq/faq.htm#started_2