Java Environment Setup for Selenium


Java Environment Setup
Java Environment Setup for Selenium


History of Java
Java was developed nearly 20 years back, launched in 1995 by Sun MicroSystems now acquired by Oracle Corporation.
 

Features of Java 

Java is a class-based object-oriented programming language which is
Platform independent – that is it can be used on any Operating System like Windows, Unix/Linux, and Macintosh etc (intended to “write once, run anywhere”  - meaning that compiled Java code(bytecode) can be run on any platform without recompilation)
Open source (free software) – that is under GNU General Public License

Java Environment Setup
 

Download Java from either java.com site or oracle.com site.

First we have to download Java-JDK (Java Development Kit) and install it.

• In JDK we Java compiler command is available.
• From command line prompt also we can execute java programs.
• If we want to write and execute java programs, first we have to set up java environment.
• We can use any editor (note pad, etc.) for writing java programs but for executing we require java environment.

Steps to download Java
 

• From oracle.com select ‘Downloads’ and click on Java SE
• Now in the Java SE page click on the logo
• Select "Accept License Agreement"
• Next select operating environment or operating system

If you have Windows 32-bit OS  then select Windows x86 or if it is 64-bit OS then select Windows x64.

• Click on the download link and download
• After installation, we get ‘Java’ folder in ‘C:\Program Files’ folder

Set Environment Variable Path (Path Variable)
 

• Environment variable path is set in order to access Java software from any directory.

To set environment variable path in MS Windows 7/10

• Select ‘My computer’ and right click
• Select ‘Properties’
• Select ‘Advanced system settings’
• Select ‘Environment Variables’
• Select path variable

• Enter JDK Bin directory path.
• Paste it in the space given for variable value.
• Click Ok-ok-ok.

Note: How to get JDK Bin directory path?

• After selecting system variables,we have to select path variables and click on edit button.
• Then a window appears as edit system variables.
• Here we have to take JDKBin directory path

• Select My computer >c drive>program files>java>jdk>bin

• Copy the path from address bar.

Verifying the Java Environment setup
 

• Launch command prompt
• Type java (it provides details).

Steps to verify
• Select ‘run’, type ‘cmd’ to get command prompt.
• From any directory type java in command prompt, it provides java details.
Or
• From any directory type javac (java compiler), it provides java compiler details.

Writing, Compiling & Executing Java Programs from Command line
 

• In computer programming, we have 3 steps for writing and execution of programs
• Writing, Compiling (compilation means converting high level language into machine level language) & Execution

Step:1 writing Java program
 

• Open ‘notepad’ and write your first Java Program as following

         public class Sample{
         public  static void main (String [] args) {
         System. out. println (“Hello Java”);
         System. out. println (“Hello Selenium”);
   }
   }
-------------
  Syntax  1.class Name (class Name should start with capital letter)
  2.main method (Java program execution starts from main method)
  3.display statement 1(every statement should end with semicolon)
  4.display statement 2(every statement should end with semicolon)
---------------
• Save the program as Sample.java (save it on ‘desktop’ or any folder which is easy to access)
***Note: Java is case sensitive

Step:2 compiling java program
 

• First select command prompt and change to program file directory (‘desktop’ or any folder where you have saved the Sample.java program , type javac program name and run.
• Go to command prompt . c:\......>
• Change program file directory. c:\......>Cd Desktop
• Type c:\......\Desktop>javac Sample.java
• Click on Enter

Compilation is done and we get the class file on ‘desktop’ or any folder where you have saved the Sample.java program

Step:3 Execute or run the java program
 

Now execute the program by typing java and program name on command prompt
• Type c:\......\desktop>java Sample.java
• Click on Enter
• Output is generated as below on console

Hello Java
Hello Selenium

Exit from command prompt type c:\.....\desktop>exit.

Note:

• The program file name and class name should be the same as it is syntax rule.
• Whenever we edit/modify the program, it is to be compiled again
• Once the program is compiled you can run/execute  multiple times
----------------------------------------------------
In Test Automation using selenium, we use an Integrated Development Environment like eclipse IDE, not notepad or command  line for ease and speed.

Eclipse IDE (Integrated development environment)

Eclipse IDE it is a platform to create and execute  programs  like Java, Pearl, Python, Ruby, PHP etc

• Eclipse IDE is open source (Eclipse public license)
• It provides Editor, help for syntax errors ,context help, auto compilation, de-bugging facility etc

Downloading  & Launching Eclipse IDE
 

• Go to google search page, type eclipse IDE download
• Click on eclipse downloads
• Download eclipse IDE for Java EE or eclipse IDE for Java developers
• Extract the zip file to a folder where we find eclipse.exe file, clicking the.exe file will launch the eclipse IDE(No need to install)

Sequence for creating Java program in Eclipse IDE
 

> Create Java project
>Create a package(under Java Project)
>Create Classes , Interfaces(under package)

Steps for creating Java program in Eclipse IDE
 

• Navigation path for creating New Java Project:

 Click on File->New->Java Project

Type Project name - eg ‘Sample’ and click on Finish

• Navigation path for creating New Package under created Java Project:

 Right Click on created Java project ‘Sample’, select New & click on Package

Type Package name – eg ‘selenium Testing’ and click on Finish

• Navigation path for creating New Class under created Java Package:
Right Click on created Java Package ‘selenium Testing’, select New & click on Class

Type Class name – eg ‘JavaProgram’ and click on Finish

Writing & Executing the Java Program using Eclipse IDE
 

Click on the ‘JavaProgram’ & write the Java Program in the Editor( in the Right)
& Click on the Run to Execute the ‘JavaProgram’

Output is generated as below

Hello Java
Hello Selenium

Navigation for Editor Settings in Eclipse (Change Font size etc...)

Editor Text Fonts

Window(Menu)->Preferences->General->Appearance->Colors and Fonts->Java->Java Editor Text Font->Edit

Output Console Fonts Window(Menu)->Preferences->General->Appearance->Colors and Fonts->Debug->Console Fonts->Edit

1 comments:

@G.C Reddy

Hi Reddy sir.

Very good video .. Nice and easy to understand. Very brilliant post for java environment setup.

Post a Comment