Printing Hello World using Java

If this is the first time you’re going to run a Java program, the very first thing you should do is check whether you have Java Development Kit installed on your system. To do this, just enter the following command in the terminal –

javac

If you don’t have JDK installed, it’ll return the following message –

No Java runtime present, requesting install.

If you see this, you can get JDK by a quick google search or by visiting this page.

Once you download JDK, just install it and test the javac command again in the terminal. If it works, you are good to write Java code.

I am using textedit to write code for now. You can use the same (textedit, notepad, anything actually as long as it’s plaintext). If you’re looking to upgrade your text-editor to something more amazing, I’ll recommend using Sublime.

Anyway, coming back to the point. Copy paste the following code in a new textedit document and save it as hello.java on your desktop. (Don’t forget the extension .java).


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

Go back to your terminal. Enter the following command to compile your program –

javac hello.java 

If it doesn’t give any errors, you’re all good to execute the program using this next command (it doesn’t need .java extension anymore) –

java hello 

The output should be Hello World.

Note: If you receive illegal character as errors while compiling the program, just switch to a different text-editor like sublime and that’ll fix the issue.

I’ll explain (or try to explain) how all of this works in my next post!