WebDeveloper.com �: Where Web Developers and Designers Learn How to Build Web Sites, Program in Java and JavaScript, and More!   
Web Developer Resource Directory WebDev Jobs
Animated GIFs
CSS Properties
HTML 4.01 Tags
Site Management
WD Forums

    Web Video
    Expression Web



    Forum, Blog, Wiki & CMS

 Site Management
    Domain Names
    Search Engines
    Website Reviews

 Web Development
  Business Issues

    Business Matters

    The Coffee Lounge
    Computer Issues

Java Programming ... From the Grounds Up Part 5

Java Programming ... From the Grounds Up
Part 5

by Mark C. Reynolds

Running applications and creating applets

Running a Java Application

Now that we've stepped through Primes.java, the source code for our prime number generator, let's compile and run the application. You'll need javac, the Java compiler in the JDK, to compile the source code:

 javac Primes.java 
The Java compiler places each class in its own output file. Since Primes.java defined two classes--Primes and Mclass--compiling the source code will produce Primes.class and Mclass.class. Our examination of the source code showed that Mclass contained the main class method for this program. As a result, you'll need to run java, the Java interpreter in the JDK, on Mclass.class.

You'll also need to assign values for where the Primes application should start looking for primes and for how many primes it should find in total. If you wanted the Primes application to start at the number 14 and continue until 105 primes have been found, you'd enter at the command line:

 java Mclass 14 105 
As we've seen, it's not possible to execute Java source directly--it must be compiled into its bytecode format first. When a browser accesses a Java page, it receives and executes the bytecode stored in the .class file. Therefore, you'll need the Java Development Kit--or at least javac--to create Java content and Java Web pages.

What's less obvious is how our prime number generator makes use of Primes.class. As we've seen, Primes.class is created as a separate file from Mclass.class when Primes.java is compiled. When a browser or the Java interpreter reads in Mclass, it will discover unresolved references to the Primes class (and also to Java classes such as System). It then has to find the file Primes.class to resolve those references. The browser or Java interpreter makes use of class loading; it dynamically loads the Primes bytecode from the Primes.class file and also loads unresolved Java classes in the same way. If for some reason you had renamed Primes.class, the Java interpreter would be unable to continue; it would be unable to locate the resources it needed. This proves to be particularly important when developing Java applets; all class files and subsidiary files, such as GIFs, referenced by an applet must follow a set of naming and organizational conventions, as our next example below shows.

Creating Java Applets

Java applets are similar to standalone Java applications, but are embedded within Web pages. We will examine two simple applets in order to see how HTML, Java, and its classes can be combined to deliver executable content. Figure 3 shows the Java code for a Variable Hello applet. A variation of the standard programmer's "Hello, World" program, the Variable Hello applet shows the basic structure of an applet and its interaction with HTML and the browser environment.

The Variable Hello Java Applet.

 1: /** 2: A simple Java applet to issue an 3: HTML-specified greeting 4: @author Mark C. Reynolds 5: @version 1.0 6: */ 7: 8: import java.awt.*; 9: import java.net.*; 10: 11: public class VH extends java.applet.Applet 12: implements Runnable { 13: 14: String msg1; 15: String msg2; 16: 17: public void init() { 18: String who; 19: String where; 20: URL uwhere; 21: 22: who = getParameter("who"); 23: if ( who == null ) who = "world"; 24: uwhere = getDocumentBase(); 25: where = uwhere.toString(); 26: msg1 = "Hello, " + who + ","; 27: msg2 = "from " + where; 28: } 29: 30: public void paint(Graphics g) { 31: g.setColor(Color.blue); 32: g.drawString(msg1, 10, 15); 33: g.drawString(msg2, 10, 30); 34: } 35: 36: public void run() { 37: resize(preferredSize()); 38: repaint(); 39: } 40: } 

The code begins with two import statements. These alert the Java compiler that this applet will use classes from the java.awt and java.net packages. The asterisk is a wildcard character indicating that everything within those packages should be imported. For simplicity's sake, we've used the wildcard instead of being more selective and designating only those classes that will be required.

In line 11, we declare a public class VH as a subclass of the Applet class with the extends clause. For this applet to function, we need to add executability functionality to the VH class. Since this is not something inherited by the Applet class--and remember, Java only has single inheritance--we will use the implements clause to fill in missing elements from a Java interface named Runnable. Interfaces such as Runnable are a useful workarounds for situations where single inheritance is a drawback.

Applet programmers must provide an init() method and a run() method within their Applet subclass. A class declaration that extends Applet and implements Runnable is mandatory in all applets; it is the equivalent of the main() entry point for applications.

The VH class has two class variables, msg1 and msg2, and three methods. The init() method is called once when an instance of VH is created. The run() method is called whenever the applet is activated. The paint() method is what displays the graphics. A Web browser will call init() when it first loads the applet's page, and it will call run() whenever the page is visited or revisited. The paint() method is called whenever the applet needs to draw something. There is a default paint() method, unlike init() and run(), but many applets will want to provide their own version of paint in order to perform their own specific graphical operations.

The VH init() method starts out by calling getParameter() to obtain the value of a parameter named who. getParameter() is a Java method for obtaining information from the HTML which defines a Web page.

[ < Java Programming ... From the Grounds Up:
Part 4 ]
[ Java Programming ... From the Grounds Up:
Part 6 > ]

HTML5 Development Center

Recent Articles