Executing Java is a two step process. First a Java class in a .java file is translated into Java byte code. On tuck.cs.fit.edu or zach.fit.edu, use the compiler
/software/java/jdk1.2/bin/javacThe byte code is executed by the interpreter
/software/java/jdk1.2/bin/javaThese programs are part of the Java Development Kit (JDK). [The previous link should work if your browser is now running on maelstrom or zach.] The JDK documentation (including the API documentation) is available locally or remotely at the JavaSoft WWW site.
Putting /software/java/jdk1.2/bin in your path is a good idea. The following line in your .cshrc file will work in some circumstances
setenv PATH /software/java/jdk1.2/bin:${PATH}
If everything is configured correctly, then entering:
maelstrom> java -versionshould yield the output:
Classic VM (build JDK-1.2-V, green threads, sunwjit)Type
which javato see which java program you are using. Be careful, /bin/java is also installed; it is version 1.1.3, so it is relatively old.
// HelloWorld.java -- A Java stand-alone, main program class HelloWorld { public static void main (String args[]) { System.out.println ("Hello World!"); } }Supppose you have this file in a local directory and /usr/local/java/bin is in your path, then the following dialog is possible:
maelstrom> javac HelloWorld.java ;; compile the source code maelstrom> java HelloWorld ;; interpret the resulting byte code Hello World!
The file HelloApplet.java contains a Java class that can be used as an applet, a program that can be run by a WWW browser.
// HelloApplet.java -- a simple Java applet import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; public class HelloApplet extends Applet { public void init() { setBackground (Color.white); // Make applet window stand out } public void paint(Graphics g) { g.drawString ("Hello world!", 40, 20); } }Like any Java class, HelloApplet is translated to Java byte code as follows:
maelstrom> javac HelloApplet.java
This HTML document contains a Java applet. A Java applet is introduced into an HTML document using the applet tag. Only some broswers have implemented the capability of requesting the Java byte-code named in the applet tag and executing the instructions.
The following applet construct appears between the horizontal lines below:
<applet code="HelloApplet.class" width=150 height=30> </applet>The html file and the Java byte-code must both be readable by everyone so that the Harris lab WWW server can deliver them when the WWW page is accessed. To ensure that this is the case, do:
chmod go+r file.html HelloAppler.class
For developing and testing an applet it is usually more convenient to avoid using a WWW browser and use the program appletviewer instead. This program searches a file for a snippets of HTML that begin with <applet> and ends with </applet>. It uses these snippets to create a window for each applet. The appletviewer program acts like a WWW browser and initilizes the applet. A menu can use used to perform some of the actions a WWW browser might take with respect to an applet.
It is a good idea to put an HTML snippet in the Java source program (in comments), to postpone writing a HTML document for the applet. Thus, the running of an applet is somewhat similar to the two step process for a main program:
maelstrom> javac HelloApplet.java ;; compile the applet maelstrom> appletviewer HelloApplet.java ;; find applet tag and launch applet
The window created by appletviewer looks like this (under the X Window System and Solaris):