Sunday, June 14, 2009

Your first Java application

Creating a Source File HelloWorld

• Any text editor can be used to create a Source file
– E.g. Notepad,word pad
• Type the following on your text editor

class HelloWorld
{
public static void main (String[] arguments)
{
System.out.println (“Hello World!!!”);
}
}

• Save the file as “HelloWorld.java” in a convenient folder (E.g.
“C:\MyJava”).
• Source Files should be saved with a .java extension. The Java
compiler will only handle source files with this file extension.

Compiling your Source File

• I assume that you have saved your source file as
“C:\MyJava\HelloWorld.java”, and you have correctly set the PATH variable.
• In the command prompt and set “C:\MyJava” as the current directory.
– You can do this by typing: cd C:\MyJava on the command prompt.
• Type: javac HelloWorld.java on the command prompt.
• If you have followed the steps correctly and all is well, you should see no
output. If not, you might see some error messages. If so, go back and recheck
your work.
• As we mentioned, javac.exe is a compiler, which converts java source code
into Java bytecode. Here, it complies the java source code in
“HelloWorld.java” and saves the resulting java bytecode as
“HelloWorld.class” in the same directory (E.g.
“C:\MyJava\HelloWorld.class”).

Executing your Java Bytecode File

• Type: java HelloWorld on the command prompt.
• You should see “HelloWorld!!!” displayed.
• As we mentioned, java.exe is an interpreter, that plays the role of the Java Virtual Machine converting the java bytecode into the machine code of the host computer and executing it.
Here, it takes the java bytecode in “HelloWorld.class” and interprets it.

No comments: