News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

Java Tutorial – 03 – Compile and Run

Running from the IDE

With your Hello World program complete you can compile and run it in one of two ways. The first method is by selecting run from the menu bar of the IDE that you are using. In Netbeans the menu command is: Run→Run Main Project. The IDE will then compile and run the application, which displays the text “Hello World”.

Running from a console window

The other way is to manually compile the program by using a console window (C:\Windows\System32\cmd.exe). The most convenient way to do this is to first add the JDK bin directory to the PATH environment variable. In Windows, this can be done by using the SET PATH command, and then by appending the path to your JDK installation’s bin folder separated by a semicolon.

SET PATH=%PATH%;"C:\Program Files\JDK\bin"

By doing this the console will be able to find the Java compiler from any folder for the duration of this console session. The PATH variable can also be permanently changed. Next, navigate to the folder where the source file is located and run the compiler by typing “Javac” followed by the complete filename.

javac MyClass.java

The program will be compiled into a class file called MyApp.class. This class file contains bytecode instead of machine code, so to execute it you need to call the Java Virtual Machine by typing “java” followed by the filename.

java MyClass

Notice that the .java extension is used when compiling a file, but the .class extension is not used when running it.

Comments

Comments are used to insert notes into the source code and will have no effect on the end program. Java has the standard C++ comment notation, with both single-line and multi-line comments.

// single-line comment
 
/* multi-line
   comment */

In addition to these, there is the Javadoc comment. This comment is used to generate public class documentation by using a utility included in the JDK bin folder which is also called Javadoc.

/** javadoc
    comment */
Recommended additional reading:
Sams - Teach Yourself Java in 24 Hours