Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Getting Started: /usr/local/java/bin Make Sure That This Directory Is in Your Load Path. Javac

Java programs are compiled using javac and run using java. The javac compiler produces .class files containing object code. Hello World can be written as both an application run from the terminal using java, or as an applet run within a browser using appletviewer. The applet example imports Applet and AWT classes, extends Applet, and overrides the paint method to draw "Hello World!" within the applet window defined in a companion HTML file.

Uploaded by

Rocky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Getting Started: /usr/local/java/bin Make Sure That This Directory Is in Your Load Path. Javac

Java programs are compiled using javac and run using java. The javac compiler produces .class files containing object code. Hello World can be written as both an application run from the terminal using java, or as an applet run within a browser using appletviewer. The applet example imports Applet and AWT classes, extends Applet, and overrides the paint method to draw "Hello World!" within the applet window defined in a companion HTML file.

Uploaded by

Rocky
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

You might also like