In the last post, I had discussed how to install java. Here I am going to teach you, how to write hello world program in java.
Just like the most famous program, we are going to write “Hello world in java“. In this post, I am going to explain the below
- Writing program in java
- Compiling a java program
- Running java class in command line
Here is hello world program in java
line 1: package com.kgcorner;
We start the java program by writing a package. A java package consists of multiple classes. It’s a way of organizing classes in a huge application. The naming convention for a package is the reverse order of the domain of the organization. My domain is kgcorner.com hence I have used com.kgcorner.
line2: public class HelloWorld
HelloWorld is our class. Java is an Object-Oriented programming language and all codes are supposed to be written inside a class. Hence we start by defining a class.
line3: public static void main(String args)
Inside the class we define methods. Methods execute to compute or create something. A static method named main which returns void and takes a single string array as a parameter is the entry point to program in java. When I will run the compiled code, this function will execute.
line4: System.out.println(“Hello World”);
This line will print Hello world in standard output. Usually, it’s a console, so it will print hello world on the console. Just in case you are not sure “What is console” then let me tell you. By console, here I mean either terminal (in case of Linux or Mac) or Command prompt or power shell (in windows). Basically where you will be executing the java class
So that was all about the program. If you are unable to understand anything then please ask in the comment section.
How to compile a java class
In order to compile a java class, you need to have JDK. In case you do not have JDK please read how to install java.
So first we will check whether java is available or not and then we will compile java.
How to check whether java is installed or not
Open terminal if you are running Linux or Mac and Open command prompt if you are running Window
Type below code and hit enter
This should return something like
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
If the above code returns something like “command not found”. Then java is not installed. So, first, install JDK.
Once you have installed java, cd into the location where you have stored the HelloWorld.java file and run the below code to compile the java code
javac -d . HelloWorld.java
The above command should not give any output on the console but it will create a directory named com and then directory kgcorner inside the com directory. and HelloWorld.class inside kgcorner directory
HelloWorld.class contains the bytecode that can be run by JVM.
What If I do not use “-d” option
If you simply run
It will also create a HelloWorld.class file but in the same location and When you try to run the class file as I will explain in the next point it will give the below error
Error: Could not find or load main class com.kgcorner.HelloWorld
It gives error because it will search for file HelloWorld.class in path ./com/kgcorner/HelloWorld.class. Since this path doesn’t exist hence it will not be able to load the class and eventually won’t run the program. -d option create this path and place HelloWorld.class in right place.
How to Execute java class
Run the below command to execute HelloWorld.class
The above code should print Hello World on the screen.
So that’s it, that’s how we write and execute a simple hello world program in java. If you have any doubt about it, please feel free to ask in the comment section.
Frequently asked question
What is Command to execute a compiled Java program?
Below is the command to execute compiled java program
<package-name>.<class-name> is called FQN(fully qualified name). If the FQN is com.kgcorner.HelloWorld then command would be
Above command would expect HelloWorld.class file at path ./com/kgcorner/HelloWorld.class
Java compiler translates source code into which format?
Java Compiler compiles the java source code into something called bytecode. Bytecodes are instructions that are understood by JVM. These instructions look similar to Assembly codes. If you want to see the actual instructions, use the below command
javap -c <path to class file>
I have a 4gb RAM which text editor and compiler should I use for C and Java programming?
For C and java both, you can use any text editor like notepad, notepad++. In Linux you can use gedit or kate. For C I suggest using gcc for compilation and for java you need to have JDK from any vendor.
Explain console commands to write and compile Java code with Example.
Please check the above post for steps to write compile and execute the java program. In Short below are steps
step 1. Write and save the java file
step2: use the command “javac -d . <path to java file>” to compile the java file
step 3: use the command “java <FQN of Class>” to run the compiled java class
Does online Java compiler need JDK to be installed in our machine for Java code to run or is it like Saas?
Online compilers are Saas. They take code as an input and compile and execute on the server and serves the output of the program as the response.