HomeJava

Java – Getting started with First Program

Java – Getting started with First Program
Like Tweet Pin it Share Share Email

In this post you will learn about how to get started with Java and How to write your first program?

For the first time, you need to do make your machine ready to compile and run Java programs, lets go through

Steps to configure your machine or laptop

  1. Download & Install Java
  2. Set path variable in environment variables

So now your laptop is ready to get started with following:

  1. Write your first Java program using any simple editor like Notepad
  2. Compile your program
  3. Execute the program and view the program output.

Lets now configure your machine in case its not done yet

  • Follow the step by step instructions to download and install latest java
  • Once Java is installed in your laptop / machine, open a command prompt and change directory to the location where Java was installed and change directory to bin directory under Java. At the command prompt type the command “javac” without double quotes and press enter, you should see something like below:
Javac works from bin directory
Javac works from bin directory
  • Now close the command prompt window and launch command prompt again and make sure you are not at the folder location where we executed javac command in earlier step, for example keep the command prompt at “C:\” it self by giving the command “cd \” without double quotes in the command prompt and press enter.
  • Try giving the “javac” command now and you will see the error “‘javac’ is not recognised as an internal or external command, operable program or batch file.” as seen below:
Error when java path is not set in path variable
Error when java path is not set in path variable
  • The reason for this to happen is that, the java bin directory is not set in your path variable, i.e. windows does not know from which folder it has to pick up these java related programs if you want to use it from different directories in the file system.
  • So, once this path is set in environment variables, you would be able to use the java related commands in command prompt from any location / directory. The reason why you want to do this is because, you will be forced to write all your java programs only in the “installation directory\bin” directory and not in any other location in the windows file system. This can limit you to store all the java related programs in your desired folder locations.
  • Follow the step by step by instructions to set Java Path and make your machine ready to compile and run java programs from your desired location.

Lets now write the first program in Java

  • Open a notepad in your machine.
  • Paste the below code in the notepad
     
    public class FirstJavaProgram
    {
    
    	public static void main(String rs[])
    	{
    		System.out.println("This is my First Java Program! Hurray!");
    	}
    
    }
    
  • Save the file as “FirstJavaProgram.java”
Sava Java Program
Sava Java Program
  • Open command Prompt
  • Change your working directory to the place where FirstJavaProgram.java file resides
  • Compile the program using below command.
Compile Java Program using Javac command
Compile Java Program using Javac command
  • Run the program using below command.
Run program from command prompt using java
Run program from command prompt using java
  • You would see an output in the command prompt saying This is my First Java Program! Hurray!

Now that you have learnt how to write your first program, compile and run, in the following weeks will explain what each statement and instruction mean in this program.

Until then if you have any queries, please do share it through comments, we would be more than happy to help.

Comments (0)

Leave a Reply

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