Create a new file in your text editor or IDE named HelloWorld.java. Then paste this code block into the file and save:

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

Run live on Ideone

Note: For Java to recognize this as a public class (and not throw a compile time error), the filename must be the same as the class name (HelloWorld in this example) with a .java extension. There should also be a public access modifier before it.

Naming conventions recommend that Java classes begin with an uppercase character, and be in camel case format (in which the first letter of each word is capitalized). The conventions recommend against underscores (\\_) and dollar signs ($).

To compile, open a terminal window and navigate to the directory of HelloWorld.java:

cd /path/to/containing/folder/

Note: cd is the terminal command to change directory.

Enter javac followed by the file name and extension as follows:

$ javac HelloWorld.java

It’s fairly common to get the error 'javac' is not recognized as an internal or external command, operable program or batch file. even when you have installed the JDK and are able to run the program from IDE ex. eclipse etc. Since the path is not added to the environment by default.

In case you get this on windows, to resolve, first try browsing to your javac.exe path, it’s most probably in your C:\\Program Files\\Java\\jdk(version number)\\bin. Then try running it with below.

$ C:\\Program Files\\Java\\jdk(version number)\\bin\\javac HelloWorld.java

Previously when we were calling javac it was same as above command. Only in that case your OS knew where javac resided. So let’s tell it now, this way you don’t have to type the whole path every-time. We would need to add this to our PATH

To edit the PATH environment variable in Windows XP/Vista/7/8/10:

You cannot undo this so be careful. First copy your existing path to notepad. Then to get the exact PATH to your javac browse manually to the folder where javac resides and click on the address bar and then copy it. It should look something like c:\\Program Files\\Java\\jdk1.8.0_xx\\bin

In “Variable value” field, paste this IN FRONT of all the existing directories, followed by a semi-colon (;). DO NOT DELETE any existing entries.