Source: http://www.tutorialspoint.com/junit/junit_environment_setup.htm
Step 1 - verify Java installation in your machine
Now open console and execute the following java command.
| OS | Task | Command |
|---|---|---|
| Windows | Open Command Console | c:\> java -version |
| Linux | Open Command Terminal | $ java -version |
| Mac | Open Terminal | machine:~ joseph$ java -version |
Let's verify the output for all the operating systems:
| OS | Output |
|---|---|
| Windows | java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)
|
| Linux | java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)
|
| Mac | java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07)
Java HotSpot(TM)64-Bit Server VM (build 17.0-b17, mixed mode, sharing)
|
If you do not have Java installed, install the Java Software Development Kit (SDK) fromhttp://www.oracle.com/technetwork/java/javase/downloads/index.html. We are assuming Java 1.6.0_21 as installed version for this tutorial.
Step 2: Set JAVA environment
Set the JAVA_HOME environment variable to point to the base directory location where Java is installed on your machine. For example
| OS | Output |
|---|---|
| Windows | Set the environment variable JAVA_HOME to C:\Program Files\Java\jdk1.6.0_21 |
| Linux | export JAVA_HOME=/usr/local/java-current |
| Mac | export JAVA_HOME=/Library/Java/Home |
Append Java compiler location to System Path.
| OS | Output |
|---|---|
| Windows | Append the string ;C:\Program Files\Java\jdk1.6.0_21\bin to the end of the system variable, Path. |
| Linux | export PATH=$PATH:$JAVA_HOME/bin/ |
| Mac | not required |
Verify Java Installation using java -version command explained above.
Step 3: Download Junit archive
Download latest version of JUnit jar file from http://www.junit.org. At the time of writing this tutorial, I downloaded Junit-4.10.jar and copied it into C:\>JUnit folder.
| OS | Archive name |
|---|---|
| Windows | junit4.10.jar |
| Linux | junit4.10.jar |
| Mac | junit4.10.jar |
Step 4: Set JUnit environment
Set the JUNIT_HOME environment variable to point to the base directory location where JUNIT jar is stored on your machine. Assuming, we've stored junit4.10.jar in JUNIT folder on various Operating Systems as follows.
| OS | Output |
|---|---|
| Windows | Set the environment variable JUNIT_HOME to C:\JUNIT |
| Linux | export JUNIT_HOME=/usr/local/JUNIT |
| Mac | export JUNIT_HOME=/Library/JUNIT |
Step 5: Set CLASSPATH variable
Set the CLASSPATH environment variable to point to the JUNIT jar location. Assuming, we've stored junit4.10.jar in JUNIT folder on various Operating Systems as follows.
| OS | Output |
|---|---|
| Windows | Set the environment variable CLASSPATH to %CLASSPATH%;%JUNIT_HOME%\junit4.10.jar;.; |
| Linux | export CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit4.10.jar:. |
| Mac | export CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit4.10.jar:. |
Step 6: Test JUnit Setup
Create a java class file name TestJunit in C:\ > JUNIT_WORKSPACE
import org.junit.Test; import static org.junit.Assert.assertEquals; public class TestJunit { @Test public void testAdd() { String str= "Junit is working fine"; assertEquals("Junit is working fine",str); } }
Create a java class file name TestRunner in C:\ > JUNIT_WORKSPACE to execute Test case(s)
import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class TestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(TestJunit.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } }
Step 7: Verify the Result
Compile the classes using javac compiler as follows
C:\JUNIT_WORKSPACE>javac TestJunit.java TestRunner.java
Now run the Test Runner to see the result
C:\JUNIT_WORKSPACE>java TestRunner
Verify the output.
true
Practical:
- Write a java program in a notepad
- Save it with same name as that of its class name
- Open the command prompt, set path and class path
path:
set path = "c:\java 1.5\bin"
C:\ YourFolder >javac YourCode.java
Note: If you set the class path you can execute java code from anywhere.set classpath = c:\java\jre\lib; %classpath%
Execute java program on your local machine without SDK:
Syntax:
- Set path="JDK path" //set up jdk path:
- javac "path/filename.java" //compile java file by giving full path of the file
- java "path/filename" //execute the file by giving full path of the file
Validation:
java -version // this will check your jre version
javac -version // this will check your java compiler version if you installed
#Executing Java file:
ReplyDeleteI have a JDK(JRE) installed in my machine. Do I need to Set Java_Home path to run my java programs from command prompt, cant I run without setting the path. What exactly this path means.
#Steps_to_be_followed_to_run_java_program_from_cmd:
ReplyDelete- Go to the cmd, and set the JDK path:
set path="jdk till bin"
or
Go to Environmental variables and set the path:
set JAVA_HOME="jdk till bin"
- Go to the java file path in cmd using cd
- Once you are in the path, execute java program by entering:
c:\path> javac filename.java
- c:\path> java filename
You should see result in cmd.