http://www.technicalpage.net/search/label/SQL

>> How to start Java with Eclipse and print Hello World

Hello World 

In this tutorial, we will learn how to start Java with Eclipse and will write a simple java code to print  "Hello World" .

To watch this in YouTube , please visit :
https://www.youtube.com/watch?v=FOiy-GGeEqU&t=46s 
 

To begin with java, you need to install latest version of JDK(Java Development Kit) and the editor: Eclipse IDE(Integrated Development Environment). You need JDK and JRE(Java RunTime Environment), but installing JDK will automatically install JRE too. So you do not need to separately install JRE. 

After installing the JDK, check your folder "C:\Program Files\Java", you will see a folder for jdk. Open the jdk folder and copy the path such as: "C:\Program Files\Java\jdk-14.0.1"

 

Again , click the bin folder inside jdk folder and copy the path such as: “C:\Program Files\Java\jdk-14.0.1\bin”

 

Now to set up environment variable, open the environment variable:

Open Control Panel then click System and Security then click System

click the link : Advanced System settings

click the tab : Environment Variables

 

Inside System Variables: click "Path" and click "Edit" and click "New"

Paste the path “C:\Program Files\Java\jdk-14.0.1\bin” and click "OK"

 

Inside System Variables: click "New" and enter

Variable Name = JAVA_HOME , remember this should be in Uppercase letter

Variable Value = "C:\Program Files\Java\jdk-14.0.1"

Click "OK" and click "OK" on System Properties window

 

To make sure the configuration is done properly:

Open CMD prompt(remember , you should close the cmd prompt if it was open already, and then open it again)

Type java  -version and hit enter

It should show the java(jre) version , remember if it does not show the version then the configuration is not done or it is not successful or incomplete

 

Type javac  -version and hit enter

It should show java(jdk) version

 

Type java   and hit enter

It should show java properties

 
 
 
Installation of Eclipse editor which is required to write java code:
 
You can download  "Eclipse IDE for Java EE Developers" or "Eclipse IDE for Java Developers" or "Eclipse Classic". You just download and unzip folder and you can use the eclipse, there is no need to install.

To start java coding, open the eclipse. While opening eclipse, you have to give the workspace location/path where your java codes will be stored.

Once eclipse is open, if you get welcome screen(This screen comes up when you open eclipse first time.) , just close that screen.

Welcome Screen


1. You need to create a project first, for that :


After clicking FINISH, if you see a pop up "Open Associated Perspective?", click YES or Open Perspective.



Now, You will see that a project has been created on the very left hand side.

If you click the toggle( small triangle shaped icon ) next to the project name, you see two folders "src" and "JRE System Library". "src " is source folder where your codes will be stored.



Next step is to create a package. For this, 


Package is created.




Next step is to create a class. For this,

Class is created.
At this point , you will see an EDIT Window . You have created a java project, a package, a class and now you are ready to write code on the EDIT Window.

Let's say  : Java project name = "HelloWorld"
Package name = packagehw
Class name = classHelloWorld
On the EDIT Window, you will see below code :

package packagehw;

public class classHelloWorld {

      
}

You will write your code inside the two curly bracket {  }

EDIT Window


Write code to print "Hello World !"
You write all the methods and the code inside the curly bracket {} . To  print "Hello World !" , you have to write a print statement inside  "main method " as displayed below. The main method has to be created inside the curley brackets ,ie, inside the class. The over-all code looks like below:

package packagehw;

public class classHelloWorld {

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

Here, System.out.println("Hello World !"); is the print statement inside the "main mehtod".

When you run this code, it prints "Hello World !" in the output.
To run this code, you need to first save this code. Click SAVE icon, and then click the RUN icon( little circle icon having a small triangle in it). The code will run and the output will be displayed as "Hello World !"  


Full code and output

No comments:

Post a Comment