01. Setting up Java step by step tutorial

This is mainly for the beginners of Java to get started with Maven, and eclipse. This is required not only for learning Java, but also conducive to creating your self-taught Java projects.

Set up folders

1) C:\tools to install Java development tools like JDK, Maven, Eclipse IDE, etc. Create a sub-folder “jdk-8u131-windows-x64” to install JDK “8u131”.

2) C:\projects to store your Java projects.

3) C:\scripts to store any DOS or Unix scripts.

Pick your own folder. It could be under “C:\users\john\tools\java\jdk-8u131-windows-x64”, “C:\users\john\projects”, etc. It is important to separate “tools“, “projects“, and “scripts“.

Install Java

Step 1: Download and install latest “Java SE Development Kit” (i.e. JDK) version “X” from

http://www.oracle.com/technetwork/java/javase/overview/index.html

by clicking on the “Downloads” tab. Select the “Java SE Platforms” and “Java” button.

Downloading JDK

Pick the Java SE Development Kit X Downloads, where “X” is the major version like 8, you will have the minor updates marked as say “u131“, etc.

Make sure that you download and install the right version for the operating system on which you will be running — for example Windows (32 bit or 64 bit), Linux, Solaris, MAC, etc. You will need both the JDK and the JRE.

Download 64 bit JDK for windows

Double click on the downloaded file “jdk-8u131-windows-x64.exe”, and follow the installation prompts and choose to change to “C:\tools\jdk-8u131-windows-x64” or whatever the folder you chose earlier.

Verify Java Installation

Step 1: Go to the installation folder, for example “C:\tools\jdk-8u131-windows-x64” to verify the presence of relevant files required for compiling & running Java.

Screen shot 2014-10-19 at 4.39.59 PM

The javac.exe is the compiler that converts a source file (e.g. HelloWorld.java) to a byte code file (e.g. HelloWorld.class). The java.exe is the run-time command to execute a program (i.e. java HelloWorld). The src.zip is where all the Java API (i.e. Java library) source (i.e. .java) files are located and rt.jar is where the Java API run-time class files (i.e. class) are located.

Step 2: Open a DOS command prompt, and go to the folder “C:\tools\jdk-8u131-windows-x64\bin” and type

If you run the above command “java -version” from any other folder outside “C:\tools\jdk-8u131-windows-x64\bin” it won’t recognize the command.

What if you want to run your *.java files from “C:\projects” folder? You need to set the environment variables to achieve that as shown below.

Configure Java to run from any folder for a given DOS session

Step 1: Set the environment variables. In setting up JDK and Java applications, you will encounter these environment variables:

1. PATH used by javac & java commands to discover JDK installation folders.
2. CLASSPATH used by javac & java commands to discover other Java classes.
3. JAVA_HOME used in defining PATH.
4. JRE_HOME used in IDEs like Eclipse to define the JRE directory location.

Q. What are environment variables & why do you need to set them to use Java?
A. Environment variables are global system variables accessible by all the processes running under the Operating System (OS). To get Java running you need to initially set the following environment variables

1) JAVA_HOME maintains the location of JDK installed directory. Similarly, JRE_HOME maintains the location of the JRE installed directory.

2) PATH stores a list of directories for searching executable programs. So, when type “javac” to compile or “java” to run a program from any directory, the operating system knows where to find these commands i.e. from %JAVA_HOME%/bin. If you don’t set the “PATH”, you can only run it from the JDK’s location under bin, which is very limiting.

Create “environment-setup.cmd” file under “C:\scripts” as shown below,

and run it

On Unix

“.bashrc” will look something like

Verifying the environment variables

In a command prompt, if you type “set” (in WIN 32) and “env” (in Unix), it will show all the environment variables. You need to set the JAVA_HOME environment variable. In DOS it is %JAVA_HOME% and in a Unix system $JAVA_HOME. You need to add %JAVA_HOME%/bin to the “path” environment variable. You can verify if Java is set up correctly by typing the following command in a command prompt.

Screen shot 2014-10-19 at 4.41.52 PM

If the version is not displayed, then Java environment is not set up correctly.

Create a simple Java program

Step 1: Create a text file named “HelloWorld.java” in folder “C:\projects”

Step 2: Compile the program with “javac.exe“, which creates an executable byte code file named “HelloWorld.class“.

Step 3: Run the byte code file HelloWorld.class without the “.class”

Output:

We will look at the CLASSPATH variable later, which is used to maintain a list of directories (containing many Java class files) and JAR file (a single-file archive of Java classes). The Java Compiler (i.e. javac) and Java Runtime (i.e. java) searches the CLASSPATH entries for Java classes referenced in your program.

Configure Java to run from any folder for a given DOS session

Once you exit the DOS command window, the Java environment variables will be lost, and you need to rerun the script “environment-setup.cmd” in every DOS session you open. Alternatively, you can set it globally on windows as shown below.

Step 1: On Windows go to Control Panel –> System –> System properties

Step 2: Add TOOLS_HOME & JAVA_HOME.

Step 3: Edit the Path variable to add “%JAVA_HOME%/bin”

Now, if you open a new DOS console, and run “java -version”, it will be recognized and you don’t have to run the script “environment-setup.cmd” as the variables are globally configured as the Windows operating system properties.

Handy DOS commands

1. To check where Java is installed

In Unix “which Java”

2. To inspect the environment variables “TOOLS_HOME”, “JAVA_HOME”, and “Path”

You can also “echo

Next Java Beginner Tutorial: Getting started with Java packages, projects, classpath & jar files.

(Visited 4 times, 1 visits today)

800+ Java Interview Q&As

Java & Big Data Tutorials

Top