Compiling and Executing Java Programs

We can break the process of compiling and running a Java program into three steps:

  1. Create the program by typing it into FireTXT text editor (You can also use any other text editor such as Notepad) and save it to a file named, say, MyProgram.java.
  2. Compile it by typing "javac MyProgram.java" in the FireCMD shell or any other command interpreter such as Command Prompt, Bash shell etc.
  3. Run (or execute) it by typing "java MyProgram" in the shell.

Before you can compile and run a Java program, you will need to install the Java Platform (JDK) on your computer system.

Creating the program (Editing source code)

You can edit the Java program's source code using the FireTXT text editor. You can open FireTXT in new tab of FireCMD from New Tab sub-menu of the File menu. You can also use any other ordinary editor like Notepad.

The program code must obey Java programming language syntax. Discussing the syntax is not in the scope of this tutorial but you can use the following hello world Java program code for testing.

public class MyProgram
{ 
   public static void main(String[] args)
   { 
      System.out.println("Hello World!"); //Display the text
   }
}

Important: The filename must end with ".java" e.g. MyProgram.java. Make sure the main method class name (i.e. "MyProgram" in the example above) and the filename must be the same. Also note that Java is case sensitive.

Compiling Java Program

Now we have the source code ready for compilation. If you don't have FireCMD then you can download it from here. Compilation converts the source code into a form (called bytecode) that a Java VM can understand. Any computer with a Java VM can interpret and run the program.

Change your current working directory where the file MyProgram.java exists and just give the following command in FireCMD shell to compile your program:

javac MyProgram.java

If there are no syntactical errors, FireCMD will return you to a new prompt without any error messages which means the code is compiled successfully.

Note: If you receive an error such as "javac was not found!" then you will need to add Java's bin directory to FireCMD's path environment variable. To do this you can give this command: path -a. This will open Browse for Folder dialog box from where you will be able to select the directory. Example: "C:\Program Files\Java\jdk1.7.0_02\bin". Note that the directory path can be different on your system according to the place where the JDK is installed.

Interpreting or Running the program

You can execute the program by simply typing "java MyProgram" at the prompt as shown in the image below.

Compiling and running Java programs