javasetup

Hello World!


This tutorial will guide you to write,compile,execute your first java programme.



In this section we will not use any IDE like eclipse,netbeans..etc instead we will be using a text editor to write the code and use command prompt to compile/execute.



1. Prerequisites


Tools Used
  A text editor    [we are using notepad++]

JDK
  Install any JDK.     [We are using JDK 1.8]


	
Now we are all set to write our first java code. Open Notepad++ and follow the steps.

2. Code


Write a java class containing a main method.

To do that Follow the steps below , read inline information.

Step 1
Hello Java
The class FirstApp contains one method : main()
Inside main method, we are just printing "Hello World" in console.

Step 2
Hello Java
Now save the class with name FirstApp.java

	
Our java programm is ready. Now we have to compile it and run it.

3. Compilation


  • To compile a java code we need to have jdk installed in our system. Here we have installed JDK 1.8[mentioned in Prerequisites section].
Steps to compile the java class FirstApp.java given below. Read inline instructions.
Step 1
Hello Java
Open cmd. Go to the FirstApp.java file location.
Write the command javac FirstApp.java and hit enter.
You may face the error shown in picture 'javac' is not recognized.
To resolve this we need to set JAVA_HOME and PATH in our system.


  • Open Environment Variables window to set JAVA_HOME and PATH.
Step 2
Hello Java
Go to Control Panel-->System and Security-->System
Click on Advanced system settings
System Properties popup will open
Click on Environment Variables..., Environment varibales popup will open.


  • Set JAVA_HOME.
Step 3
Hello Java
Now in User variable section Click on the New button(highlighted).A popup Edit User variables will open.
Write the variable name as JAVA_HOME and the value as the location of installed jdk1.8 inside Program Files.
Click on ok... and JAVA_HOME is created.


  • Set PATH.
Step 4
Hello Java
Find Path in System variables section, Click on Edit button.

Step 5
Hello Java
Append the jdk1.8/bin folder location in Path varibale delimitted by semicolon(;).
Click on OK button.

	
Now we are good to compile the java class.

  • Compile the class. Check the steps below. read inline instructions.
Step 1
Hello Java
Now Write the command javac FirstApp.java and hit enter again in cmd.
This time no error will come.

Step 2
Hello Java
Now go to the folder where you have saved the FirstApp.java file.
After compilation you will see FirstApp.class file will be generated.
	
Now we have successfully compiled our java code. Next step is to run it.

4. Run



Step 1
Hello Java
Go to cmd again and write java FirstApp and hit enter.
Hello World will be printed on the cmd.

	
That's it, we have successfully completed our first java application.

5. Code


This application contains one file :

1. FirstApp.java - A basic java class to print some message in console.

FirstApp.java

public class FirstApp{

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



In this section we will use an IDE to write our first java application.



1. Prerequisites

Before we start coding let us first tell you the tools we used and the libraries we need for a java application.

Tools Used
  Eclipse     [Any version 4.2(Juno) or above . We are using 4.8(oxygen).]

JDK
  Install any JDK.     [We are using JDK 1.8]


	Now we are all set to write our first java code. Open eclipse and follow the steps.
	

2. Create a Java project


Follow the steps below, read inline instructions.

Step 1
Hello Java
Goto File --> New --> Java Project


Step 2
Hello Java
Write the project name.
Check the JRE section(Highlighted yellow box). Make sure installed JRE is selected.
Click on Finish button.


Step 3
Hello Java
Right click on the src directory.
Go to New --> Package as shown.


  • Create a Package.
Step 4
Hello Java
Write Name of the package.
Click on Finish button.


  • Create a java Class.
Step 5
Hello Java
Right click on the package com.javasetup.corejava.
Go to New --> Class as shown.


Step 6
Hello Java
Write Name of the class.
Click on Finish button.


Step 7
Hello Java
This is the default java class structure that eclipse will generate.
	
Now we are done with creating a java project. Lets start coding :)

4. Code


This application contains following file :

1. FirstApp.java - A java class that prints 'Hello World!'.

  • Review Directory structure of the project just after creating it in below screen.
Project structure in eclipse
Hello Java


  • Write a main method in the created java class FirstApp.java.
FirstApp.java

public class FirstApp{

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



5. Run the application

  • Follow the steps below to run the application.
Step 1
Hello Java
Right click on the java class.
Go to Run As --> Java Application.


  • See the output.
Step 2
Hello Java
Hello World! is printed in the console.


References