|
JAVA
for Oracle Developers
Basic
Steps to write, compile, and run a JAVA applet
|
More Resources by
Google: |
|
|
|
|
Gathered
By:
John Kazerooni
How
to write a JAVA applet?
A JAVA applet is
not a standalone program and need JAVA runtime to run. You can run a JAVA applet
using your browser. Your browser should have JAVA runtime and be enabled. You
can also use the Applet Viewer included in the SDK.
Now, write a JAVA
applet called IamSelfBuilder to display “I am a self builder!”
Step
#1: Write and Create a JAVA applet source file
Use the notepad
editor. Write, your program,
IamSelfBuilder, to display the “I am a self Builder!” message as a JAVA
applet program.
Then save your JAVA
applet as “c:\IamSelfBuilder.java” within the double quote and exit from
notepad. Be careful what you type. Notice that the JAVA compiler and interpreter
are very case-sensitive.
Notice
that the bold characters in the following listing are comments.
import java.applet.*;
import java.awt.*;
/***************************************************
*
The IamSelfBuilder class implements an applet that
*
simply displays "I am a self builder!".
***************************************************/
public class
IamSelfBuilder extends Applet {
public void paint(Graphics g) {
// Display "I am a self builder!"
g.drawString("I am a self builder!", 50, 25);
}
}
Step
#2: Compile your JAVA applet source program
Go to the MS-DOS or
Command prompt and change your current directory to the directory where you
saved the JAVA source program. Remember that the JAVA compiler be in your path.
If it is not, then set the path (set PATH=your java path/bin).
Example: MS-DOS>
c:\java folder\bin\javac IamSelfLearner.java
If there are no
errors, then you have successfully compiled your JAVA applet program. Now, you
have generated a JAVA applet bytecode file, IamSelfBuilder.class. Now that you
have the class file (a JAVA bytecode file), you can run your program. This file
can run using your browser.
Step
#3: Write an HTML file
Now, you should
write an HTML file to accompany your applet. Use the notepad editor.
Save it as c:\IamSelfBuilder.html.
<HTML>
<HEAD>
<TITLE><CENTER>
An example of my
first JAVA applet program
</CENTER></TITLE>
</HEAD>
<BODY>
This is my first
JAVA applet:
<APPLET
CODE="IamSelfBuilder.class" WIDTH=300 HEIGHT=50>
</APPLET>
</BODY>
</HTML>
Step
#4: Run the JAVA applet Program
You can either view
your applets by using your web browser or appletviewer application.
On your web
browser, simple open your browser and type your html program.
For example: c:\IamSelfBuilder.html
To use the
appletviewer application, go to MS-DOS and type appletviewer IamSelfBuilder.html
For example:
MS-DOS>cd /
MS-DOS>appletviewer
IamSelfBuilder.html
And your should see
your applet works.
If you got the “I
am a self builder” message then your program works. Congratulations!
If not, then make
sure that your are in the “class” folder or you might have to change your
CLASSPATH variable (set CLASSPATH=).
|