Java Basics:Getting Started with Java

This entry is part 1 of 34 in the series Core Java Course

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

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 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 Java home directory.

How to set 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 were 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 were 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

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

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 any editor and write the below code.

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.

If the java file is compiled properly the compiler will create a class file for  the sourceJava. 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.

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

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

Now that you know how to compile Java file with package and without 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

With Package

Or

 

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.

Series Navigationjdk vs jre vs jvm >>

173 Comments Java Basics:Getting Started with Java

    1. J Singh

      package is way of organizing java classes inside a project. You needs to have unique class name inside a package name. But class with same name can exist in different packages.

      Reply
  1. Komal

    This tutorial is very helpful as I have just started learning ,I am getting things in correct manner ,and by following steps that are mentioned in the article, I’m getting things very easily and definitions are also very clearly understood.. :-)

    Thanks & Regards,
    Komal

    Reply
  2. Vivek

    can i start learning java core even if i dont know C language or any basic concepts of c
    like even loops or syntex or even 0.% knowledge of c
    CAN YOU RECOMMEND ME TO START JAVA CORE ?????

    Reply
  3. Prabhavathy. S

    I am graduated in finance and know nothing about computer programming. But I am so interested in becoming a software engineer. Can you please tell me if learning java will help me for the same?

    Reply
    1. J Singh

      Hi Prabhavathy,

      I really don’t know the answer of this question. Just knowing java will not solve all your problem. But it will help you definitely. And learning java is not difficult unless you have good teacher.

      Regards

      Reply
    2. Srinivasa Tupurani

      I would like to share my real time experience here. I would say if you love programming go for it. Learning Java, gives you strong foundation for any IT professional line you desire. There are so many opportunities in IT like Business Analyst, Quality Assurance where one would expect to bring programming skills and Java is one among them. Within QA there are Automation programming tools like selenium. You can take this knowledge (Programming skills) and apply these skills in learning any other tool or building a Programming career (Entry level). Hope this helps!

      Reply
  4. Srinivasa Tupurani

    I accidentally landed on your page while I am googling for Java Tutorial for my nephew.
    I have gone over this blog. The Tutorial was been built with very good thought process.. I would say If one spends their time, they certainly end up with strong foundation level in JAVA which I feel many of the IT professionals certainly need.

    Reply
  5. Qlems

    Am a student of medicine who is trying to learn JAVA and belief you me , your tutorial is exceptional . I really love the systematic explanation.
    On point. We need people like YOU to help us understand software environment.

    Reply

Leave A Comment

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