Scroll to top

Java Basics:Getting Started with Java

Here we will discuss some basics topics related to Java.

  1. Where to download Java.
  2. How to install Java.
  3. Setting up the Environment Variables.
  4. Our First Java Program.
  5. How to compile a Java application.
  6. How to run a Java Application.
  7. Difference between important terms in Java (JDK vs JRE or J2SE vs J2EE..).

 

How to Download Java

The latest version of Java can be downloaded from Java Website.

Java Installation

There are no special requirements when installing Java.  Be sure that you have suitable permissions on your computer to install software. It can be installed like any other software (.exe).

Setting up the Environment Variables

After installing Java there are some environment variables that need to be set.

  • CLASSPATH: This environment variable points to the location of the JDK home directory. It also contains the address of folder from where jars get loaded by ClassLoader (For more details of ClassLoader visit here)
  • JAVA_HOME: This environment variable will point to the location of the Java home directory.

How to set an environment variable in different platforms

Windows XP

To set up environment variables in Windows XP right click on the “My Computer” icon and select Properties. In the  Property window select the “ADVANCED”  tab and click on “ENVIRONMENT VARIABLES”. A window will appear where you can enter a new environment variable under System Variables by selecting New button.

Windows Vista / 7

To set up environment variables in Windows Vista / 7  right click on “Computer” and select Properties. In Property window select  “ADVANCED SYSTEM SETTINGS” and then select “ADVANCED” tab and click “ENVIRONMENT VARIABLES”. A window will appear where you can enter new environment variable under User/System Variables by selecting New button.

How to Check if Java is Installed

To check if your Java is installed properly open Command Prompt. To open command prompt write “CMD” in run command and hit enter. In the command prompt window write “java -version“.  If your Java is installed properly and all environment variables are configured correctly it will show the version of Java installed. Information reflected on the command prompt will be like

C:\Users\Jbt>java -version
java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) Client VM (build 25.51-b03, mixed mode, sharing)

If there is any problem while installing or in setting up the environment variable, the output on the command prompt will be as shown below.

'java' is not recognized as an internal or external command,
operable program or batch file.

How to check if Java is up to date

To know if the Java installed on your system is up to date Click Here.

Our First Java Program

Though the popular first program to write in Java is” Hello World !! “, here we will write a program to print ” Hello JBT !! “. 🙂

Open an editor and write the below code.

public class FirstProgramme {
	public static void main(String args[]) {
		System.out.println("Hello JBT!");
	}
}

Save the file with the name “FirstProgramme.java” in folder “c:\jbt”. Please note that name of the file should be same as the name given to public class(For more details regarding class file rules click here). Once the file is saved, open the command prompt and change the working directory to “c:\jbt”, where your file is saved. Write “javac FirstProgramme.java to compile the Java code as below.

C:\Users\JBT>cd C:JBT

C:JBT>javac FirstProgramme.java

C:JBT>

If the java file is compiled properly the compiler will create a class file for the source java. It will be saved in the same location as the source file. Since no package is declared in the given code the .class file will be created in the same folder location.

Notice the difference for Java files using package declaration. Let’s create another Java file as below.

package com.jbt;

public class FirstProgrammeWithPackage {
   public static void main(String args[]) {
   System.out.println("Hello JBT!");
  }
}

Save this file as “FirstProgrammeWithPackage.java” to “c:\jbt”. Now go to the command prompt and execute the below command.

javac -d . FirstProgrammeWithPackage.java

It will create the class file in a corresponding package(com.jbt).

Now that you know how to compile Java file with a package and without a package, the next step would be run these class files.

How to Run Java Application

Now that your  Java file is compiled we can execute the application using the “java” command as below.

Without Package

C:\JBT>java FirstProgramme
Hello JBT!

C:\JBT>

With Package

C:\JBT>java com.jbt.FirstProgrammeWithPackage
Hello JBT!

Or

C:\JBT>java com/jbt/FirstProgrammeWithPackage
Hello JBT!

 

Note*: The “java” command uses the class file name without its extension(.class).

With this, we are done creating and running our very first Java application.

In the next section, we will learn the difference between JDK and JRE.

Author avatar
JBT
https://javabeginnerstutorial.com/

175 comments

  1. aparna

    Thanks for sharing this information

Post a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.