Below it
will be described how you write a Java program using a text editor and execute
the program using a command prompt.
Start the
text editor Notepad. It
is available from the menu Start via the menus All Programs and Accessories. Adjust the window of the editor
such that it approximately occupies the upper 2/3 of the computer screen.
Select Command Prompt from the menu Start via the menus All Programs and Accessories. Thereby a window with a command
prompt will appear where commands to the computer can be typed. To begin with,
the window of the command prompt contains the following.
...
C:\Documents and Settings\torben>_
Of course, your
own user name will occur in the window instead of torben. Adjust the window of the command
prompt such that it approximately occupies the lower 1/3 of the computer
screen.
Using the
command prompt, generate a new folder with the name my_programs. This is done by typing the command
md my_programs in the window of the command prompt
and then pressing Enter at the keyboard. After the command md my_programs is executed, the window of the command prompt contains the following.
...
C:\Documents and Settings\torben>md my_programs
C:\Documents and Settings\torben>_
The command
prompt is still operating on the folder Documents and Settings\torben at the C drive of the computer, but it has to operate directly
on the folder my_programs. This is accomplished by typing the command cd my_programs and then pressing Enter at the keyboard. After this command
is executed, the window contains the following.
...
C:\Documents and Settings\torben>md my_programs
C:\Documents and Settings\torben>cd my_programs
C:\Documents and Settings\torben\my_programs>_
Now, type the
following Java program in the window of the text editor.
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello, World");
}
}
Save in the
following way the typed program as a file in the folder my_programs: Select Save as
from the File menu
left-most at the top of the editor. Thereby a dialogue window pops up where the
name Hello.java is typed and where the folder my_programs is selected (you go upwards in the file system using the Save in: menu at the top of the dialogue box
and you go down by clicking at the desired folder in the dialogue window).
Select All Files
in the Save as type:
menu at the bottom of the dialogue window. Finally, click at the button Save in the dialogue window whereby the
program is saved as a file with the name Hello.java, and the dialogue window
disappears. Note that what is immediately before .java in the name of the file is
identical to the name of the class in the program (this MUST be the case).
Type the
command javac Hello.java in the window of the command prompt
and press Enter at the keyboard. Thereby the
program in the file Hello.java is compiled and the compiler
generates a file with the resulting bytecode. The
generated file has the name Hello.class and is located in the folder my_programs (the same folder as where the file Hello.java is located). After execution of the
command javac Hello.java, the window contains the following.
...
C:\Documents and Settings\torben>md my_programs
C:\Documents and Settings\torben>cd my_programs
C:\Documents and Settings\torben\my_programs>javac Hello.java
C:\Documents and Settings\torben\my_programs>_
Now, type
the command java Hello
in the window of the command prompt and press Enter at the keyboard; note that you have
to type Hello, not Hello.java. Thereby the program is executed, that is, the bytecode
in the file Hello.class is interpreted. After execution of the command
java
Hello, the window
contains the following.
...
C:\Documents and Settings\torben>md my_programs
C:\Documents and Settings\torben>cd my_programs
C:\Documents and Settings\torben\my_programs>javac Hello.java
C:\Documents and Settings\torben\my_programs>java Hello
Hello, World
C:\Documents and Settings\torben\my_programs>_
As it can be
seen above, the result of executing the program is that the text Hello, World is printed at the screen.
You can
move around in the file system using the command prompt as follows: You go a
step up in the file system by typing the command cd .. in the
window of the command prompt and thereafter pressing Enter at the keyboard. You go a step down in the
file system by using the command cd as described above. Using the
command dir you can
see which folders and files that are located the actual place. Another remark:
Instead of using an editor and a command prompt, an integrated development
environment (abbreviated IDE) can be used to write and run Java programs, an example is JCreator which is supported by Computer
Science at