Getting Started
Java is in /usr/local/java/bin; make sure that this directory is in your load path.
Use javac to compile java programs as in
prompt% javac Hello.java
The compiler produces files with the extension .class, which is the object code for
each of the classes defined within the file.
If you've written an application, you can run it using java; as in:
prompt% java Hello
Hello World!
for the classic first program.
If you've written an applet to be run through a browser, you can run it using a
viewer, appletviewer as in:
prompt% appletviewer Hello.html
which will bring up a separate window.
Simple Example Programs
Hello World as Application
All the code is in a single file called Hello.java:
// standard first program
// runs via terminal i/o
class Hello {
public static void main (String[] args)
{
System.out.println("Hello World!");
}
}
Hello World as Applet
For this we need two files: one for the java code and one to define the html page from
which to run it. The java code looks like:
// standard first program
// run as an applet
import java.applet.Applet;
import java.awt.*;
public class HelloWorld extends Applet {
public void paint (Graphics g)
{
g.drawString("Hello World!", 25, 25);
}
}
In this case, we need to inherit the Applet class for our class definition. Additionally,
we overwrite a method from the class, paint, to print what we want.
We also need the companion html file, as shown in your notes to define the size of the
window to open and designates the java code to be run within it.