Lesson 4

Running a Java Application

Java source code is always stored in files with the extension .java. Once you have created the source code for a program and saved it in a .java file, you need to process the source using a Java compiler. Using the compiler that comes with the JDK, you would do this with the following command:

    javac MyProgram.java

Here, javac is the name of the Java compiler, and MyProgram.java is the name of the program source file. Note that the javac is case-sensitive. Assuming your program contains no errors, the compiler generates a bytecode program that is the equivalent of your source code. This is stored in a file with the same name as the source file, but with the extension .class. Java executable modules are always stored in a file with the extension .class.

If you are using some other product to develop your Java programs, you will probably be using a more user-friendly, graphical interface for compiling your programs that won't involve entering commands such as that shown above. The file name extensions for your source file and the object file that results from it will be just the same however.
To execute the bytecode program in the .class file with the Java interpreter in the JDK, you enter the command:

    java MyProgram

The bytecode instructions are analyzed and executed by the Java interpreter. The Java virtual machine is identical in all computer environments supporting Java, so you can be sure your program is completely portable. As we already said, your program will run just as well on a Unix Java implementation as it will on that for Windows 95/98, MacOS or any other operating system that supports Java.

Note that the Java compiler in the JDK will compile both applications and applets. However, an applet is not executed in the same way as an application. You must embed an applet in a web page before it can be run. You can then execute it either within a Java-enabled web browser, or by using the appletviewer provided in the JDK. If you have compiled an applet and you have included it in a web page stored as MyApplet.html in the current directory on your computer, you can execute it by entering the command:
   
appletviewer MyApplet.html

Remember: Java is case sensitive!

So how do you put an applet in a web page?
You use an applet tag within the head tag or a div tag. The applet tag contains attributes for the source class, and the size of the applet.




Assignment 4

- Write a program that takes an array and sorts it using the sort of your choice
Don't use a sort algorithm demonstrated in this class! :)

  GO BACK
Return to the Top