|
Basic Java Commands |
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.
// standard first program
// runs via terminal i/o
class Hello {
public static void main (String[] args)
{
System.out.println("Hello World!");
}
}
// 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.