A Simple Java Tutorial by Charles W. Neville, 1998 Copyright Charles W. Neville, 1998 0. Introduction. In this tutorial, you will do three things: (1) You will enter (that is TYPE IN) a simple Java program, compile it, and run it. (2) You will enter a simple Java applet, compile it, and run it. (3) You will enter a simple Java GUI (Graphics User Interface) application, compile it, and run it. When you are finished, you won't be a Java expert, but you will be prepared to start entering, compiling and running the example programs given in class, and you will be prepared to start writing your first computer projects. Before beginning this tutorial, you may wish to consult the Survival DOS document in this directory on this Web site. 1. A Simple Java Program. Java programs have to be written in plain ASCII text. (Plain ASCII text is UNFORMATTED text. By contrast, the text produced by a word processor, such as Word Perfect or Word, is NOT plain ASCII text because it contains special formatting characters.) So you are going to use the ASCII text editor which comes with Windows 95 and Windows NT, namely Notepad. (Warning to DOS mavens: DO NOT USE THE DOS EDIT PROGRAM. It mangles filenames and you will get nothing but grief when you try to compile your program.) Go to the Start menu at the bottom of your Windows 95 Desktop and click on it. You will see a submenu named Programs. Move the mouse cursor to Programs and pause for a moment. A further submenu will unfold. Accessories will be on this submenu. Move the mouse cursor to Accessories and pause again. A submenu will open up which contains an entry named Notepad. Move the mouse cursor to Notepad and double click the LEFT mouse button. Notepad should open. Type the following program into Notepad exactly as it is given below. Be careful to get the capitalization and spelling, right because Java is VERY case sensitive. (You don't have to get the indentation right as far as Java is concerned, but you do as far as your instructor is concerned because proper indentation makes programs MUCH easier to read.) // My First Java Program. // Simple Hello World Program. class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } Be sure you have a FORMATTED floppy disk in the A: drive. Use the Notepad File menu to Save this program on your floppy disk as HelloWorld.java. (No period at the end.) Click on the - sign at the top of the Notepad window to minimize it (reduce it to an icon on the task bar.) Be careful NOT to click on the small x at the top of the Notepad window, as that ends the Notepad program. From the Start menu again, go to Programs and then to Cmd.exe (on many systems it will be called MS DOS Prompt or DOS Prompt instead). Double click on Cmd.exe (or MS DOS Prompt or whatever it is called today on the computer you are working at). A rather uninteresting black window will open. This is the DOS command window, and it is what you will use to compile and run Java programs. Type the following sequence of commands in the DOS command window. Start at the left edge, not in the middle, don't type the first line, "Command Meaning," and just type the commands, not the meanings. Command Meaning DON'T TYPE THIS LINE A: get to A: drive DON'T TYPE THE MEANINGS dir see what files you have on the A: drive javac HelloWorld.java compile your HelloWorld program dir see what files you have java HelloWorld run your HelloWorld program. When you type the first dir command, look to see if you have a file named HelloWorld.java. (No period at end of name.) If not, you saved your program in the wrong place. (Microsoft products such as Notepad are rather good at saving things in the wrong place.) If you saved your program in the wrong place, click once on the Notepad icon on the task bar to reopen the Notepad window and use SaveAs on the Notepad File menu to resave your file on the A: drive. When you type the javac HelloWorld.java command, look to see if the Java compiler gave you any ERROR MESSAGES. (I am afraid this happens rather frequently.) If so, you will have to use the error messages to figure out what went wrong, then go back into Notepad and fix the errors, resave your HelloWorld.java file, and then retype the javac HelloWorld command in the DOS command window. When you type the second dir command, look to see if you have a file named HelloWorld.class. (No period an end of name.) If not, then something went wrong when you tried to compile your program, and you will have to go back and fix the error as described above. If all went well, when you type the java HelloWorld command, you should see the line Hello World printed on the DOS command window screen. CONGRATULATIONS! You have just entered, compiled, and run your first Java program. 2. A Simple Java Applet. An applet is a Java program which is designed to be run from a Web browser. Applets tend to be considerably more interesting than command window programs like HelloWorld.java because they incorporate all the elements of a GUI (Graphic User Interface) that people who use XWindows, Nexts, Macs, or Windows 95 (and NT) have come to love. You already know a great deal about writing and compiling an applet from your experience writing and compiling the simple HelloWorld.java program. The first steps are the same, although the details of the Java code are different. Click on the Notepad icon on the Task Bar to open the Notepad window. From the Notepad File menu, click on New to clear the screen and start a new document. Enter the following program exactly as given: // My First Java Applet // Simple Hello World Java Applet import java.awt.*; import java.applet.Applet; public class HelloApplet extends Applet { public void paint(Graphics g) { g.drawString("Hello World", 50, 100); } } Save this program on your floppy disk as HelloApplet.java. (No period at the end.) Applets are designed to be run through a Web browser. Web browsers understand HTML (HyperText Markup Language) so to run an applet you have write a short HTML file. From the Notepad File menu, click on New to clear the screen and start a new document, and enter the following exactly as given: Save the file as HelloApplet.html. (No period at the end.) Type the following sequence of commands in the DOS command window to compile and run your applet. Command Meaning DON'T TYPE THIS LINE A: get to A: drive DON'T TYPE THE MEANINGS dir see what files you have on the A: drive javac HelloApplet.java compile your HelloApplet applet dir see what files you have appletviewer HelloApplet.html run your HelloApplet applet. When you type the first dir command, look to see if you have files named HelloApplet.java and HelloApplet.html. (No period at end of name.) As before, when you type the javac HelloApplet.java command, look to see if the Java compiler gave you any ERROR MESSAGES. When you type the second dir command, look to see if you have a file named HelloApplet.class. (No period an end of name.) If all went well, when you type the appletviewer HelloApplet.html command, you should see the line Hello World but this time drawn on a 400 pixel by 275 pixel window. To close the applet window and stop your applet, just click on the small x at the top of the appletviewer window. It is worth considering exactly what went on just now. You COMPILED your Java applet just like you compiled your first simple Java program; you typed javac followed by the program file name. But when you ran your applet, you invoked a very simple Web browser named appletviewer by typing appletviewer followed by the HTML file name. All appletviewer can do is run applets; it can't do any of the other things Web browsers usually do, but it is still a Web browser, and so the files it understands are written in HTML. Appletviewer loaded your HelloApplet.html file, and that file caused appletviewer to load and run your HelloApplet.class Java class file. If you want to experiment and you have a recent enough version of Netscape or Microsoft Internet Explorer, you might try loading your HelloApplet.html file into one of those and watching your applet run on a full featured Web browser. (Warning: The results are likely to be disappointing unless you have Netscape version 4.5 or higher. We have Netscape version 4.0 here.) CONGRATULATIONS! You have just entered, compiled, and run your first Java applet. 3. A Simple Java GUI Application. An application is a Java program which is designed to be run as a stand-alone program, rather than from a Web browser. Java applications can do all of the GUI (Graphic User Interface) things that applets do, and they can do things with files that applets cannot do. For example, Sun's HotJava Web browser is a full featured Web browser written entirely in Java as a Java application. The process for writing, compiling and running a Java GUI application is exactly like the one you used to write the simple HelloWorld.java command window program,although the details of the Java code are different. Click on the Notepad icon on the Task Bar to open the Notepad window. From the Notepad File menu, click on New to clear the screen and start a new document. Enter the following program exactly as given: // My First Java GUI Application // Simple Hello World Java GUI Application import java.awt.*; // Frame with "Hello World" painted on it. // // Note: Applications must have their GUI components contained // in Frames. Nothing will be displayed if an application // tries to start by displaying a Panel. // // You have to click on MSDOS window and type CTRL C to end this // application. class HelloApplication extends Frame { public void paint( Graphics g ) { g.drawString("Hello World", 25, 50); } public static void main(String[] args) { HelloApplication h; h = new HelloApplication(); h.setSize(200, 100); h.setVisible(true); } } Save this program on your floppy disk as HelloApplication.java. (No period at the end.) The commands to compile and run your HelloApplication.java program are exactly like the commands you used to compile and run your simple HelloWorld.java program, except that you use the name HelloApplication in place of HelloWorld. So type the following sequence of commands in the DOS command window. Command Meaning DON'T TYPE THIS LINE A: get to A: drive DON'T TYPE THE MEANINGS dir see what files you have on the A: drive javac HelloApplication.java compile your HelloApplication application dir see what files you have java HelloApplication run your HelloApplication application. If all went well, when you type the java HelloApplication command, you should see the line Hello World but this time drawn on a 400 pixel by 275 pixel window running in a STANDALONE APPLICATION. To close the window and end your application, you have to click on the DOS command window and type CTRL C. (No period at end.) CONGRATULATIONS! You have just entered, compiled, and run your first Java GUI application. 4. The Next Step. Now you are thoroughly familiar with the process of entering, compiling, and running Java Applets and Applications. But you probably find many sections of the example programs you have just entered somewhat mysterious. You may also find additional information about the Java program creation process helpful. For example, we haven't mentioned this before, but the name of the class, for example HelloApplication, must agree with the name of the file containing the class, in this case HelloApplication.java. You can find a summary and overview of the Java program creation process in the files Applet.txt and Application.txt, contained in this directory on this Web site. These two files fill in all the information we skipped for the sake of brevity in this tutorial. You can find a more detailed explanation of the Java statements you encountered in the remarks (comments) attached to the demo programs in the Demos directory on this Web site. In particular, the Demos directory contains fully commented versions of all three programs you just typed in, that is HelloWorld.java, HelloApplet.java, and HelloApplication.java. You may also wish to consult the demo Java program BetterHelloApplication.java, contained in the Demos directory, for a better behaved version of the HelloApplication application. Clicking on the small x at the top of the HelloApplication window does not end the application, but BetterHelloApplication.java has the additional code needed to end the application gracefully when the small x is clicked. Finally, the Demos directory contains several more fully commented Demo programs that do interesting things, such as displaying the basic arithmetic operators in Java.