Writing our first JAVA program…

Now that JDK is installed and Environment Variables are set up in our machine, lets try to create and execute a simple “HelloWorld” program using Command Prompt or Terminal.

  • Using a text editor like Notepad or TextEdit write a simple program and then save the file as “HelloWorld” with .java extension. You can save it in any location.
screen-shot-2017-02-13-at-10-40-51-pm
HelloWorld in text editor
  • Open your Command Prompt or Terminal and go to the location where your .java file is saved.
  • Type in “javac -d . HelloWorld.java” in the Command Prompt or Terminal. Here “javac” refers to JAVA Compile command. As you remember in our previous post, we set our PATH to JDK Bin in our machine. Bin folder contains the executable files for “javac” and “java” commands. “-d” refers to Directory where “.class” files will be stored. When we run “javac” command, the javac compiler compiles your .java source files into .class files, and so that JavaVM can execute (interpret) those files. “.” refers to current working directory and “HelloWorld.java” is the file that we just created.
  • As you see we created a package “com.selenium.test” in our first JAVA program. What is a package? I know many will have this question. A package in JAVA can be defined as a grouping of related types like classes, interfaces, enumerations and annotations and providing them access protection and namespace management. In simple terms, it will store the source file inside a folder structure. Here in this case, folder structure will be created in the form of com -> selenium -> test. When we run this command, system will automatically create a folder structure com -> selenium -> test and store the “.class” file inside the “test” folder.
screen-shot-2017-02-14-at-12-03-16-pm
com folder is created inside HelloWorld folder
  • Once the compilation is complete, next step is to execute the “.class” file. Type in “java com.selenium.test.HelloWorld” in the Command Prompt or Terminal and hit Enter. This time we don’t have to use the file extension. And as you see below, the execution is success.
screen-shot-2017-02-14-at-12-08-42-pm
Hello World!
  • If we are not using any package is our program, then we can simply go to our source folder and run the below commands.

Screen Shot 2017-02-14 at 12.14.23 PM.png

Additional Notes:

Command Prompt is Windows based DOS application and Terminal is Mac Os based Linux application. The purpose is same but the commands are bit different. You can get a complete list of required commands for both Windows and Mac machine from below URL:

DOS Linux Commands

 

 

Setting up Environment Variables…

Once the JDK installation is complete, the next big step is setting the Environment Variables in your machine. Environment Variables setting is different for Windows and Mac machines. I will walk you through in setting up the Environment Variables in both machines.

Before setting up the Environment Variables, let me give you a basic idea on what are Environment Variables?

Environment variables are, in short, variables that describe the environment in which programs run in.Examples of environment variables in an operating system include PATH, HOME etc.

To execute JAVA console based programs in Windows or Mac environment we have to use javac and java commands. These commands are unknown for Windows or Mac till we do not specify explicitly where those executable resides. This is the reason while setting the PATH we specify path of bin folder(bin contains all the binary executable).

Mac OS:

  • Open Terminal in Mac OS similar to command prompt in Window
  • Type in the following command  “vim .bash_profile”. This will open the VIM editor in terminal. bash_profile is a configuration file for bash shell. When .bash_profile is invoked as an interactive login shell it first reads and executes commands from ~/.bash_profile. This file can be used to export variables in shell.
  • Type ‘i’ to insert into the VIM Editor.
  • Type in  “export JAVA_HOME=$(/usr/libexec/java_home)” to set up JAVA_HOME.
  • Type in “export PATH=${JAVA_HOME}/bin:$PATH” to set up PATH.
  • To Save the entires in VIM Editor, hit Escape, type in: “:wq + Enter”. To Quit without saving the editor, hit Escape, type in “:q!”.
  • Type in “source .bash_profile”. This command will save the entries in .bash_profile.
  • Type in “echo $JAVA_HOME” to verify JAVA_HOME Environment Variable.
  • Type in “echo $PATH” to verify PATH Environment Variable.
Screen Shot 2017-02-11 at 9.51.56 PM.png
VIM Editor in Mac

Please ignore M2_HOME as of now. This is for setting up the MAVEN Home which I will explain in future blogs.

Windows OS:

  • Type “advanced system settings” in the search box (beside the Windows start button), clicks View advanced system settings.
  • Select Advance tab, clicks Environment Variables.
  • In System variables, add a new “JAVA_HOME” variable and point it to the JDK installed folder which by default is “C:Program FilesJavajdk1.8.0_111”.
  • In System variables, find “PATH”, clicks edit and append this “%JAVA_HOME%bin” to the end.
  • Type in “echo %JAVA_HOME%” to verify JAVA_HOME Environment Variable.
  • Type in “echo %PATH%” to verify PATH Environment Variable.
Screen Shot 2017-02-11 at 10.10.02 PM.png
Environment Variable Setting in Windows 1o

Now that we have completed the Environment Variable settings in our machine, we can move on to the next topic…

Please note that you need to be an Admin in your machine or else you won’t have the privilege to set the Environment Variables.

Installing Java Development Kit…

Before you start working with Java, the first step is to check whether Java is already installed on your machine or not. There are different ways to check if Java is already installed.

The easiest way is to open the Command Prompt/Terminal in Windows/Mac machine and type in “java -version” and hit enter. If Java is already installed on your machine, then below message will be displayed as you hit enter.

screen-shot-2017-02-10-at-10-30-04-am
Terminal in Mac Machine

If Java is not installed, then below message will be displayed in Command Prompt or Terminal:

“‘Java’ is not recognized as an internal or external command”.

This is good news as now we will start with JDK installation…

Open Browser and type in “jdk download”. Click on the Java SE Development Kit 8 link to go to Oracle Website download section. Java 8 is the latest version. However, lower versions are also available for download. For now we will download the Java 8 version.

Once you are in below downloads page, select one of the options depending on the machine you are using. Depending on your Windows machine 32 or 64 bit, select Window x86 or Windows x64 option or else if its Mac, select the Mac OS X option. Click to download the exe or dmg file into your machine.

screen-shot-2017-02-10-at-12-13-44-pm
Oracle Downloads Page

Once after the successful download, click on the file in the Download folder of your machine to proceed with the installation. Installation is straight forward and you will be taken through various prompts to complete the installation. In Windows machine, JDK and JRE will be installed in C:Program Files folder. In Mac, it will be in /Library/Java/JavaVirtualMachines folder.

Once after complete installation of JDK, you can confirm the installation by going to Command Prompt or Terminal and type in java -version” and hit Enter.

And now the JDK installation is complete, we will move on to the next topic…

 

 

Design a site like this with WordPress.com
Get started