Java from the Command Line
This tutorial covers how to edit, compile and run Java programs from the command-line

Step 0:  Install the Java SDK (Software Development Kit)
Step 1:  Use Bash (Terminal on Mac OSX or Linux) or on Windows: DOS or PowerShell

The Process of Java via the Command Line 
  1. Edit the .java file
  1. Use javac to compile the .java into a .class file
  1. Fix CLASSPATH issues
  1. Use java to run the run .class file
  1. Fix CLASSPATH issues

Editing Java

First, we have to create the Java source code file. This is a plain-text file that uses the extension .java . There are a variety of text editors. Text editors are used to create plain text files (i.e., not word documents or rich text documents). I recommend Sublime, Emacs, Vim, or Atom. Like many editors, and unlike some, these four text editors are suited for reading & writing code because they provide syntax highlighting, line numbers, and are configurable and extensible.

Use a text editor to create an Empty class definition in a file named Empty.java :

public class Empty{
}

  • Note the folder (or directory) where you saved that file. It will be easiest to save it in your home directory for now.

Compiling Java

Next we have to compile the java source file. We have to temporarily leave the editor for this step. The compiler will create a file of bytecode, the intermediate form of a java program. Assuming you have saved the `Empty.java` in your home directory, start a terminal, and enter:

javac Empty.java

Alternately, if you saved Empty.java in another folder, for instance, named  Documents you first have to change to that directory before compiling:

cd Documents/
javac Empty.java

Once you have run javac , it should have created a file named Empty.class that includes the Java byte code.  

If this did not happen, two things may have gone wrong:

  1. javac wasn't found: verify you installed the java development kit (step 0 above) and javac is in your PATH
  1. Empty.java not found: verify you named the file exactly Empty.java and you are running javac in the correct folder.

We can verify the compilation worked by disassembling the class file. This is pretty neat: we are essentially de-compiling the class file to recreate the original source code (or something like it).

javap Empty

gives us:

Compiled from "Empty.java"
public class Empty {
  public Empty();