Using the Java compiler and the Java Virtual Machine on CS Nodes at SEU
-
Create your program using a text editor on your computer system,
at SEU the computer system we use will be referred to as one of
the CS nodes.
Emacs, vi, pico are some of the more common text editors on unix systems.
We suggest using emacs as you can edit/compile/execute easily from within emacs.
Your program should be saved as a file with an extension of .java (for
Java programs).
Remember that any unix operating system is case-sensitive. So
the name you choose for your program file must be used exactly as spelled
and the particular file name should match the class name in your file.
For example, a simple program that prints a greeting hello to the output screen
might be created with emacs and saved as
Hello.java
on your computer system.
-
Now you are ready to compile your program, Hello.java, into
java byte codes ( a .class file)
that you will then run(execute) on the java virtual machine (java VM).
At the operating system command line prompt you give
the command:
javac Hello.java
(Be sure to press enter after typing the command.)
This command instructs the java compiler (javac) to compile your source file,
Hello.java, into java byte codes that can than be interpreted (executed)
on the java virtual machine (Java VM). This compilation step should create
a file named Hello.class in your directory, this is the file
containing the java byte codes. So there are really 2 separate steps,
the first is to use the java compiler (javac) to create a file (a
.class file) that can
then be interpreted (run or executed) on the java virtual machine.
If any errors occur in the original source code file (Hello.java)
then you will see them appear on the screen.
You must correct any errors with your text editor(emacs) and compile
the program again. You repeat steps 1 and 2 until no errors are
reported and your .class file is successfully created.
- To execute the compiled program (the .class file)
the command is: java Hello
Summary of Commands to create/compile/execute a Java program
- Use the emacs text editor to create your program in a file,
save the file as a java program, ie. Hello.java
- Compile your java program with this command:
javac Hello.java
- Execute your compiled program (your .class file) on the
java virtual machine
java Hello