Installing a Java Application on a PC
In this Appendix, we will show you how to install an application onto a Windows PC from the RAD environment. The means explaining system environment variables and the software required to run a Java application on a PC. We will also explore PC batch (.bat) files and show how they make applications easier to use. Finally, we’ll demonstrate how to import files into the RAD environment. The example assumes Chapters 1 and 2 have been completed.
Tutorial: Exporting an Application from RAD
Programmers use RAD to develop applications. Users do not have RAD. Therefore applications have to be installed onto user’s computers. To install an application, the application’s .class files must be moved from RAD onto the computer where they will be run. This is called exporting. In the process of installing a Java application, exporting is a relatively simple step. The more complicated steps involve installing a JRE and configuring Windows to enable Java applications.
In the example, the Employee application in myFirstPackage will be moved to a flash memory stick identified as the F: drive. We are putting the application on a memory stick so that it can be easily transported between computers. (If you don’t have a memory stick, any storage media that has at least 150 megabytes of space is OK. For example, a zip disk can provide the needed space and portability. At worst, sacrifice portability and use the C: drive.)
The Export window will be displayed (see Figure A-1). Files can be exported in several different formats. For instance, there are ZIP and JAR format options for Java applications, as well as, WAR (Web Archive File) and EAR (Enterprise Archive File) options for exporting server-based applications. (In later chapters, the text will cover server-based applications and how to install them.) All of these formats compress the files. Usually these compressed formats are used when exporting and installing (rather than exporting the .class files directly) because of storage and transfer time efficiencies.
The application can also be exported in several different “ways.” For instance, the FTP (File Transfer Protocol) option allows files to be sent to any computer that has a Web address (thereby helping with a remote application install). We are simply moving the application to a different location on the PC (i.e., from the RAD work space to the F: drive), so we will choose the File system option.
The Export window will be displayed (see Figure A-2). Because we selected MyFirstProject for export, RAD assumes every folder and file will be exported. Because we are only going to run the application (not edit the source code) the .java files do not need to be exported.
The Select Types window will be displayed (see Figure A-3). As this window’s name implies, files for export can be specified by their type (i.e., file extension). As an application gets more complex, it may be comprised of hundreds if not thousands of class files. Rather than specifically selecting each class, this enables you to specify all of them for export.
On the Export window, notice that the check mark next to MyFirstProject has changed to a solid blue square. This means that the project folder is not selected for export but something in the project is selected. Expanding MyFirstProject shows that a subfolder called bin has something selected.
Are you wondering where bin came from? Each perspective has a unique view of an application’s resources. The Java perspective does not display the class files or the folders and subfolders that hold them. Continuing to expand the subfolders would show that inside of myFirstPackage the two class files have been selected for export.
When files in a package are exported, they must be installed in a PC folder with the same name as the package. The easiest way to do this is to export the package also.
The two class files will be displayed as in Figure A-4.
Tutorial: Downloading a JRE
The application class files have been successfully exported. Unfortunately, there is no JRE to translate the bytecode (in the class files) into machine language. We need to download and install a JRE before we can run the application.
There are many sources for a JRE. We will go to the Oracle website. (Be aware that there are frequent updates to the Oracle website and often the procedure for downloading and installing will change.)
The download page will be displayed (see Figure A-5).
Oracle puts out new releases of the JRE quite frequently, so the download Web site address and page (as well as the documentation Web site) may change with different Java releases. In addition, the currently available JRE release you will download will probably be newer than JRE 6.23 shown in these figures.
The license agreement page will be displayed (see Figure A-6).
As mentioned earlier, there are many different JRE versions. Do you remember why?
Because the JRE must work with a specific operating system, there are different JRE versions for each operating system. (This is the same reason applications—like games, word processors, etc.—come in Windows and Apple versions.)
This activates the download links at the bottom of the page.
The File Download window will be displayed.
The Save As window will be displayed. The name of the file (jre-6u25-windows-i586-p.exe) will already be specified. (The name of the file will change with different releases, so do not worry if it is not exactly the same.) Make sure the correct drive is specified (i.e., F :).
The file will be downloaded to the specified drive (i.e., F:).
The newly downloaded Setup Launcher file and myFirstPackage folder will be displayed (see Figure A-7).
Tutorial: Installing a JRE
The Java Setup – Welcome window will be displayed. The Typical setup puts the JRE on the computers default drive. Because we want to install to the F: drive, we will need to customize the setup option.
The “Java Setup – Destination Folder” window will be displayed (see Figure A-9).
The folder will be renamed (see Figure A-11).
The “Java Setup – Destination Folder” window will be redisplayed with F:jre specified as the destination.
A progress window is displayed as the installation is performed. This may take a while, depending on the speed of the computer and the media chosen for the install. In addition, if there are open browser sessions, you will be required to close them to continue the install. (There are other applications that you may also be prompted to close.)
Eventually, the “Java Setup - Complete” window will be displayed.
If the installation was successful, a folder named jre containing the JRE will be on the F: drive.
There should be a new folder called jre.
The window should look like Figure A-12. (The contents of the folder may also change with new releases.) Most of the files contain general and legal information. The README file does contain a list of subfolders and files that are not essential for running Java applications. However, the entire jre folder is approximately 90 MB, so space should not be a problem.
More important are the bin and lib subfolders. The lib folder contains the JAR files that hold the core classes (e.g., String). For example, if you display the contents of lib, the rt.jar file can be seen. This file contains many of the core classes.
The bin folder holds many JRE functions. For instance, the file named java.exe in bin contains a program that allows you to run a Java application in the JRE. However, before that can happen Windows must be configured to find the Java support functions and core classes.
Tutorial: Environment Variables
There are many different Windows’ environment variables, each with a unique name. Environment variables can be defined as system variables—meaning that they are universal for all applications on the PC. (It is like saying that a red traffic light is a national “driving environment variable” meaning stop. Regardless of where you are driving in the nation, a red traffic light means stop.) Environment variables can also be defined as user variables. User variables are only valid for a particular user of the computer. In addition, environment variables can be changed dynamically. This means that a user or an application can change them temporarily. When changed dynamically, the new values are only in effect for as long as that user or application continues processing. If another application or the same user starts up, the variables original values are used.
One way to change environment variables values is through the Control Panel.
The “All Control Panel Items” window will be displayed (see Figure A-13).
Notice that the environment variables are broken up in User and System variables. System variables are universal for all users on a computer. User variables are only valid for the particular user currently signed on to the computer. If there is a System and User variable with the same name, the User variable value will override the System variable value.
To understand the importance of Environment variables to Java we have to explain a little about how Windows (and many operating systems) “work”. When a user clicks an icon or a menu item, they are telling Windows to do “something” (i.e., specifying a function/command) against “something” (i.e., a file). So clicking on the “Internet Explorer” icon or menu item means the user is specifying run (i.e., the command) the iexplore.exe file stored in C:Program FilesInternet Explorer. This information was all specified when Internet Explorer was installed on the computer. Thank you, Microsoft!
Notice that Windows had to be told every little detail—like the exact location of the file to be run. This exact location and file name (C:Program FilesInternet Exploreriexplore.exe) is called a fully qualified file name. If a fully qualified name is not specified, Windows does not search the entire system to find the file. Instead Windows reads the environment variable PATH. PATH can hold many locations (separated by semicolons) and tells Windows where to search whenever an executable file (i.e., a command) is specified. The CLASSPATH variable tells Windows where to search whenever a Java class file is specified. As with the PATH variables, multiple path names (folders and subfolders) can be defined for the CLASSPATH variable. The “Environment Variables” window allows users to define a variable or edit an already existing variable.
In Chapter 2, you learned that Java source code has to be compiled (converted to bytecode) and interpreted (converted to machine language and run). Both of these functions are not standard Windows commands. If you tried to run a bytecode file, Windows would search the location in the PATH variable and not find the “run” command. In other words, Windows does not know anything about Java. Therefore, for a Java application to be executed, the PATH and CLASSPATH variables have to be edited to include the java command files location (i.e., in the JRE) and the location of the .class files.
If we modify the variables using the Environment Variables window, our application will only work for this computer. For example, if we took the memory stick (in the F: drive) to another computer, we would have to set that computer’s environment variables to look for our class files and the JRE on the F: drive. This would have to be done for every computer we want to run the application on. To get around this problem, we are going to write a small batch program (and store it on the F: drive) that will set the PATH and CLASSPATH variables dynamically on whatever computer we want to run the application on.
If you have not programmed on a PC before this may come as a surprise, but all the functions performed from the Windows GUI (graphical user interface) can be performed with commands. For instance, the dir (directory) command lets a user display the contents of a folder or drive (the same function that the My Computer icon provides). In addition, there are path and Set classpath commands that can control the two environment variables we need to define. Our batch program will use these commands to configure the environment variables. In addition, we will include a JRE command to run our Java application.
Tutorial: Creating a Batch File
Windows commands can be stored in a file with an extension of “bat” (indicating that the file is a batch file). When a bat file is specified at the command prompt (or its icon is double-clicked), Windows will execute the commands contained in the file in sequential order. We will use the Windows Notepad utility to enter, and save the commands in a batch file called Employee_Application.bat.
Notepad will create an unnamed file and display a blank window indicating that the file is empty. Commands are entered by positioning the cursor and typing. The first command we want to enter is the path command.
This will set the path to the bin folder that contains the JRE commands. The java command is needed to run the employee application in the JRE and is stored in the bin subfolder. (If you need proof, use My Computer to display the contents of the bin folder. You will see an “application” file called java. This program is run when the java command is issued.)
This statement defines the classpath variable, which, essentially, tells Windows where to find our application classes (i.e., the package folders myFirstPackage). There must be at least one space between Set and classpath, and there are no other spaces in the command. Be careful: this must be coded exactly as shown. Common mistakes include using forward slashes instead of the back slashes, using colons for semicolons (and vice versa), and having an extra space at the end of a line.
There must be at least one space after the java command and no spaces thereafter. When this command is encountered, Windows finds the java program (because of the path statement) and passes the name of our application (myFirstPackage/EmployeeApp) to the java program. Notice that when identifying the application both the package and class file names are needed and are separated by a forward slash. The java program then passes the bytecode (i.e., the class file) to the Java interpreter (i.e., the JVM) for translation into machine language and execution. The final code should look like Figure A-18.
Notice that the default file save type is a text document (i.e., an extension of .txt). However, this file needs to be saved as a batch file (i.e., a file extension of .bat).
The “Save as type” will change to All Files.
A hard copy of the file contents will be printed.
The window will display the Employee_Application.bat file (see Figure A-19).
Tutorial: Running a Java Application on the PC
Let’s run an application:
If the Command Prompt option is not listing in the Accessories menu, you can click the Start button, and then in the “Search programs and files” text field, specify cmd and press Enter.
The “Command Prompt” window will be displayed with the computer’s default directory (in this case, C:Windowssystem32 as in Figure A-20). The default directory can be changed, therefore it can be different for every PC, and so don’t worry if yours doesn’t match Figure A-20.
The “Command Prompt” window allows users to issue Windows commands interactively. The cursor (the blinking underscore) indicates where the commands will appear when typed. To the left of the cursor, the default directory followed by a > is displayed. To execute a command, type the command and simply hit Enter. As mentioned earlier, everything you can do in Windows can be done with commands. We’ll use the dir command an example.
Whoa, that was a lot of information!
The directory command (dir) displays the contents of a drive or directory (folder). In this case, we did not specify what directory to display so the default directory was used. Figure A-21 shows that all the files and subfolders contained in C:Windowssystem32 (the default directory), as well as, some information about each (date and time, size, etc) and totals information for the directory. Notice that this is similar to double-clicking on a folder icon in the My Computer window and displaying its contents (well, maybe not as pretty).
To run the Employee_Application batch file all you need to do is identify the file and press the Enter key. To identify a file specify where the file is located (the file’s path) and the file name.
Because Employee_Application is a batch file, Windows knows to execute the commands contained within. Notice that the Command Prompt window (in Figure A-22) shows each of the three batch file commands and the result of the Java application.
This may seem like a lot of work (for very little result). However, most of the work was one time, setup tasks. To rerun the application you only have to execute the java command again. No environment variables have to be reset, nor do you have to export the application again. However, if the application were modified, the classes would have to be reexported.
Tutorial: Breaking the Code
The batch file is very important to the successful installation of a Java application. Unfortunately, batch file mistakes are not easily diagnosed. We will create some of the most common mistakes and show their effects on running the application.
The application worked! You are probably saying, “Hey, you said Java was case sensitive!” That’s still true. However, Windows is not case-sensitive. If you reference files called Employee, employee, or EMPLOYEE, Windows will find the same file.
You will get the following (always frustrating) error message:
Exception in thread “main” java.lang.NoClassDefFoundError: myFirstPackage/EmployeeApp
The error was caused by an incorrect classpath. Let’s walk through what is happening. The java command tells the JVM to execute myFirstPackage/EmployeeApp. The JVM searches through all the folders in the classpath (C:) looking for the folder called myFirstPackage. Unfortunately, myFirstPackage is on the F: not the C:. (Note, the set classpath command was executed, Windows not only did not check to see C: had the class file, it doesn’t even check to see if the C: is a valid drive.) Because the application cannot be found, the JVM throws up its hands and issues the “No class found” message. This is a frustrating error to fix because the message does not provide any clues as to where the problem is. For example, this message is also generated if the application name is specified incorrectly in the java command. Let’s prove it.
Notice that the same error is generated, but the message says it cannot find EmployeeAp. Whenever you get the “No class found” message, try to remember that the JVM usually cannot find the class specified in the java command because of a simple typo in either the classpath or the application name.
You will get the following annoying error message:
‘java’ is not recognized as an internal or external command, operable program or batch file.
(If you do not get this error, the System32 folder contains the java.exe file. Go to the folder, rename the file, and do step 14 again. Don’t forget to change the file name back to java after you are done with step 19.) This time the problem is with the path environment variable. Whenever you enter text at the command prompt, Window searches the directories in the path looking for a batch file, executable program file, or operating system command file with the same name as the text specified. In this case we specified an incorrect folder name (jr instead of jre) when we defined the path. Windows looked for the executable file called java (which contains the java command program) in the folders specified in the path. The file was not in any of those folders, so Windows could not find the java command (stored in the f:jre bin folder).
If you had specified jave instead of java in the third line, you would have gotten the same error but this time identifying jave as the unrecognized command.
Just to emphasize how careful you need to be (or how picky computers are), we will create one last mistake.
You will get the “java command not recognized” error message again. Why? Because spaces are valid characters in a folder name, therefore a folder specified as “bin ” is not the same as a folder specified as “bin.” Yikes-a-hootie!! Isn’t programming fun?
Tutorial: Stopping a Runaway Application in Windows
There will be occasions where you will write and/or run an application that never ends or “freezes up” (does not respond to the keyboard or mouse). On these occasions, you will want to end the application. The Windows Task Manager allows you to do this. (The Task Manager also displays performance information about the computer hardware, as well as, all active applications and processes.)
There are several ways to display the Task Manager. For instance, you can start the command prompt, type taskmgr, and press Enter. Alternatively, you can press the Ctrl, Alt, Delete keys simultaneously, and display the Windows Security window. The Windows Security window has a Task Manager option. Ctrl-Alt-Delete is used to regain control when the entire computer or all the applications are not responding.
However, it’s easiest to simply right-click in an empty area of the task bar. The task bar is (usually) at the bottom of the screen and shows buttons for all of the applications that are currently running. You have probably clicked the task bar buttons to move between applications. However, right-clicking in an empty area anywhere to the right of the Start button, displays a shortcut menu and one of the options is the Start Task Manager.
The Windows Task Manager frame will be display as in Figure A-23. In this example, there are twelve active applications and their names appear in the Applications view. (The applications would also appear in the task bar’s application tray. Within each task bar icon would be individual items that are displayed in the task manager in Figure A-23.)
Java applications are identified with the “steaming cup of coffee” icon. In Figure A-23, the application My First Frame is a Java application. To end an application, first select the application by clicking its name and then click the End Task button.
Then “End Program – My First Frame” dialogue box will be displayed with a message saying that the application is not responding (Figure A-24). Of course, you would click the End Now button.
If you go back to the Task Managers Application list, you will see that the application name has been removed as in Figure A-25. (Sometimes this may take a second or two.)
As the applications you write become more complex, you will run into unexpected results and problems (such as endless loops) that prevent the application from responding or stopping. Try to remember to use the Task Manager; it is your escape hatch from such problems.
Importing into RAD
Importing allows programmers to bring files from outside of the RAD workspace into a project. Java source code files can be imported, as well as, many other types of files (image, database, web page, etc.). To import, RAD simply requires that a project already exists. If an application were exported, how and what was exported has implications for importing. For example, earlier only myFirstPackage and the .class files were exported (i.e., the project, bin, and src folders and were not exported). If myFirstPackage and the .class files were imported, the application could not be run because RAD’s Java perspective does not provide access to the .class files. To import and access an application in the Java perspective the source files have to be imported. This means the source code has to be exported if you want to import.
An entire project can be exported and imported very easily. However, this makes running the application more complicated because the entire folder structure will be exported. Figure A-26 shows the folder structure for the .java and .class files if the whole project is exported. In addition, this takes up a lot of space.
In the real world, Java applications are usually exported and transported as JAR files. However, the project structure is not preserved and if the whole JAR file is imported, the source code is not put in the correct location within the project. To avoid this problem we need to specify that only myFirstPackage be created in NewProject/src when importing.
Let’s prove it.
Tutorial: Importing into RAD
Now let’s import into RAD:
If you expand the tree and drill down into src/myFirstPackage, the .java files will appear checked in the right-hand pane. Even though all the folders, subfolders and java files are selected, the source code will not be exported. Exporting the source code must be explicitly specified.
Well that’s great that we have a jar file but how can you tell what is in it? How about importing it into a new project?
If you expand the src folder in the Package Explorer, myFirstPackage should appear and within myFirstPackage should be the two .java files Employee and EmployeeApp.
That seemed like a lot work. Let’s show how easy it is to import if the entire project is simply exported to the file system.
Looking in NewProject should show that the myFirstPackage folder and its contents were imported. You can confirm that the import was successful by running EmployeeApp.
The “problem” with exporting the entire project is that structure the holds the package with the .class files is more complicated than the Tutorial example (which simply exported the package). This means that the classpath value has to be changed to point to this new location rather than the F: drive. If you are using the bat file, it must be modified to issue the correct set classpath command.
Results of the Tutorial
You will export the shipment application to the F: drive and create a batch file to set the system variables and run the application. Because all the setup work has already been done, you will see how relatively easy it is to install the application.
Results of the Review Exercise
The results are:
Check that the Exercise Was Done Correctly
Finally, let’s check it was done properly.
13.58.50.156