INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM

ICS201, SECTIONS 52  (002 Semester)

INTRODUCTION TO COMPUTER SCIENCE

LAB  # 10: Automation of Execution of Java Applications & Applets

 

Instructor: Bashir M. Ghandi

 


1. Objectives:

To gain experience with:

 

2.  How to automate the execution of Java applications.

So far, we have been executing our Java application in two ways:

Either by opening the source file from a Java editor such as JCreator and using the execute button of the editor

Or by typing a command line at the DOS prompt.

 

Obviously both of these ways are not convenient for the ordinary user.  The first case requires giving the source files to the user, which we may not wish to do.  It also assumes that the user has a Java editor.  The second case is equally not good enough since it assumes that the user must have some background on DOS, which is most likely a wrong assumption.

 

To automatically execute a java application, we create a batch file for it.  A batch file is a text file with extension .BAT, containing a sequence of DOS commands – one command per line.   Like .EXE files, Batch files execute automatically when they are double-clicked.  Thus to execute a Java application by mouse click, we create a batch file containing the DOS commands that are required to execute the Java application.

 

However, the DOS command required to execute a Java application depends on whether the JDK tools folder (e.g. E:\JDK1.3\BIN) is in the DOS PATH or not.  As we learnt earlier, the DOS PATH is a list of folders separated by semicolon that DOS automatically searches when looking for an application to execute.

 

We can check if the JDK folder tools is in the DOS PATH by typing SET at the DOS prompt.

IF the JDK tools folder is not in the PATH, then we need to add it by editing the AUTOEXEC.BAT file add appending the following command at the end of the file and reboot the computer:

PATH = %PATH%;E:\JDK1.3\BIN

 

If you are using Windows ME, you may not have the AUTOEXEC.BAT file.  In this case, you need to create it and save it at the root of drive C.  If you are using NT (or 2000) the you use the system icon from the control panel to set the PATH environment variable.

 

Now assuming the JDK tools folder is in the DOS PATH, then the following is an example of a batch file needed to execute a compiled Java application named BooksDisplay.class

java -classpath . BooksDisplay

if errorlevel 1 pause

 

The above must be named with .BAT extension (e.g.  BooksDisplay.bat) and saved in the same folder as the corresponding class file.  The pause will make the DOS window to remain open until a key is pressed.

 

If we wish to execute this file from another location different from the location of the .class file, e.g. from the desktop, then need to add commands to change directory on the location of the .class file as follows.

E:

cd \ics201\cs\labs\lab09

java -classpath . BooksDisplay

if errorlevel 1 pause

 

Since this second version will always run irrespective of where the batch file is stored, it is better

 

The dot “.” in the classpath option is directing the interpreter to look for class files on the current folder.  However, if your program uses user-defined packages, then you must add the folder of the packages in the classpath.

E:

cd \ics201\cs\labs\lab00

java -classpath .;\ics201 cs.labs.lab00.TestPoint

pause

if errorlevel 1 pause

2.  How to run Java 2 applets using normal browsers

The popular browsers, (Intenet Explorer and Netscape Navigator) are equipped with Java 1.1 interpreter, as such they cannot execute applets written using later version of Java such as the version 1.3 we are using.

 

To solve this problem, SUN provides a plug-in which can be installed on a computer.  The plug-in is actually the interpreter for Java 1.3, thus if you install Java 1.3 on your computer you do not need the plug-in.

 

In addition to the plug-in, SUN also provides a program called HTML converter, than can be used to convert your HTML file so that they can be understood by the popular browsers.  Actually the converter simply inserts code to direct the browsers to use the interpreter in the plug-in instead of their own interpreter.

 

Java HTML converter is provided to you in the ICS102.  It is also available on my computer (icswww) under the folder JavaResource.  The name of the file is: htmlconv1_3.zip.  Of course you can also get it directly from SUN site.  Unzip the file in a folder. (The zip file will expand into a folder convert\classes).

 

The following lists the steps you need to follow to convert your HTML file using the converter so that they can be understood by Internet explorer and Netscape Navigator.

1.       Locate the converter\classes directory.

2.       Launch the HTML Converter by double-clicking a batch file named HTMLConverter.BAT.  This should display the following:

3.       Select files to convert

Choose "Browse..." to select the folder from a dialog box. Once you have selected a folder, you can choose specific files to be converted by specifying patterns to match in the "Matching File Names" box. You can use * as a wildcard. Each pattern must be separated by a comma.  To convert all the matching files in nested subfolders, select "Include Subfolders".

4.       Choose a backup folder

The default backup folder has the same name as the folder that contains the source documents with _BAK appended to it.

If you want to select a different backup folder, type the new path in the "Backup Files to Folder" box or choose "Browse..." to select the new folder from a dialog box.

5.       Choose a conversion template

A template file determines how the HTML of your file is altered to use Java Plug-in. If no template is chosen, a default template will be used. The default template will produce converted HTML files that will work with IE and Netscape.

If you want to use a different template, select it from the drop-down menu labeled "Template File"..

6.       Convert the documents

Click the "Convert..." button to begin the conversion process. A dialog box will show the file being processed, the number of files processed, the number of applets found, and number of errors found.

 

3.  Assignment

1.        Write a batch file to execute one of your applications from previous lab.

2.        Convert one of the applets from the previous lab to work with Internet explorer/Netscape.