Blakes 21 Days Appendix D
APPENDIX D
Using the Java Development Kit
- Top
- 615 In addition to the integrated development environment NetBeans, Oracle offers the Java Development Kit (JDK),
- a free set of command-line programs that are used to create, compile, and run Java programs.
- Every new release of Java is accompanied by a new release of the development kit.
- The current version is JDK version 8.
- 615 Although NetBeans and other programs such as IntelliJ IDEA and Eclipse are more sophisticated, some programmers continue to use the Java Development Kit.
- This appendix covers how to download and install the Java Development Kit, set it up on your computer, and use it to create, compile, and run a simple Java program.
- 615 It also describes how to fix a common configuration problem faced by JDK users.
Choosing a Java Development Tool
- Top
- 616 If you're using a Microsoft Windows or Apple Mac OS system, you probably have a Java Virtual Machine (JVM) installed that run Java programs.
- 616 To develop Java programs, you need more than a JVM.
- You also need a compiler and other tools that are used to create, run, and test programs.
- 616 The Java Development Kit includes a compiler, JVM, debugger, file archiving program, and several other programs.
- 616 The kit is simpler than other development tools.
- It does not offer a graphical user interface, text editor, or other features that many programmers rely on.
- 616 To use the kit, you type commands at a text prompt.
- MS-DOS, Linux, and UNIX users will be familiar with this prompt, which also is called a command line.
- 616 Here's an example of a command you might type while using the Java Development Kit:
- 616 This command tells the javac program - the Java compiler included with the kit -
- to read a source code file called RetrieveMail.java and create one or more files.
- These files contain compiled bytecode that a JVM can execute.
- 616 When RetrieveMail.java is compiled, one of the files will be named RetrieveMail.class.
- If the class file was set up to function as an application, a JVM can run it.
- 616 People who are comfortable with command-line environments will be at home using the Java Development Kit.
- Everyone else must become accustomed to the lack of a graphical point-and-click environment as they develop programs.
- 616 If you have NetBeans or another Java development tool compatible with Java 8, you don't need to use the Java Development Kit.
- Many different development tools can be used to create the tutorial programs in this book.
Installing the Java Development Kit
- Top
- 616 You can download the Java Development Kit from Oracle's Java website at www.oracle.com/technetwork/java.
- 616 The website's Downloads section offers links to several versions of the Java Development Kit.
- It also offers the NetBeans development environment and other products related to the language.
- The product you should download is in the Java Standard Edition (Java SE) and is called the Java Software Development Kit version 8.
- 617 The kit is available for Windows, Mac OS, Linux, and Solaris SPARC systems.
- 617 The kit requires a computer with a Pentium 2 processor that is 266MHz or faster, 128MB of memory, and 300MB of free disk space.
- 617 When you're looking for this product, you might find that the Java Development Kit's version number has a number after 8, such as "JDK 8.0"
- To fix bugs and address security problems, Oracle periodically issues new releases of the kit and numbers them with a period and digit after the main version number.
- Choose the most current version of JDK 8 that's offered, whether it's numbered 8.0, 8.1, 8.2, or higher.
- 617 CAUTION: Take care not to download two similarly named products from Oracle by mistake: the Java Runtime Environment (JRE) 8.0 or the Java Standard Edition 8.0 Source Release.
- 617 To set up the kit, you must download and run an installation program.
- On the Java website, after you choose the version of the kit that's designed for your operating system, you can download it as a single file.
- 617 After you have downloaded the file, you'll be ready to set up the kit.
Windows Installation
- Top
- 617 Before installing the kit, make sure that no other Java development tools are installed on your system (assuming, of course, that you don't need any other tool at the moment).
- Having more than one Java programming tool installed on your computer can often cause configuration problems with the kit.
- 617 To set up the program on a Windows system, double-click the installation file or choose Start from the Windows taskbar to find and run the file.
- 617 The installation wizard guides you thru the process of installing the software.
- If you accept the terms and conditions for using the kit, you'll be asked where to install the program, as shown in Figure D.1.
- 617 The wizard suggests a folder where the kit should be installed.
- In Figure D.1, the wizard suggests the folder C:\Program Files\Java\jdk1.8.0_60.
- When you install the kit, the sugested name might be different.
- 617 To choose a diferent folder, click the Change button.
- Either select or create a new folder, and click OK.
- The wizard returns to the Custom Setup options.
Figure D.1 - Installing the JDK. - goes here.
- Top
- 618 TIP: Before continuing, make note of the folder you have choosen.
- You'll need it later to configure the kit and fix any configuration problems that may occur.
- 618 You also are asked what parts of the kit to install.
- By default, the wizard installs all components of the JDK:
- Development tools - The executable programs needed to create Java software
- Source code - The source code for the thousands of classes that make up the Java Class Library
- Public JRE - A JVM you can distribute with the programs you create (also called a Java Runtime Environment)
- 618 If you accept the default installation, you need about 300MB of free hard disk space.
- You can save space by omitting everything but the program files.
- However, the source code and Java Runtime Environment can be useful, so it's a good idea to install them.
- 618 To prevent a component from being installed, click the hard drive icon next to its name, and choose the This Feature Will Not Be Available option.
- 618 After you choose the components to install, click the Next button to continue.
- You may be asked whether to set up the Java Plug-in to work with the web browsers on your system.
- 619 The Java Plug-in is a JVM that runs Java programs incorporated into web pages.
- These programs, which are called applets, can work with different virtual machines, but most browsers do not include one that supports the current version of the Java language.
- Oracle offers the plug-in to provide full language support to Microsoft Internet Explorer, Microsoft Edge, Mozilla Firefox, Google Chrome, Safari, and other browsers.
- 619 After you complete the configuration, the wizard installs the kit on your system.
Configuring the Java Development Kit
- Top
- 619 After the wizard installs the kit, you must edit your computer's environment variables to include references to the kit.
- 619 Experienced MS-DOS users can finish setting up the kit by adjusting two variables:
- Edit the computer's PATH variable and add a reference to the kit's bin folder
- (which is C:\Program Files\Java\jdk1.8.0_60\bin if you installed the kit into the C:\Program Files\Java\jdk1.8.0_60 folder).
- Edit or create a CLASSPATH variable so that it contains a reference to the current folder - a period and semicolon (.;) - followed by a reference to the tools.jar file in the kit's lib folder
- (which is C:\Program Files\Java\lib\tools.jar if the kit was installed into C:\Program Files\Java\jdk1.8.0_60).
- reserve
- 619 For Windows users unfamiliar with MS-DOS, later sections cover in detail how to set the PATH and CLASSPATH variables on a Windows System.
- 619 Users of other operating systems should follow the instructions provided by Oracle on its Java Development Kit download page.
Using a Command-Line Interface
- Top
- 619 The kit requires the use of a command line to compile Java programs, run them, and handle other tasks.
- 619 A command line is a way to operate a computer entirely by typing commands using the keyboard, rather than by using a mouse.
- Very few programs designed for Windows users require the command line today.
- Top
- 620 NOTE: To get to a command line in Windows, do the following:
- On Windows 8 and 10, choose Start, click the Search magnifying glass icon at the upper right, enter Command Prompt in the search box, and click the Command Prompt icon.
- On Windows 7, Vista, XP, or Server 2003, choose Start, All Programs, Accessories, Command Prompt.
- On Windows 98 or Me, choose Start, Programs, MS-DOS Prompt.
- On Windows NT or 2000, choose Start, Programs, Accessories, Command Prompt.
- 620 When you open a command line in Windows, a new window opens in which you can type commands, as shown in Figure D.2
Figure D.2 - Using a newly opened command-line window. - goes here
- Top
- 620 The command line in Windows uses commands adopted from MS-DOS, the Microsoft operating system that preceded Windows.
- MS-DOS supports the same functions as Windows - copying, moving, and deleting files and folders; running programs; scanning and repairing a hard drive; formatting a floppy disk; and so on.
- 620 In the window, a cursor blinks on the command line whenever you can type in a new command.
- In Figure D.2, C:\Users\caden_000> is the command line.
- 620 Because MS-DOS can be used to delete files and even format your hard drive, you should learn something about the operating system before experimenting with its commands.
- Top
- 621 NOTE: If you'd like to learn a lot more about MS-DOS, a good book is Special Edition Using MS-DOS 6.22, 3rd Edition (ISBN 978-0-78972-573-8), published by Que.
- The emphasis is on the words "a lot," because this book is 1,056 pages long.
- Top
- 621 However, you need to know only a few things about MS-DOS to use the kit: how to create a folder, how to open a folder, and how to run a program.
Opening Folders in MS-DOS
- Top
- 621 When you are using MS-DOS on a Windows system, you have access to all the folders you normally use in Windows.
- For example, if you have a Windows folder on your C: hard drive, the same folder is accessible as C:\Windows from a command line.
- 621 To open a folder in MS-DOS, type the comand CD, followed by the name of the folder, and press Enter. Here's an example:
- 621 When you enter this command, the TEMP folder on your system's C: drive is opened, if it exists.
- After you open a folder, the command line is updated with the name of that folder, as shown in Figure D.3
Figure D.3 - Opening a folder in a command-line window. - goes here
- Top
- 622 You also can use the CD command in other ways:
- Type CD \ to open the root folder on the current hard drive.
- Type CD Foldername to open a subfolder matching the name you've used in place of foldername, if that subfolder exists.
- Type CD .. to open the folder that contains the current folder. For example, if you are in C:\Windows\Fonts and you use the CD .. command, C:\Windows is opened.
- 622 It's helpful to create a folder for the projects you create in this book, such as one named J21work.
- If you already have done this, you can switch to that folder by using the following commands:
- 622 If you haven't created that folder yet, you can do so using an MS-DOS command.
Creating Folders in MS-DOS
- Top
- 622 To create a folder from a command line, type the command MD followed by the folder's name, and press Enter as in the following example:
- 622 The STUFF folder is created in the root folder of the system's C: drive.
- To open a newly created folder, use the CD command followed by that folder's name, as shown in Figure D.4
Figure D.4 - Creating a new folder in a command-line window. - goes here
- Top
- 623 If you haven't already created a J21work folder, you can do so from a command line:
- Change to the root folder (using the CD \ command).
- Type the command MD J21work and press Enter.
- 623 After J21work has been created, you can go to it at any time from a command line by using this command:
- 623 The last thing you need to learn about MS-DOS to use the Java Development Kit is how to run programs.
Running Programs in MS-DOS
- Top
- 623 The simplest way to run a program at the command line is to type its name and press Enter.
- For example, type DIR and press Enter to see a list of files and subfolders in the current folder.
- 623 You also can run a program by typing its name followed by a space and some options that control how the program runs.
- These options are called arguments.
- 623 To see an example of this, change to the root folder (using CD \) and type DIR J21work.
- You'll see a list of files and subfolders contained in J21work folder, if it contains any.
- Top
- 623 After you have installed the kit, run the JVM to see that it works.
- Type the following command at a command line:
- 623 java is the name of the JVM, and -version is an argument that tells it to display its version number.
- 623 You can see an example of this in Figure D.5, but your version number might be different, depending on what version of the kit you have installed.
- 623 If java -version works and you see a version number, it should begin with 1.8.
- Oracle tacks on a third number, but as long as the version number begins with 1.8, you are using the correct version of the Java Development Kit.
- 623 If you see an incorrect version number or a "Bad command or filename" error after running java -version,
- you need to make some changes to how the Java Development Kit is configured on your system.
Figure D.5 - Running the Java Virtual Machine in a command-line window. - goes here
- Top
- 624 CAUTION: Since Java 8 is the current version, you might be confused about the references to version 1.8.
- Although the language is called Java 8 and the JDK is designated JDK 8.0, the kit's internal version number is 1.8.0.
- This internal number shows up in the -version command as well as in your choice of installation folder for the kit.
- When all else fails, run the java -version command to make sure the right development tool has been installed on your system.
- If it begins with 1.8, you've got the right tool to develop programs for Java 8.
Correcting Configuration Errors
- Top
- 624 When you are writing Java programs for the first time, the most likely source of problems is not typos, syntax errors, or other programming mistakes.
- Most errors result from a misconfigured Java Development Kit.
- 624 If you type java -version at a command line and your system can't find the folder that contains java.exe,
- you see one of the following error messages or something similar (depending on your operating system):
- Bad command or file name
- 'java' is not recognized as an internal or external command, operable program, or batch file
- reserve
- 624 To correct this, you must configure your system's PATH variable.
Setting the Path on Most Windows Versions
- Top
- 625 On most versions of Windows, including Windows 7, 8, and 10, you configure the Path variable using the Environment Variables dialog, on of the features of the system's Control Panel.
- 625 To open this dialog on Windows 7, 8, or 10, follow these steps:
- Click the Start button on the taskbar.
- Click the Search icon (a magnifying glass) at the upper right.
- Type Environment Variables in the search box.
- Click the result Edit the System Variables. The System Properties dialog opens with the Advanced tab in front.
- Click the Environment Variables button. The Environment Variables dialog opens, as shown in Figure D.6.
- 625 To open this dialog in other versions of Windows, follow these steps:
- Right-click the Computer icon on your desktop or Start menu, and choose Properties. The System Properties dialog opens.
- Click the Advanced tab or the Advanced System Settings link.
- Click the Environment Variables button. The Environment Variables dialog opens, as shown in Figure D.6
Figure D.6 - Setting environment variables in Windows NT, XP, 2000, and 2003. - goes here
- Top
- 626 You can edit two kinds of environment variables: system variables, which apply to all users on your computer, and user variables, which apply only to you.
- 626 Path is a system variable that helps MS-DOS find programs when you run them at a command line.
- It contains a list of folders seperated by semicolons.
- 626 To set up the kit correctly, the folder that contains the Java Virtual Machine must be included in the PATH.
- The virtual machine has the filename java.exe.
- If you installed the kit in the C:\Program Files\Java\jdk1.8.0_60 folder on your system, java.exe is in C:\Program Files\Java\jdk1.8.0_60\bin
- 626 If you can't remember where you installed the kit, you can look for java.exe by choosing Start, Search (or Start and Search icon) and typing the filename in the serach box.
- You might find several copies in different folders.
- To see which one is correct, open a command-line window and do the following for each copy you have found:
- Use the CD command to open a folder that contains java.exe.
- Run the command java -version in that folder.
- 626 When you know the correct folder, return to the Environment Variables dialog, select PATH in the System Variables list, and click Edit.
- The Edit System Variable dialog opens with Path in the Variable Name field and a list of folders in the Variable Value field, as shown in Figure D.7.
Figure D.7 - Changing your system's Path variable. - goes here
- Top
- 626 To add a folder to the Path, click the Variable Value field and move the cursorto the end without changing anything.
- At the end, add a semicolon followed by the name of the folder that contains the Java Virtual Machine.
- 626 For example, if C:\Program Files\Java\jdk1.8.0_60\bin is the correct folder, add the following text to the end of the Path variable:
- ;C:\Program Files\Java\jdk1.8.0_60\bin
- 626 After making the change, click OK twice: once to close the Edit System Variable dialog, and another time to close the Environment Variables dialog.
- 627 Try it: Open a command-line window and type the command java -version.
- 627 If it displays the correct version of the Java Development Kit, your system is probably configured correctly,
- although you won't know for sure until you try to use the kit later in this appendix.
Setting the PATH on Windows 98 or Me
- Top
- 627 On a Window 98 or Me system, you configure the PATH variable by editing the AUTOEXEC.BAT file in the root folder of your main hard drive.
- MS-DOS uses this file to set environment variables and configure how some command-line programs function.
- 627 AUTOEXEC.BAT is a text file you can edit with Windows Notepad.
- Start Notepad by choosing Start, Programs, Accessories, Notepad from the Windows taskbar.
- 627 The Notepad text editor opens.
- Choose File, Open from Notepad's menu bar, go to the root folder on your main hard drive, and then open the file AUTOEXEC.BAT.
- 627 When you open the file, you see a series of MS-DOS commands, each on its own line.
- 627 The only commands you need to look for are any that begin with PATH.
- 627 The PATH command is followed by a space and a series of folder names separated by semicolons.
- It sets up the PATH variable, a list of folders that contain command-line programs you use.
- 627 PATH helps MS-DOS find programs when you run them at a comand line.
- 627 You can see what PATH has been set to by typing the following command at a command line:
- Top
- 627 To set up the kit correctly, the folder that contains the Java Virtual Machine must be included in the PATH command in AUTOEXEC.BAT.
- 627 The virtual machine has the filename java.exe.
- If you installed JDK 8 in the C:\Program Files\Java\jdk1.8.0_60 folder on your system,
- java.exe is in C:\Program Files\Java\jdk1.8.0_60\bin.
- Top
- 627 If you can't remember where you installed the kit, you can look for java.exe by choosing Start, Find, Files or Folders.
- You might find several copies in different folders.
- To see which one is correct, open a command-line window and do the following for each copy you have found:
- Use the CD command to open a folder that contains java.exe.
- Run the command java -version in that folder.
- 628 When you know the correct folder, create a blank line at the bottom of the AUTOEXEC.BAT file and add the following:
- PATH rightfoldername;%PATH%
- Top
- 628 For example, if C:\Program Files\Java\jdk1.8.0_60\bin is the correct folder, add the following line at the bottom of AUTOEXEC.BAT:
- PATH c:\"Program Files"\Java\jdk1.8.0_60\bin;%PATH%
- 628 The addition of the text %PATH% keeps you from wiping out any other PATH commands in AUTOEXEC.BAT.
- Quotation marks appear around the folder name Program Files because some versions of Windows require this to handle folder names that contain spaces.
- 628 After making changes to AUTOEXEC.BAT, save the file and reboot your computer.
- When this is done, try the java -version command.
- 628 If it displays the correct version of the kit, your system is probably configured correctly.
- You'll find out for sure when you try to create a sample program later in this appendix.
Using a Text Editor
- Top
- 628 Unlike more sophisticated Java development tools such as NetBeans, the Java Development Kit does not include a text editor to use when you create source files.
- 628 For an editor or word processor to work with the kit, it must be able to save text files with no formatting.
- 628 This feature has different names in different editors.
- Look for a format option such as one of the following when you save a document or set the properties for a document:
- Plain text
- ASCII text
- DOS text
- Text-only
- 628 If you're using Windows, several editors are included with the operating system.
- 628 Windows Notepad is a no-frills text editor that works only with plain-text files.
- It can handle only one document at a time.
- On Windows 7, 8, or 10, choose Start, click the Search icon at the upper right, search for Notepad, and click the search result Notepad.
- On earlier versions of Windows, choose Start, Programs, Accessories, Notepad.
- 628 Windows WordPad is a step above Notepad.
- It can handle more than one document at a time and can handle both plain-text and Microsoft Word formats.
- It also remembers the last several documents it has worked on and makes them available from the File menu.
- It's on the Accessories menu along with Notepad.
- 629 Windows users also can use Microsoft Word, but you must save files as text rather than in Word's proprietary format.
- (UNIX and Linux users can author programs with emacs, pico, and vi; Macintosh users have SimpleText or any of the previously mentioned UNIX tools available for Java source file creation.)
- Top
- 629 One disadvantage of using simple text editors such as Notepad or Wordpad is that they do not display line numbers as you edit.
- 629 Seeing the line number helps in Java programming because many compilers indicate the line number where an error occurred.
- Take a look at the following error generated by the JDK compiler:
- Palindrome.java:2: Class Font not found in type declaration. reserve
- 629 The number 2 after the name of the Java source file indicates the line that triggered the compiler error.
- With a text editor that supports numbering, you can go directly to that line and start looking for the error.
- 629 Usually there are better ways to degug a program with a Java integrated development environment.
- But kit users must search for compiler-generated errors using the line number indicated by the javac tool.
- This is one of the best reasons to move on to an advanced Java development program after learning the language with the kit.
- Top
- 629 TIP: Another alternative is to use the kit with a programmer's text editor that offers line numbering and other features.
- One of the most popular for Java is jEdit, a free editor available for Windows, Linux, and other systems at www.jedit.org.
- I use UltraEdit, an execellent programmer and web designer's editor that currently sells for $79.95.
Creating a Sample Program
- Top
- 629 Now that you have installed and set up the Java Development Kit, you're ready to create a sample Java program to make sure it works.
- 629 Java programs begin as source code - a series of statements created using a text editor and saved as a text file.
- You can use any program you like to create these files, as long as it can save the file as plain, unformatted text.
- 630 The kit does not include a text editor, but most other Java development tools include a built-in editor for creating source code files.
- 630 Load your editor of choice and enter the Java program shown in Listing D.1.
- Be sure to correctly enter all the parentheses, braces, brackets, and quotation marks in the listing, and capitalize everything in the program exactly as shown.
- If your editor requires a filename before you start entering anything, call it HelloUser.java.
Listing D.1 - Source Code of HelloUser.java - goes here.
- public class HelloUser {
public static void main(String[] arguments) {
String username = System.getProperty("user.name");
System.out.println("Hello " + username);
}
}
- Top
- 630 The line numbers and colons at the begining of each line are not part of the program.
- They're included so that I can refer to specific lines by numbers in each program as you read this book.
- If you're ever unsure about the source code of a program in this book, you can compare it to a copy on the book's official website at www.java21days.com.
- 630 After you finish typing in the program, save the file somewhere on your hard drive with the name HelloUser.java.
- Java source files must be saved with the extension .java.
- Top
- 630 TIP: If you have created a folder called J21work, you can save HelloUser.java in that folder.
- This makes it easier to find the file while using a command-line window.
- 630 If you're using Windows, a text editor such as Notepad might add an extra .txt file extension to the filename of any Java source files you save.
- For example, HelloUser.java is saved as HelloUser.java.txt.
- As a workaround to avoid this problem, place quotation marks around the filename when saving a source file.
- Top
- 630 TIP: A better solution is to permanently associate .java files with the text editor you'll be using.
- In Windows, open the folder that contains HelloUser.java, and double-click the file.
- If you have never opened a file with the .java extension, you're asked what program to use when opening files of this type.
- Choose your preferred editor, and select the option to make your choice permanent.
- From this point on, you can open a source file for editing by double-clicking the file.
- 631 The purpose of this project is to test the Java Development Kit.
- None of the Java programming concepts used in the six-line HelloUser program are described in this appendix.
- 631 You learn the basics of the language during the first several days of Week 1, "The Java Language."
- If you have figured out anything about Java simply by typing in Listing D.1, it's your own fault.
Compiling and Running the Program in Windows
- Top
- 631 Now you're ready to compile the source file with the kit's Java compiler, a program called javac.
- The compiler reads a .java source file and creates one or more .class files that can be run by the Java Virtual Machine.
- 631 Open a command-line window; then open the folder where you saved HelloUser.java.
- 631 If you saved the file in the J21work folder inside the root folder on your main hard drive, the following MS-DOS command opens the folder:
- 631 When you are in the correct folder, you can compile HelloUser.jav by entering the following at a command prompt:
- 631 Figure D.8 shows the MS-DOS commands used to switch to the \J21work folder and compile HelloUser.java.
Figure D.8 - Compiling a Java program in a command-line window. - goes here
- Top
- 631 The kit's compiler does not display a message if the program compiles successfully.
- If there are problems, the compiler tells you by displaying a message explaining each error along with the number of the line that triggered the error.
- 632 If the program compiles without any errors, a file called HelloUser.class is created in the same folder that contains HelloUser.java.
- 632 The class file contains the Java bytecode that a Java Virtual Machine will execute.
- If you get any errors, go back to your original source file and make sure that you typed it in exactly as it appears in Listing D.1.
- Top
- 632 After you have a class file, you can run that file using a JVM.
- The kit's JVM is called java, and it also is run from the command line.
- 632 Run the HelloUser program by switching to the folder containing HelloUser.class and entering the following:
- 632 You see the text "Hello" followed by a space and your username.
- Top
- 632 CAUTION: When running a Java class with the kit's JVM, don't specify the .class file extension after the class's name.
- If you do, you'll see an error such as the following:
-
Exception is thread "main"
java.lang.NoClassDefFoundError: HelloUser/class
- 632 Figure D.8 shows the successful output of the HelloUser application along with the commands used to get to that point.
- 632 If you can compile the program and run it successfully, your kit is working, and you are ready to start Day 1, "Getting Started with Java."
- 632 If you cannot get the program to compile successfully even though you have typed it in exactly as it appears in the book,
- there may be one last problem with how the kit is configured on your system.
- The CLASSPATH environment variable might need to be configured.
Setting Up the CLASSPATH Variable
- Top
- 632 All the Java programs you write rely on two kinds of class files: the xlasses you create,
- and the Java Class Library, a set of hundreds of classes that represent the functionality of the Java language.
- 632 The kit needs to know where to find Java class files on your system.
- In many cases, the kit can figure this out on its own by looking in the folder where it was installed.
- 633 You also can set it up yourself by creating or modifying another environment variable: CLASSPATH.
Setting the CLASSPATH on Most Windows Versions
- Top
- 633 If you have compiled and run the HelloUser program successfully, the kit has been configured successfully.
- You don't need to make any more changes to your system.
- 633 On the other hand, if you see a Class not found error or NoClassesFound error whenever you try to run a program,
- you need to make sure your Classpath variable is set up correctly.
- 633 On most versions of Windows, including Windows 7 thru 10, you configure the Classpath variable using the Environment Variables dialog.
- (Skip to the next section if you're using Windows 98 or Me.)
- 633 To open this dialog on Windows 7, 8, or 10, follow these steps:
- Click the Start button on the taskbar.
- Click the Search icon (a magnifying glass) at the upper right.
- Type Environment Variables in the search box.
- Click the result Edit the System Environment Variables. The System Properties dialog opens with the Advanced tab in front.
- Click the Environment Variables button. The Environment Variables dialog opens, as shown in Figure D.9.
- Top
- 633 To open it in other versions, do the following:
- Right-click the My Computer icon on your desktop or Start Menu, and choose Properties. The System Properties dialog opens.
- Click the Advanced tab to bring it to the front.
- Click the Environment Variables button. The Environment Variables dialog opens, as seen in Figure D.9.
- Top
- 633 If your system has a Classpath variable, it probably is one of the system variables.
- Your system may not have a Classpath variable set.
- Normally the kit can find class files without the variable.
- 633 However, if your system has a Classpath, it must be set up with at least two things:
- a reference to the current folder (a period) and a reference to a file that contains the Java Class Library, tools.jar.
Figure D.9 - Setting environment variables in most Windows versions. - goes here.
- Top
- 634 If you installed the kit in the C:\Program Files\Java\jdk1.8.0_60 folder, tools.jar is in the folder C:\Program Files\Java\jdk1.8.0_60\lib.
- 634 If you can't remember where you installed the kit, you can look tools.jar by choosing Start, Search (or Start, Search icon) from the Windows taskbar.
- If you find several copies, you should be able to find the correct one using this method:
- Use CD to open the folder that contains the JJVM (java.exe).
- Enter the command CD ..
- Enter the command CD lib
- 634 The lib folder normally contains the right copy of tools.jar.
- 634 When you know the correct folder, return to the Environment Variables dialog, shown in Figure D.9
- 634 If your system does not have a Classpath, click the New button under the System Variables list.
- The New System Variable dialog opens.
- 634 If your system has a Classpath, choose it and click the Edit button.
- The Edit System Variable dialog opens.
- 634 Both dialogs contain the samething: a Variable Name field and a Variable Value field.
- 635 Enter Classpath in the Variable Name field and the correct value for the Classpath in the Variable Value field.
- 635 For example, if you installed the kitin C:\Program Files\Java\jdk1.8.0_60, your Classpath should contain the following:
- C:\Program Files\Java\jdk1.8.0_60\lib\tools.jar
- 635 After setting up your Classpath, click OK twice: once to close the Edit or New System Variable dialog, and again to close the Environment Variables dialog.
- 635 To see whether this change has fixed your problem, open a new command-line window and type the command java -version.
- 635 If it displays the correct version of the kit, your system should be configured correctly and require no more adjustments.
- Try creating the sample HelloUser program again.
- It should work after the CLASSPATH variable has been set up correctly.
Setting the CLASSPATH on Windows 98 or Me
- Top
- 635 If you are using Windows 98 or Me and the HelloUser program fails with a Class not found or NoClassDefFound error when you run it,
- you need to make sure your CLASSPATH variable is set up correctly.
- 635 To do this, run Windows Notepad and choose File, Open, Go to the root folder on your system, and then open the file AUTOEXEC.BAT.
- A file containing several MS-DOS commands is loaded in the editor.
- 635 Look for a line in the file that contains the text SET CLASSPATH= command followed by a series of folders and filenames separated by semicolons.
- 635 CLASSPATH is used to help the Java compiler find the class files it needs.
- A CLASSPATH can contain folders or files.
- It also can contain a period character (.), which is another way to refer to the current folder in MS-DOS.
- 635 You can see your system's CLASSPATH variable by typing the following command at a command line:
- Top
- 635 If your CLASSPATH includes folders or files that you know are no longer on your computer,
- you should remove the references to them on the SET CLASSPATH= line in AUTOEXEC.BAT.
- Be sure to remove any extra semicolons also.
- 635 To set up the kit correctly, you must include the file containing the Java Class Library in the SET CLASSPATH= command.
- This file has the filename tools.jar.
- If you installed the kit in the C:\Program Files\Java\jdk1.8.0_60 folder on your system, tools.jar is probably in the folder C:\Program Files\Java\jdk1.8.0_60\lib.
- 636 If you can't remember where you installed the kit, you can look for tools.jar by choosing Start, Find, Files or choosing Folders from the Windows taskbar.
- If you find several copies, you should be able to find the correct one using this method:
- Use CD to open the folder that contains the Java Virtual Machine (java.exe).
- Enter the command CD ..
- Enter the command CD lib
- 636 The lib folder normally contains the right copy of tools.jar.
- 636 When you know the correct location, create a blank line at the bottom of the AUTOEXEC.BAT file and add the following:
- SET CLASSPATH=%CLASSPATH%;.;rightlocation
- 636 For example, if the tools.jar file is in location C:\"Program Files"\Java\jdk1.8.0_60\lib folder, add the following line at the bottom of the AUTOEXEC.BAT:
- SET CLASSPATH=%CLASSPATH%;.;C:\"Program Files"\Java\jdk1.8.0_60\lib\tools.jar
- 636 After making changes to AUTOEXEC.BAT, save the file and reboot your computer.
- After this is done, try to compile and run the HelloUser sample program again.
- You should be able to accomplish this after the CLASSPATH variable has been set up correctly.
The End of Appendix D