410 likes | 532 Views
Lesson1 How to get started with Java. Description Versions 1.0 and 1.1 of the Java toolkit were called the Java Development Kit , or JDK . Versions 1.2 through 1.4 of the Java toolkit are called the Software Development Kit , or SDK .
E N D
Lesson1 How to get started with Java
Description • Versions 1.0 and 1.1 of the Java toolkit were called the Java Development Kit, or JDK. • Versions 1.2 through 1.4 of the Java toolkit are called the Software Development Kit, or SDK. • The Java 2 Platform, Standard Edition, or J2SE, supports versions 1.2 through 1.4 of the SDK. • The Java 2 Platform, Enterprise Edition, or J2EE, can be used to create enterprise- level, server-side applications.
Description • Any text editor can save and edit the source code for a Java application. Source code files use the java extension. • The Java compiler translates source code into a platform- independent format known as Java bytecodes. Files that contain Java bytecodes use the class extension. • The Java interpreter executes Java bytecodes. Since Java interpreters exist for all major operating systems, Java bytecodes can be run on most platforms. Any computer with a Java interpreter can be considered an implementation of a Java virtual machine (JVM). • Some web browser is like Netscape and the Internet Explorer contain Java interpreters. This lets applets run within these browsers. However, both Netscape and the Internet Explorer only provide older versions of the Java interpreter. • Sun provides a tool known as the Java plug-in that allows the Netscape and Internet Explorer browsers to use the most current version of the Java virtual machine.
The Java web site address www.java.sun.com How to download and install the SDK from the Java web site Go to http://java.sun.com/docs/books/tutorial/getStarted/cupojava/index.html Or 1. Go to the Java web site. 2. Locate Java products and find the Java 2 Platform, Standard Edition. http://java.sun.com/j2se/1.4.1/download.html 3. Go to the download page for the most current SDK version that is available for your platform. 4. After clicking on the download button, follow the instructions. Note the name of the file and the download size. 5. Select one of the FTP download options, unless you're behind a firewall and you need to use the HTTP option.
6. Save the set up file to your hard disk. On a 56K modem, takes about two hours to download this file. 7. Once the entire package has download, check to make sure that you got the executable and that the size is correct. Otherwise, you'll get an error when you try to run the executable. 8. Run the exe file, and respond to the resulting dialogue boxes. When you prompted for the SDK directory, use the default directory and install all the components unless disk space is a problem.
The summary of the directories and the files of the SDK Figure 1-5 shows the directories and files that created when you install the SDK. Here, the SDK is stored in the c:\j2sdk1.4.0 directory. By default, this directory has five sub directories: bin, demo, include, jre, and lib. The bin directory holds all the tools necessary for developing and testing a program including the Java compiler. The demo directory contains many sample applications and applets. You can browse through these to learn more about what Java can do, and you can review the source code. The include directory holds header files for the C programming language that allows you to incorporate C code into a Java program. The jre directory contains the Java interpreter, or Java Runtime Environment (JRE), as needed to run Java applications once have been compiled. Although the SDK uses this internal version of the JRE, you can also download stand-alone version of the JRE from the Java web site. Once you've done developing the Java application, for example, you can distribute the stand-alone JRE to other computer so they can run your application. The lib directory contains libraries and supporting files required by the development tools.
The last directory is the docs, which is used to store the Java documentation. In chapter 3, you'll learn how to download and install this documentation. In the j2sdk1.4.0 directory, you confined to readme files that contain much of the information that's presented in this figure as well is more technical and detailed information about the SDK. You can review the HTML file with a web browser, and you can open the text file with a text editor. The j2sdk1.4.0 directory also contains the src.zip file. This file holds the source code for the SDK. Before you can view the source code, though, you must extract the files that contain the source code from this ZIP file. When you work with Windows, you'll find that uses the terms folder and subfolder to refer to DOS directories and subdirectories. For consistency, though, we use the term dictionary throughout this book. In practice, these terms are often used interchangeable.
Description • The Java Runtime Environment, or JRE, is the Java interpreter that allows you to run compiled programs in Java. The jre directory is an internal copy of the runtime environment that works with the SDK. You can also download stand-alone version of the JRE for computers that don't have the SDK installed on them.
How to set the path for Windows 95/98/ME 1. Start the Windows Explorer and navigate to the autoexec.bat file, which should be stored in the root drive (c:\autoexec.bat). 2. Right-click on the autoexec.bat file and select the Edit command. This should open the file on a text editor. 3. The file contains a Path or Set Path command, type a semicolon at the end of the command. Then, type "c:\j2sdk1.4.0\bin" as shown above. If the file doesn't contain a Path or Set Path command, enter "path=c:\j2sdk1.4.0\bin"at the beginning of the file. 4. Save the file and exit the text editor. 5. To have the new path take effect, you can restart your computer (which runs the autoexec.bat file).
How to set the path for Windows 2000/NT/XP 1. Go to the Start menu, point to Settings, and select the Control Panel. 2. Edit the environmental variables for your system. For NT, select the System icon, and the Environment tab. For 2000, select the System icon, the Advanced tab, and the Environment Variables button. For XP, select the Performance and Maintenance icon, the System icon, the Advanced tab, and the Environment Variables button. 3. Add c:\j2sdk1.4.0\bin to the far right side of the current path in System Variables and select the OK. How to check the current path and classpath • Start a DOS prompt and enter the set command. When you do, the DOS window will display various configuration data including the path and classpath.
When and how to modify the classpath • If your system doesn't have a classpath, the default class path will allow you to run all of the programs described in this book. As a result, you won't need to modify the classpath. However, if your system has a classpath that doesn't include the current directory, which is specified by a period (.), you need to add the current directory to the classpath. • To modify the classpath, follow the procedure shown above for modifying the path, but modify the classpath command instead. Description • For more information about configuring your system, or for information about configuring non-Windows operating systems, you should refer to the Java web site.
Syntax to save the code and file "ProgramName.java" Typical capitalization for file names Book.java BookOrderApp.java Operation • To start Notepad, click on the Start menu, select Programs, select Accessories, and select Notepad. • To enter or edit code, use the same techniques that you use with any other text editor or word processor. • To save the source code, select the Save command from the File menu and enter the filename without quotation marks. That way, the file will be save the capitalization that you've used and with the java extension. Otherwise, the capitalization may be changed or the extension may be truncated to jav. (On some versions of Windows, the quotation marks may not be necessary, so you may want experiment with this.)
Syntax to compile an application javac PogramName.java Syntax to run an application java ProgramName Description • The DOS prompt, or command prompt, is the prompt that indicates that the operating system is waiting for the next command. When you use DOS, this prompt usually shows the current directory, and always ends with >. In the example above, the last line is the command prompt, which shows the current directory is c:\java\ch01.
Operation • To open the DOS Prompt window with Windows 95, 98 or NT, click on the Start button, and select MS-DOS Prompt. With Windows 2000, ME, or XP, click on the Start button, select Accessories, and select Command Prompt. • To change to the directory that contains the file with your source code, use the change directory command (cd) as shown above. • The compile the source code, enter the Java compile command (javac), followed by the filename (including the Java extension). • If the code compile successfully, the compiler generates another file with same name, but with class is the extension. This file contains the bytecodes.
If the code doesn't compile successfully, the Java compiler will generate error messages. Then, you must switch back to your text editor, fix the errors, save the changes, and trying compiling the program again. • To run the compiled version of your source code, enter the Java command (Java), followed by the program name without any extension. Since this is the case-sensitive command, make sure to use the same capitalization that you used when naming the file. Note • The code shown in the DOS Prompt window above will only work if C:\j2sdk1.4.0\bin has been added to the command path as shown in figure 1-6. If the code compiles but doesn't execute, you may need to edit your classpath settings as shown in figure 1-6.
Two common compile-time error messages and solutions Error: BookOrderApp.java:3: ‘;’ expected System.out.println(“Title: War and Peace”) ^ Description: The first line in this error messages displays the filename of the *.java file, the number indicating the line where the error occurred, and a brief description of the error. The second line displays the line of code that may have caused the error with a caret symbol (^) below the location where there may be improper syntax. Solution: Use a text editor to correct the problem and save the file.
Error: BookOrderApp.java:1: class Bookorder is public, should be public class BookOrder{ ^ Description: The *.java filename doesn't matched the name of the public class. Remember, you must save the file with the same name as the name that's coded after the words "public class". In addition, you must add the java extension to the filename. Solution: Enter the correct filename after the javac command. Or, use the text editor to save the java file with the appropriate spelling and capitalization.
A common run-time error message and solution Error: Exception in thread “main” java.lang.NoClassDefFoundError: bookorderapp (wrong name: BookOrderApp) Description: The name that was entered for the *.class file isn't correct. Remember, you must use the proper capitalization and you must admit the class extension. Solution: Enter the correct name after the java command. You can use the dir command to check the capitalization for the file as shown in the next figure. If necessary, you may need to use the javac command to recreate the *.class file from the *.java file.
TextPad Editor How to install TextPad www.textpad.com TextPad with source code in it