to the publisher - slowchinese.com · complete workbook for intelligent people who value their...

116
TO THE PUBLISHER... i TO THE PUBLISHER... 24 July 2002 Terry Taylor contact info: - Email: [email protected], [email protected] - Mobile: + 7 921 916 2841 (St. Petersburg, Russia) or +49 173 2845 784 (Berlin). - Website: www.hotterrytaylor.com (examples of work, resume, etc. available for download). These first few pages provide info for you (the publisher): 1. Contents of this pdf file (pg. i) 2. Why publish this book? (pg. ii) 3. Projected delivery date and current status (pg. ii) 4. Book overview (pg. ii) 5. CD ROM (pg. iv) 6. About the author (Terry Taylor) (pg. iv) 1. Contents of this pdf file This pdf file is a partial copy of the book “Hello World! Getting Started with Java for Windows (a complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher) a good idea of what the completed book will look like. This partial copy includes: - The first “pages” the potential reader sees: - (front cover) - (back cover) - Sections that should convince someone to buy the book: - Is this book for you? (pg. 3) - Learning with this book (pg. 5) - Contents (pg. 11) of the book - 1. Parts description (pg. 11). Describes all parts. - 2. TOC (pg. 12). Lists the book headings down to the first chapter subheading level. - The first few pages of each part (describes the part and all chapters in the part) - Part O. Overviews (pg. 17) - Part HU. "Hello to User!" on a PC (pg. 39) - Part HUc. "Hello to User!" on a CD (pg. 143) - Part HW. “Hello to World!” PC->PC (pg. 177) - Part L. Language(s) (pg. 265) - Part RU. “Reply from User!” on a PC (pg. 373) - Part RW. “Reply from World!” PC<->PC (pg. 385) - Part V. Versions (pg. 433) - Part D. Documentation (pg. 443) - Part E. Examples (advanced) (pg. 453) - Part Z. The last part (pg. 487) - Selected chapters - 1. This book / CD ROM (pg. 19) - 4. HTML (pg. 41) - 5. JavaScript (pg. 47)

Upload: others

Post on 26-Apr-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

TO THE PUBLISHER...24 July 2002

Terry Taylor contact info:- Email: [email protected], [email protected] Mobile: + 7 921 916 2841 (St. Petersburg, Russia) or +49 173 2845 784 (Berlin).- Website: www.hotterrytaylor.com (examples of work, resume, etc. available for download).

These first few pages provide info for you (the publisher):1. Contents of this pdf file (pg. i)2. Why publish this book? (pg. ii)3. Projected delivery date and current status (pg. ii)4. Book overview (pg. ii)5. CD ROM (pg. iv)6. About the author (Terry Taylor) (pg. iv)

1. Contents of this pdf fileThis pdf file is a partial copy of the book “Hello World! Getting Started with Java for Windows (a complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher) a good idea of what the completed book will look like. This partial copy includes:- The first “pages” the potential reader sees: - (front cover) - (back cover)- Sections that should convince someone to buy the book: - Is this book for you? (pg. 3) - Learning with this book (pg. 5)- Contents (pg. 11) of the book - 1. Parts description (pg. 11). Describes all parts. - 2. TOC (pg. 12). Lists the book headings down to the first chapter subheading level.- The first few pages of each part (describes the part and all chapters in the part) - Part O. Overviews (pg. 17) - Part HU. "Hello to User!" on a PC (pg. 39) - Part HUc. "Hello to User!" on a CD (pg. 143) - Part HW. “Hello to World!” PC->PC (pg. 177) - Part L. Language(s) (pg. 265) - Part RU. “Reply from User!” on a PC (pg. 373) - Part RW. “Reply from World!” PC<->PC (pg. 385) - Part V. Versions (pg. 433) - Part D. Documentation (pg. 443) - Part E. Examples (advanced) (pg. 453) - Part Z. The last part (pg. 487)- Selected chapters - 1. This book / CD ROM (pg. 19) - 4. HTML (pg. 41) - 5. JavaScript (pg. 47)

TO THE PUBLISHER... i

Page 2: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

- 6. J2SDK/JRE: Download / Install (pg. 49) - 7. Console Application (pg. 55) - 15. Console Application (using DE) (pg. 81) - 27. Install Wireless Toolkit (pg. 145) - 35. Server -> Client (pg. 179) - 36. Server -> Client (http) (pg. 181) - 54. Classes: Basics (define, instantiate, send messages (pg. 267) - 55. Classes: Extensions (class/methods) (pg. 275) - 56. Classes: Location (packages, jars) (pg. 281) - 63. Graphical Interface (pg. 339)This partial copy also includes most of the bookmarks. Thus you can get a good idea of the content of most of the chapters buy viewing the bookmarks.

2. Why publish this book?2.1. The book will sellThe book will sell because

•The target readership (new Java programmers) is large.•The book is very well organized and written.•The book is unique. Although the general subject “HelloWorld!” has been written about many times,this approach of this book is unique.

2.2. The author is ready to meet your requirementsThis is my first book. I realize that there are some things about this business that I dont understand, and therefore some major changes in the book may be required. I am ready and willing to make the required changes.2.3. There could be many future versions of the bookThis book could be constantly updated in the future because existing Java technologies are evolving and new Java technologies appearing rapidly. Future versions of the book could also focus much more on Java on mobile devices.

3. Projected delivery date and current status3.1. Projected delivery date1 November 2002 (version 1). Version 1 of the book should have 450-600 pages.3.2. Current statusThe book is 60-70% ready. My working version (not this partial version) has 400+ pages.Please note the following about the current book:

•The diagrams are very rough. They will all be replaced.•Commentary and text are sparse. I want to start “filling out” the book after the general structure,content and programming examples have been more or less fixed (and meet your requirements anddesires).•There may be incomplete sentences and grammar mistakes.•The fonts may not be perfect (the font size, spacing, etc.). The finished book will contain the fonts,font spacing, page dimensions, etc. that you require. All text in this book uses standard styles, andtherefore the format of the entire book can be changed easily.•Some screenshots may be in German. These will be replaced. Note that in general all pics will bechecked and changed as necessary.

Note also the following about this partial version:•The chapter numbering may be a bit irregular.•Chapters may contain empty pages and the right/left pages may be switched.

These problems appear because the version I am sending you does not include hidden text (my com-mentary) and pages have been automatically and manually deleted. This will not occur in the final release.

4. Book overview

ii TO THE PUBLISHER...

Page 3: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

4.1. Book conceptThe book title says it all:“Hello World! / Most of the chapters are very similar in that they show how to create the simplest pos-sible application that generates a simple phrase (such as “Hello World!”). Each chapter is unique in that it shows how to do this using a specific Java technology (a console application, frame application, beans, JDBC, Servlets, Cookies, etc.). The idea is to give the reader hands-on experience with the var-ious Java technologies by creating the simplest example applications.Getting Started with Java for Windows / The book is basically a step-by-step guide for learning Java on the Windows platform for those with little or no experience.a complete workbook / The book and CD ROM provide all the information that the reader needs to start working with Java (not just reading about it).for intelligent people / The book is not for “dummies” :) .who value their time” / The book is for people who don’t want to spend days reading books filled with useless text and incomplete directions, but rather want to learn hands-on ASAP.4.2. Target audienceThe target audience for this book are non-programmers or non-Java-programmers who want to learn Java. Is this book for you? (pg. 3) tries to convince the potential buyer of the book that he is a mem-ber of the target audience.4.3. How the reader learns with the bookThis book is unique in that it leads the student in a very structured way through the learning process. This is described in detail in Learning with this book (pg. 5).4.4. ContentThe book is organized into approximately 12 parts, with each part containing several chapters.4.4.1. First version of bookThe contents of the first version of the book are described in Contents (pg. 11) by parts (1. Parts description (pg. 11)) and by chapters (2. TOC (pg. 12)). Note in particular the following:- (front cover), (back cover). The front and back covers of the book (what I propose).- Is this book for you? (pg. 3). The first pages of the printed book. Explains to the potential buyer in the bookstore why he should buy the book.- Learning with this book (pg. 5). Describes how the reader should use the book and CD ROM to learn Java.- Part O. Overviews (pg. 17) contains overview (introductory) chapters that do not include program-ming. Includes Ch. 1. This book / CD ROM (pg. 19), which describes this book in detail.4.4.2. Future versions of bookIn future versions of this book I would like to add parts and chapters for evolving Java technologies (especially mobile devices and the internet). For version 2 I would like to add the following:

•Part HWc. "Hello to World!" PC->CD. Applications that send the phrase “Hello World!” from a PCthru the internet and mobile phone network to a consumer device (mobile phone, etc.).•Part RUc. "Reply from User!" on a CD. Applications that run on consumer devices that allow userinput.•Part RWc. "Reply from World!" CD<->PC. Applications that allow user input on the consumerdevice that is sent thru the mobile and internet network to the PC.

Thus parts HU to RWc would look like the following:

TO THE PUBLISHER... iii

Page 4: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

4.5. Drawings and diagramsThe drawings and diagrams will be redone by a professional artist for the final version.4.6. Delivery formatThe book was created with Framemaker 6 (I will soon upgrade to Framemaker 7). I can deliver the book in any format that you want (page sizes, color palettes, fonts, etc.).

5. CD ROMThe book assumes that a CD ROM will be delivered with the book. The CD ROM is described in detail in section 1.3.2. CD ROM (pg. 22).I have not yet inquired about whether or not the software that can be downloaded for free from the inter-net may be included on the CD ROM. In any case, the book text describes how to download the soft-ware from the internet.

6. About the author (Terry Taylor)A small description of the author is included in section 1.5. Author (pg. 24).6.1. Contact info

•Email: [email protected], [email protected]•Mobile: + 7 921 916 2841 (St. Petersburg, Russia) or +49 173 2845 784 (Berlin).•Website: www.hotterrytaylor.com (examples of work, resume, etc. available for download).

6.2. Professional activities6.2.1. Past

•BS in Electrical Engineering, Purdue University, Indiana, 1986.•Systems and project engineer at a Navy warfare center in Indiana.•Technical writer (in the USA, Germany, Russia, Japan).

6.2.2. Current•Writing this book about Java.•Technical writer for Novavox (www.novavox.com, www.novavox.ru) in St. Petersburg, Russia.Novavox is a Swiss-based company that develops computer telephony applications.

6.2.3. Future•Writing technical books about Java, C#, etc. These books will be similar in style to this book (step-by-step workbooks for “intelligent people who value their time”).•Creating a family of technical books that are interrelated. The student starts out with the “GettingStarted” book, and then, depending on his particular interests, branches out to books that covermore specific topics.

I am currently living in Russia, and I would like to attempt to write these books with the help of (low-cost) Russian programmers.

iv TO THE PUBLISHER...

Page 5: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

(front cover)

Hello World!!Getting Started

with Java for Windows

a complete workbook

for intelligent people

who value their time

Page 6: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

(back cover)

This book providesdetailed, step-by-step instructions for using the following

tools (ALL available forFREE via internet and

included on the enclosedCD ROM):

J2SDK (Java 2 Softwaredevelopment kit)

AntForte

Internet Server (Apache,Tomcat,J2EE,iPlanet)Beans Development Kit

Java Help WorkshopJ2EESDK (Java 2 Enterprise Edition SDK)

WinZipto create the simplest “Hello World!” applications that demo the following technologies

(Parts HU/RU)HTMLJavaScriptConsole ApplofationJar (Java Archive)AppletJava SecurityCode signingDialog ApplofationBeansEncryptionXML / JDBC (Java Database Connection)

(Part HUc)Mobile phonePDA

Next Generation DeviceJava Emulation

Java Card

(Parts HW/RW)Internet Server-Cli-ent

SSL (secure sockets layer)JSP (Java Server Page)

PHPServlet

RMI (Remote method invocation)IDL (??)

EJB (Enterprise Java Beans)JMS (Java Messaging Servofe)

SessionsCookies / JavaDoc /Java2Html Java Help

and (Part L) provides a step-by-step intro to the following languages:

JavaJavaScript

JSP (Java ServerPages)

that finally explains“object-oriented” pro-

gramming in a waythat is easy to under-

stand and makessense!

Page 7: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

Is this book for you?

Is this book for you?This book is for you if you answer yes to the following questions:•Do you want to “Get started with Java for Windows”?•Are you looking for a complete workbook that gives you everthingyou need for hands-on experience NOW?•Are you intelligent? (you are :) )•Do you value your time? (you should :) )

1. Do you want to “Get started with Java for Windows”?This book is for you if you want to get started with Java for Windows (you have little or no experience with Java). Getting started with Java can be a bit frustrating, since even the smallest mistake might require hours to find. This book will quickly get you started, giving you that first-hand experience that will enable to quickly resolve any problems you encounter when trying to create and run Java applications. You will then be able to tackle more complex programming examples (that you find

in the internet, in advanced Java books, etc.).

2. Are you looking for a complete workbook?If you want to get started programming Java, then you need a complete workbook that provides everything you need.

Complete, because details are everything. Getting started with Java is not easy. If you have little or no experience, then you can spend days trying to find the answer to some small problem.This book pro-vides those details, so that you dont get stuck and give up along the way.Complete, because all you need in addition to this book is:

•A computer with Windows•An internet connection (recommended strongly, but actually you can do the examples without it)

The examples in this book use development environments and tools that are FREE for download (and also on the supplied CD ROM).This book is a workbook, becausing it provides straightforward, detailed step-by-step instructions for creating the simplest (Hello World) Java applications that run on the following platforms

•Windows PC•Consumer devices (CD) (Handheld, Mobile Phone, SmartCard, etc.)•Internet

A workbook, because it shows you first how to do an example, and then explains conceptually what you did. Many books for learning Java start out with the history of Java, then conceptual block dia-grams of the Java architecture, then a detailed explanation of the difference between integers and long integers, etc. Theory is great, but hands on experience is what counts. This book gives you what you need to get that experience.

Java Getting Started 3of 501

Is this book for you?

Page 8: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

4of 501

Is this book for you?

3. Are you intelligent?

Books are not read by dummies... books are written by dummies. :)This book is for you, the intelligent reader.Intelligent, because you understand that you have what it takes to learn Java, if you could just get the (clear and concise) info you need to start programming! You under-stand that nobody (yes, nobody) can learn or has learned Java by simply reading books, but rather by programming!This book provides clear and concise directions that include those important little

details so that almost anyone (even with minimal PC experience) can start programming.

4. Do you value your time?IThis book is for you if you value your time.This book will save you an immense amount of of time.In programming, details are everything. Sitting down in front of the computer and trying to actually do what you read about in a book, you discover very quickly if the important details that you require are there or not. If not, then you either start searching for an experienced Java programmer, spend hours (or days) hacking,

or simply give up.This book provides those critical details, so that you can spend your time programming.

Java Getting StartedIs this book for you?

Page 9: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

Learning with this book

Learning with this bookIf the previous section ( Is this book for you? (pg. 3) ) did not convince you to buy this book, or if you have already bought it and now want to get started programming, then read this section to get a good idea of how to use this book (and CD ROM) to learn Java.

1. Get a “clean” computer with Windows 2000This book is written assuming that you have a “clean” PC with the Windows 2000 operating system in English. “Clean” means that no Java programs are currently installed on the computer. This is really not impor-tant if you have some computer experience already. However, if you have no experience, then any pre-viously installed Java applications might force you to do things a bit differently than described in the step-by-step directions.Windows 2000 English is not a strict requirement. Again, however, if you have a different OS (such as Win95) or language, then some things will be a bit different than described in this book.

2. Get an internet connection (optional)The included CD ROM contains all software required to do the examples in this book.However, an internet connection is recommended for the following reasons:

•A more recent version of the software may be available.•A few examples require an internet connection (for verification of DTD’s).

3. Determine what you want to learnYou probably want to do the book from start to finish. However, this book is designed to make it easy to start immediately with the parts or chapters that specifically interest you.3.1. Select individual chapter(s)...Each chapter introduces a particular technology (console application, beans, JDBC, etc.). Therefore, if you want to learn something specific (for example, JDBC) then simply do the corresponding chapter(s). This is easy, since each chapter is “self-contained”: Each chapter begins with the section “System requirements”, which describes any software that must be installed to do the examples in the chapter. 3.2. or select a part (includes many chapters)Each part includes multiple chapters that focus on a particular programming situation. The following describes the major parts.3.2.1. Part OPart O. Overviews (pg. 17) contains no code, but rather “overviews” of

•this book•the software profession•computing platforms and languages

Therefore, if you want to immediately start programming, then skip this part.3.2.2. Parts HU, HUc, and HWThese 3 parts are designed to introduce you to Java technologies (not the language itself). The exam-ple applications in these 3 parts are all similar in that they simply generate the phrase “Hello user!” or “Hello World!”. The difference between the parts is the platform on which the programs run. There are 3 different platforms.- Part HU. "Hello to User!" on a PC (pg. 39). Example applications that run on a single PC.- Part HUc. "Hello to User!" on a CD (pg. 143). Example applications that run on a “consumer device” (“CD”; includes non-PC devices such as mobile phones, smart cards, etc.). The applications are actu-ally run on a CD emulator on the PC.

Java Getting Started 5of 511

Learning with this book

Page 10: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

6of 511

Learning with this book

- Part HW. “Hello to World!” PC->PC (pg. 177). Example applications that run on a client PC and a server PC. The client and server are normally connected via internet, but in this part they both run on the same PC.By doing these parts you will get hands-on experience with a variety of Java technologies. However, these parts do not explain the details of the Java code in the applications. Details of the Java language are introduced in the next part.3.2.3. Part LHaving seen what you can do with Java (in Parts HU, HUc, and HW), you now need to learn the basics of the Java language itself. Part L. Language(s) (pg. 265) presents the Java language from a much more different perspective than the typical Java book. It starts with clear and simple examples that show you from a programming perspective how object-oriented programming is implemented in Java. It also describes the basics of Java Script and Java Server Pages (and thus the language”s”).3.2.4. Parts RU and RWHaving gained a good basic understanding of the Java language, you will now “revisit” 2 of the earlier parts (HU and HW). Parts Part RU. “Reply from User!” on a PC (pg. 373) and Part RW. “Reply from World!” PC<->PC (pg. 385) expand on parts HU and HW by creating more sophisticated applications that accept user input.Thus, parts RU and RW somewhat combine what you learned in parts HU, HW and L. However, RU and RW also introduce some new technologies (such as cookies and sessions) that are best demon-strated with user input.Note that there is no Part RUc (“Reply to User”) on a CD (consumer device). Consumer devices are somewhat specialized, and going into more detail about them is probably for the vast majority of read-ers of this book not of interest.3.2.5. Parts V, DThese parts introduce additional topics that are important for anyone who wants to get a job creating real-world Java applications.Part V. Versions (pg. 433) chapters show how to create and manage multiple versions of a Java pro-gram.Part D. Documentation (pg. 443) chapters show how to create the documentation that allow others to use your applications effectively.3.2.6. Part EAt this point you have completed the major parts of the book. Part E. Examples (advanced) (pg. 453) shows how to do advanced examples from Sun. You can spend a great deal of time trying to do these examples with the documentation provided with the code. This book provides the info you need to “quickly” do the examples.Good programmers learn by analyzing, modifying and using the code of others. Therefore, after getting the applications in Part E running, try to modify them.3.2.7. Part ZPart Z. The last part (pg. 487) includes appendices, index, etc. It is similar to Part O in that it does not involve programming.

4. Start programmingNow that you have an idea of what you want to do, its time to do it.4.1. Fulfill the system requirementsThe first section of each chapter is “System requirements”. It describes the software which must be installed before you can do the examples in the chapter, and references the chapter where step-by-step instructions for the installation can be found.4.2. Follow the directionsEach chapter contains step-by-step directions that allow you to quickly do the examples without getting stuck on technical problems. Java can be extremely difficult at the beginning: If you make a single small mistake, then sometimes nothing will work. Therefore, follow the directions carefully!

Java Getting StartedLearning with this book

Page 11: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

Learning with this book

Most chapters will ask you to do the following.4.2.1. Create the dir for chapterFor each chapter you will create the directory

c:\jgs\XXwhere

•“jgs” stands for “java getting started”•XX = the chapter number (07, 22, etc.)

For example, c:\jgs\14 would contain all files (code sources, compiled code, html files, etc.) created in Chapter 14.By creating the directories as described above, you can easily find the code created in a particular chapter by simply viewing the subdirectories of c:\jgs. For example, having completed a few chapters in the book, the contents of c:\jgs might look like the following:

Figure 1. Example dir c:\jgs 4.2.2. Install any required softwareNote, however, that a chapter may also describe how to install a particular application. If you follow the directions in the chapter for installing the applicaiton, then you will create a directory of the form

c:\jgs\XX_appnamewhere “appname” is the name of the application. For example, c:\jgs\14_forte4j would contain the files installed for Forte for Java as described in Chapter 14. This is shown in the screen shot above.Each chapter describes how you can get the required software for the chapter from the:

•included CD ROM (in directory \jgs\installers) or via•internet (note that the internet web pages could be changed at any time, and therefore the informa-tion in this book may not be exact)

The CD ROM directory \jgs\installers might look like the following:

Figure 2. Dir \jgs\installers on the CD ROM where the name of each application begins with the number of the chapter in which the application is installed.4.2.3. Create the code filesThe required code for each chapter is

•listed in full in the book•included on the CD ROM in the text file \jgs\XX\XX_code+commands.txt (this makes it easy tosimply copy and paste the code when creating source files)

Java Getting Started 7of 511

Learning with this book

Page 12: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

8of 511

Learning with this book

•included on the CD ROM as a text file in directory \jgs\XX (you can simply copy the source file fromthe CD ROM)

where XX = the chapter number.For example, assume that the CD ROM directory \jgs\57 for Chapter 57 contains the following:

Figure 3. Example code dir \jgs\57 on the CD ROM The code for Chapter 57 section 1.3.1 would would be found in the files

•J57_131_InstantAbstract.java•57_code+comments.txt

The contents of 57_code+comments.txt

Figure 4. 57_code+comments.txt The text “57.6” above is the number of the chapter step where the code is listed.4.2.4. Execute commandsThe commands that are executed in a chapter (usually in a DOS window) are

•listed in full in the book•included in the CD ROM text file \jgs\XX\XX_code+commands.txt (this makes it easy to simply copyand paste the command in the DOS window)

where XX = the chapter number.For example, 57_code+commands.txt includes all the commands required in chapter 57:

Figure 5. Example commands for chapter 57 on the CD ROM In the above pic, the command for step 57.3 is listed.4.3. Review what you have doneThe last section of each chapter is “Review”. This section

•reviews and explains conceptually what you have done•recommends where to go for more information

4.4. Repeat the chapter

Java Getting StartedLearning with this book

Page 13: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

Learning with this book

It is highly recommended to do the examples in a chapter several times and to experiment with small changes to the code. Practice makes perfect.

5. Go furtherHaving completed the chapters that are of interest to you (hopefully most chapters in the book), you need to start thinking about what to do next.5.1. Repeat the chapters with code modificationsIf you completed a lot of chapters, then you may have forgotten a lot of the details of what you learned. Therefore, considering doing the chapters again, making as many code changes as possible. If possible, do this on a different computer computer. In this way you are also forced to pay attention to those very important “minor” details that are so critical when programming in Java.5.2. Explore new sources of infoCh. 98. Where to go from here (pg. 489) describes where to best get information to further your Java education.

Java Getting Started 9of 511

Learning with this book

Page 14: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

10of 511

Learning with this book

Java Getting StartedLearning with this book

Page 15: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

Contents

ContentsThis section describes the contents of the book with the following:

•A Parts description•A TOC down to the chapter subheading level (chapter descriptions can be found at the beginning ofeach part)

1. Parts descriptionPart O contains introductory chapters that can be skipped.Part O. Overviews (pg. 17). Provides overview information about this book and the java profes-sion / language. Does not include any program-ming. Skip this part (go immediately to Part HU. "Hello to User!" on a PC (pg. 39)) if you want to start programming now.

Parts HU, HUc, HW introduce Java tech-nologies with simple “Hello World!” appli-cations.Part HU. "Hello to User!" on a PC (pg. 39). The most important part of this book. This part dem-onstrates (step-by-step) how to create simple programs that say “Hello to User” on a PC (HtU_PC) (the classic “Hello World” type of appli-cations).Part HUc. "Hello to User!" on a CD (pg. 143). Java runs not only on PC’s, but also on various “consumer devices” (“CD”). This part demon-strates how to create simple programs that say “Hello to User” on a CD (HtU_CD). You create these programs with tools that designed for the PC, and then you run the programs on PC pro-grams that emulate the CD’s (since you probably dont have the various CD’s available).Part HW. “Hello to World!” PC->PC (pg. 177). Java is designed not for an island (a single com-puter), but for the world (the internet). This part demonstrates how to create simple programs that say “Hello to World” from a PC (Server) to another PC (Client) (HtW_PCPC) via the inter-net. Note that although the programs run on a single computer, they would just as easily run on 2 different computers connected via Internet.Part L introduces the Java language(s).Part L. Language(s) (pg. 265). Parts RU, RW extend the applications introduced in parts HU, HUc, HW with the capability to accept user input.

Part RU. “Reply from User!” on a PC (pg. 373). Simple programs that allow a Reply from the User on a PC.Part RW. “Reply from World!” PC<->PC (pg. 385). Simple programs that allow a Reply from the client PC to the server PC (via internet).Parts V,D,E are recommended after hav-ing completed the above “core” parts.Part V. Versions (pg. 433). Part D. Documentation (pg. 443). Demonstrates how to create documentation and help for a Java program.Part E. Examples (advanced) (pg. 453). Dem-onstrates step-by-step how to do some advanced (and very interesting) Java examples.Parts Z is the last part.Part Z. The last part (pg. 487). Includes:

•A list of websites and books for increasingyour Java ability after having completed thisbook.•Trouble-shooting and FAQ chapters.•List of figures and index.

Java Getting Started 11of 501

Contents

Page 16: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

12of 501

Contents

2. TOCTO THE PUBLISHER... ..................................... i

1.Contents of this pdf file ...................................... i2.Why publish this book? ..................................... ii3.Projected delivery date and current status........ ii4.Book overview................................................... ii5.CD ROM .......................................................... iv6.About the author (Terry Taylor)........................ iv

Is this book for you? ...................................... 31.Do you want to “Get started with Java for Win-

dows”? ............................................................. 32.Are you looking for a complete workbook? ...... 33.Are you intelligent? .......................................... 44.Do you value your time? .................................. 4

Learning with this book ................................. 51.Get a “clean” computer with Windows 2000 .... 52.Get an internet connection (optional)............... 53.Determine what you want to learn ................... 54.Start programming ........................................... 65.Go further......................................................... 9

Contents ........................................................ 111.Parts description ............................................ 112.TOC ............................................................... 12

Part O.Overviews - - - - - - - - - - - - - - - - -171. This book / CD ROM ............................... 19

1.1. Core concept............................................... 191.2. Style ............................................................ 191.3. Organization................................................ 201.4. History (how the book got started) .............. 241.5. Author ......................................................... 24

2. Software professions ............................. 252.1. Why become a software professional? ....... 252.2. Why become a Java programmer?............. 252.3. What was and is the software profession? . 252.4. Who are the software professionals?.......... 262.5. How to become a software professional ..... 28

3. Computing platforms / languages ........ 313.1. Computing basics ....................................... 313.2. Operating system........................................ 333.3. Compiled languages ................................... 333.4. Interpreted languages ................................. 343.5. OO languages............................................. 343.6. Java: Compiled / interpreted / OO language343.7. OO jargon and Java.................................... 363.8. Java platforms............................................. 37

Part HU."Hello to User!" on a PC - - - - -394. HTML ....................................................... 41

4.1. System requirements (none)....................... 414.2. Create working folders for chapter.............. 414.3. Text file basics ............................................ 424.4. HTML basics ............................................... 444.5. Review ........................................................ 46

5. JavaScript ............................................... 475.1. System requirements (none)....................... 475.2. Create source text file ................................. 475.3. Open in a browser....................................... 475.4. Review ........................................................ 47

6. J2SDK/JRE: Download / Install ............. 496.1. System requirements .................................. 496.2. Download J2SDK........................................ 496.3. Install........................................................... 496.4. View the installed files................................. 516.5. Set the path to javac.exe, java.exe ............. 516.6. Review ........................................................ 54

7. Console Application ............................... 557.1. System requirements .................................. 557.2. Create source text file ................................. 557.3. Specify DOS window defaults..................... 55

7.4. Compile J09_Console.java (create J09_Console.class) ............................................ 567.5. Run J09_Console.class............................... 577.6. Review......................................................... 57

8. Debugging .............................................. 598.1. System requirements .................................. 598.2. .................................................................... 598.3. Review......................................................... 59

9. Jar ............................................................ 619.1. System requirements .................................. 619.2. Create class file........................................... 619.3. Create JAR file ............................................ 619.4. Run JAR file via CLASSPATH .................... 629.5. Run JAR file class ....................................... 639.6. Review......................................................... 63

10. Dialog Application ................................ 6510.1. System requirements ................................ 6510.2. Create Dialog class ................................... 6510.3. Review....................................................... 66

11. Applet .................................................... 6711.1. System requirements ................................ 6711.2. Create source text file ............................... 6711.3. Compile ..................................................... 6711.4. Create html file .......................................... 6711.5. Run............................................................ 6711.6. Review....................................................... 68

12. Beans .................................................... 6912.1. System requirements ................................ 6912.2. .................................................................. 6912.3. Review....................................................... 69

13. Ant ......................................................... 7113.1. System requirements ................................ 7113.2. Download Ant ............................................ 7113.3. Install Ant................................................... 7113.4. Compile with Ant ....................................... 7413.5. Review....................................................... 75

14. Development Environments (DE): Down-load / Install ................................................ 77

14.1. System requirements ................................ 7714.2. Forte CE 3.0 (ffj30_ce_ml.exe) ................. 7714.3. Visual Age 3.0 ........................................... 8014.4. JBuilder ..................................................... 8014.5. Review....................................................... 80

15. Console Application (using DE) ......... 8115.1. System requirements ................................ 8115.2. Forte (Sun) ................................................ 8115.3. VisualAge (IBM) ........................................ 8515.4. JBuilder (Borland)...................................... 8515.5. Review....................................................... 85

16. Debugging (using DE) ......................... 8716.1. Forte .......................................................... 8716.2. Visual Age ................................................. 8716.3. JBuilder ..................................................... 8716.4. Review....................................................... 87

17. JAR (using DE) ..................................... 8917.1. System requirements ................................ 8917.2. Forte .......................................................... 8917.3. Visual Age ................................................. 9017.4. JBuilder ..................................................... 9017.5. Review....................................................... 90

18. Dialog Application (using DE) ............. 9318.1. System requirements ................................ 9318.2. Forte .......................................................... 9318.3. Visual Age ................................................. 9618.4. JBuilder ..................................................... 9618.5. Review....................................................... 96

19. Applet (using DE) ................................. 97

Java Getting StartedContents

Page 17: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

Contents

19.1. System requirements ................................ 9719.2. Forte (java.applet.Applet) .......................... 9719.3. Visual Age ................................................. 9919.4. JBuilder ..................................................... 9919.5. Review ...................................................... 99

20. Beans (using DE) ................................10120.1. System requirements .............................. 10120.2. Forte........................................................ 10120.3. Visual Age ............................................... 10420.4. JBuilder ................................................... 10420.5. Review .................................................... 104

21. File .......................................................10521.1. System requirements .............................. 10521.2. ................................................................ 10521.3. Review .................................................... 105

22. XML ......................................................10722.1. System requirements .............................. 10722.2. Create c:\jgs\22\J22_XML.xml ................ 10722.3. Display in IE ............................................ 10722.4. Modify display with XSL .......................... 10822.5. Parse with SAX ....................................... 10922.6. Review .................................................... 112

23. Database (using JDBC) ......................11323.1. System requirements .............................. 11323.2. Create / mount directory.......................... 11323.3. Create database...................................... 11323.4. Create database tables (using Java) ...... 11523.5. Create table data (using Java) ................ 11523.6. Read table data (using Java) .................. 11623.7. Review .................................................... 117

24. Code restriction ..................................11924.1. System requirements .............................. 11924.2. Application restrictions ............................ 11924.3. Applet restrictions.................................... 12524.4. Review .................................................... 129

25. Code signing .......................................13125.1. System requirements .............................. 13125.2. Create code............................................. 13125.3. Generate signed code............................. 13225.4. Test without certificate ............................ 13325.5. Import certificate, create policy file.......... 13425.6. Test with certificate ................................. 13825.7. Review .................................................... 139

26. File encryption ....................................14126.1. System requirements .............................. 14126.2. ................................................................ 14126.3. Review .................................................... 141

Part HUc."Hello to User!" on a CD - - 14327. Install Wireless Toolkit .......................145

27.1. System requirements .............................. 14527.2. Download ................................................ 14527.3. Installation ............................................... 14527.4. Test install: Integrated............................. 14727.5. Review .................................................... 149

28. Handheld: Mobile Phone ....................15128.1. System requirements .............................. 15128.2. Ktoolbar................................................... 15128.3. Forte........................................................ 15428.4. Review .................................................... 157

29. Handheld: PDA ...................................15929.1. System requirements .............................. 15929.2. ................................................................ 15929.3. Review .................................................... 159

30. NexGen ................................................16130.1. System requirements .............................. 16130.2. ................................................................ 16130.3. Review .................................................... 161

31. Install Personal Java Emulation ........16331.1. System requirements .............................. 163

31.2. Download.................................................16331.3. Install wizard............................................16331.4. Review.....................................................163

32. Personal Java ..................................... 16532.1. System requirements...............................16532.2. .................................................................16532.3. Review.....................................................165

33. Install JavaCard .................................. 16733.1. System requirements...............................16733.2. Install javax.comm...................................16733.3. Install JavaCard kit ..................................16733.4. Review.....................................................167

34. Java Card ............................................ 16934.1. System requirements...............................16934.2. Running Sun examples ...........................16934.3. HelloWorld example ................................17334.4. Review.....................................................175

Part HW.“Hello to World!” PC->PC - - 17735. Server -> Client ................................... 179

35.1. System requirements...............................17935.2. Create Server application ........................17935.3. Create Client application .........................18035.4. Test..........................................................18035.5. Review.....................................................180

36. Server -> Client (http) ......................... 18136.1. System requirements...............................18136.2. Create Server application ........................18136.3. Create Client application .........................18236.4. Create HTML file .....................................18336.5. Test..........................................................18336.6. Receive with browser client (IE) ..............18436.7. Analyze request params..........................18436.8. Review.....................................................184

37. Server -> Client (udp) ......................... 18537.1. System requirements...............................18537.2. Create Server application ........................18537.3. Create Client application .........................18537.4. Test..........................................................18637.5. Review.....................................................186

38. Server -> ClientS (multicast) ............. 18738.1. System requirements...............................18738.2. Create Sender application .......................18738.3. Create Client application .........................18738.4. Test..........................................................18738.5. Review.....................................................187

39. Server -> Client (secure (SSL)) ......... 18939.1. System requirements...............................18939.2. Create Server application ........................18939.3. Create Client application .........................18939.4. Sun ClassFileServer demo (works) .........19039.5. Review.....................................................196

40. Commerical Server: Download / Install ..199

40.1. Apache ....................................................19940.2. Tomcat.....................................................20340.3. J2EE........................................................20540.4. iPlanet......................................................208

41. Commercial Server -> Client ............. 21541.1. System requirements...............................21541.2. Simple text...............................................21541.3. http...........................................................21541.4. udp...........................................................21541.5. multicast ..................................................21541.6. secure (SSL)............................................21541.7. Review.....................................................215

42. JavaScript ........................................... 21742.1. System requirements...............................21742.2. Create source text file..............................21742.3. Open in a browser ...................................217

Java Getting Started 13of 501

Contents

Page 18: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

14of 501

Contents

42.4. Review .................................................... 217

43. JSP ....................................................... 21943.1. System requirements .............................. 21943.2. Manually (no Forte)................................. 21943.3. With Forte ............................................... 21943.4. Review .................................................... 220

44. Tags ..................................................... 22344.1. System requirements .............................. 22344.2. Create tag classes .................................. 22344.3. Create tld (tag library description)........... 22544.4. Add tld to web.xml................................... 22644.5. Create jsp page....................................... 22644.6. Test ......................................................... 22744.7. Review .................................................... 228

45. Applet .................................................. 22945.1. System requirements .............................. 22945.2. ................................................................ 22945.3. Review .................................................... 229

46. Servlets ............................................... 23146.1. System requirements .............................. 23146.2. Enable servlets for server ....................... 23146.3. Create servlet.......................................... 23146.4. Add servlet to server ............................... 23146.5. .shtml file................................................. 23246.6. Test ......................................................... 23246.7. Review .................................................... 233

47. File ....................................................... 23547.1. System requirements .............................. 23547.2. ................................................................ 23547.3. Review .................................................... 235

48. XML ...................................................... 23748.1. System requirements .............................. 23748.2. Install xt.jar.............................................. 23748.3. Create AphorismsToHtmlTbl.class (rename Show.class)...................................................... 23748.4. Create aphorismtbl.xsl (rename Show.xsl) ...24048.5. Create Aphorisms.xml............................. 24148.6. Test ......................................................... 24148.7. Review .................................................... 242

49. Database (JDBC) ................................ 24349.1. System requirements .............................. 24349.2. ................................................................ 24349.3. Review .................................................... 243

50. RMI ....................................................... 24550.1. System requirements .............................. 24550.2. Create / mount directory c:\C42 .............. 24550.3. Start RMI................................................. 24550.4. Create server classes ............................. 24550.5. Create C42_RMIServer stubs and skeleton .24650.6. Create C42_RMIPolicy.txt (security policy)...24650.7. Start C42_RMIServer.............................. 24650.8. Create client class C42_RMIClient.class 24650.9. Start C42_RMIClient's (test) ................... 24750.10. Review .................................................. 247

51. IDL ........................................................ 24951.1. System requirements .............................. 24951.2. Create / mount dir ................................... 24951.3. Create CORBA components (Hello.idl)... 24951.4. Create HelloServer ................................. 25451.5. Create HelloClient................................... 25551.6. Start name server ................................... 25551.7. Start HelloServer..................................... 25651.8. Start HelloClient's.................................... 25651.9. Review .................................................... 256

52. EJB ...................................................... 25752.1. System requirements .............................. 257

52.2. Start J2EE Server.................................... 25752.3. Create enterprise bean source files ........ 25752.4. Compile source files ................................ 25852.5. Create Web Client (HelloWorld2.jsp) source file..................................................................... 25852.6. Create application HelloWorld2.ear......... 25952.7. Deploy Application................................... 26152.8. Run Application Client ............................. 26252.9. Run Web Client ....................................... 26252.10. Review................................................... 262

Part L.Language(s) - - - - - - - - - - - - - - 26554. Classes: Basics (define, instantiate, send messages .................................................. 267

54.1. Create and run class ............................... 26754.2. Creating objects and references to them 26754.3. Methods and “messages” (commands)... 271

55. Classes: Extensions (class/methods) ....275

55.1. 833 Create subclass............................... 27555.2. Use methods/constructors of parent class in subclass ........................................................... 27655.3. Create new methods (in own class) ........ 27755.4. Override methods.................................... 27855.5. Overload constructors / methods ............ 279

56. Classes: Location (packages, jars) .. 28156.1. Single class ............................................. 28156.2. Multiple classes ....................................... 286

57. Classes / Methods / Vars : Require / Re-strict .......................................................... 289

57.1. Class ....................................................... 28957.2. Method .................................................... 29157.3. Variables ................................................. 29559.1. Simple flow .............................................. 29959.2. if-elseif-else ............................................. 29959.3. switch-case-default.................................. 30059.4. while ........................................................ 30059.5. do-while ................................................... 30059.6. for ............................................................ 30159.7. break ....................................................... 30259.8. continue................................................... 30359.9. labels ....................................................... 30559.10. return ..................................................... 307

60. Processes / Threads (subprocesses) 30960.1. Show thread info for simple class............ 30960.2. Run a thread............................................ 30960.3. Multiple threads (variable delays)............ 31060.4. Daughter threads..................................... 31160.5. Synchronization between threads ........... 31360.6. wait / notify .............................................. 31660.7. Priority of thread ...................................... 31960.8. Daemons................................................. 320

61. Input / Output ...................................... 32161.1. Keyboard / Screen (Strings) .................... 32161.2. File I/O..................................................... 32261.3. Serialization............................................. 32361.4. Printer...................................................... 32361.5. Network (WWW / TCP/IP / UDP) ............ 328

62. Exceptions .......................................... 33562.1. Unhandled exceptions............................. 33562.2. try / catch................................................. 33562.3. throw........................................................ 33662.4. Own Exceptions ...................................... 336

63. Graphical Interface ............................. 33963.1. Containers ............................................... 33963.2. Components ............................................ 34163.3. Layouts.................................................... 34363.4. Menu ....................................................... 345

64. Events ................................................. 34964.1. “Homemade” events................................ 349

Java Getting StartedContents

Page 19: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

Contents

64.2. Input events............................................. 35164.3. Non-input events ..................................... 35764.4. Dispatchers ............................................. 357

65. Sound ..................................................35965.1. System requirements .............................. 35965.2. Play MIDI................................................. 35965.3. Create (synthesize) MIDI ........................ 361

66. JavaScript ...........................................36567. JSP (Java Server Pages) ....................36768. Collections ..........................................369

68.1. Vector...................................................... 36968.2. Stack ....................................................... 36968.3. Hashtable ................................................ 36968.4. Collection ................................................ 36968.5. List........................................................... 36968.6. Set........................................................... 36968.7. Map ......................................................... 36968.8. Abstract class collections ........................ 36968.9. Iterator..................................................... 369

69. Math / Data types ................................371Part RU.“Reply from User!” on a PC - 373

70. Console Application ...........................37571. Dialog Application ..............................37772. XML ......................................................37973. File .......................................................38174. JDBC ....................................................383

Part RW.“Reply from World!” PC<->PC -385

76. JavaScript ...........................................38777. Applet ..................................................38978. JSP .......................................................39179. Servlet ..................................................393

79.1. System requirements .............................. 39379.2. Create servlet.......................................... 39379.3. Add servlet to server ............................... 39379.4. Create .htm file........................................ 39579.5. Test ......................................................... 39579.6. Review .................................................... 396

80. Beans ...................................................39781. Sessions ..............................................39982. Cookies ................................................41383. XML ......................................................41784. File .......................................................42985. JDBC ....................................................431

Part V.Versions - - - - - - - - - - - - - - - - 43387. Resources ...........................................43588. Languages ...........................................43789. CVS Download / Install .......................43990. CVS versioning ...................................441

Part D.Documentation - - - - - - - - - - - 44391. JavaDoc ...............................................44592. Java2Html ............................................44793. JavaHelp ..............................................449

93.1. System requirements .............................. 44993.2. Download / Install Java Help................... 44993.3. Create help.............................................. 44993.4. Display help............................................. 45193.5. Integrate into application ......................... 45193.6. Review .................................................... 451

Part E.Examples (advanced) - - - - - - - 45394. Java PetStore ......................................45595. Java Management Extension (JMX) ..47596. Java Media Framework (JMF) ............47797. Java 3D ................................................485

Part Z.The last part - - - - - - - - - - - - - - 487

98. Where to go from here ....................... 48999. Trouble-shooting ................................ 491100. FAQ .................................................... 493101. List of figures .................................... 495102. Index .................................................. 501

Java Getting Started 15of 501

Contents

Page 20: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

16of 501

Contents

Java Getting StartedContents

Page 21: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWLUWVDEZ

HHHRR

Part O. OverviewsThis part includes overviews of

•this book•the java profession and language

This part does NOT include any programming. Therefore, if you want to start programming NOW, then skip this part (go immediately to Part HU. "Hello to User!" on a PC (pg. 39)).This part contains the following chapters:1. This book / CD ROM (pg. 19). A description (not TOC) of this book (how it got started, what it pro-vides, conventions, etc.).2. Software professions (pg. 25). Describes what it takes to be a Java professional and what such a professional does.3. Computing platforms / languages (pg. 31). Provides a conceptual and historical overview of the Java language (the Java language itself is introduced in detail in Part L. Language(s) (pg. 265))

Java Getting Started 17of 501

Page 22: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

18of 501

Part O. Overviews

OHUHUcHW

LRURW

VDEZ

Java Getting Started

Page 23: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

O123UUcWLUWVDEZ

Part O. Overviews

HHHRR

1. This book / CD ROM

NOTE: If you want to start immediately programming, then skip to Chapter 4. HTML (pg. 41).This chapter provides a general overview of this book:- 1.1. Core concept. What this book is all about.- 1.2. Style. The general teaching style of the book.- 1.3. Organization. How the book and CD ROM are organized.- 1.4. History (how the book got started). How this book got started. - 1.5. Author. The author’s background.

1.1. Core conceptThe core concept of this book is to a great extent defined by the book title:

•HelloWorld!. This book shows step-by-step how to create the simplest (i.e., “Hello World!”) applica-tions using various Java technologies. •Getting Started. This book is intended for those with little or no programming experience.•for Java. This book explores only Java technology (with a few exceptions).•for Windows. On the Windows (2000) operating system.•a complete workbook. This book is a workbook, in that it shows the reader how to program (with-out pages of text or discussions about the difference between long and short integers). This is acomplete workbook, in that the book and CD (and a Windows 2000 computer) is absolutely all youneed to do the examples.•for intelligent people. The fact that you want to learn Java and that you bought this book to do itshows a great deal of intelligence :) .•who value their time. This book will save you a great deal of time (you will learn Java ASAP).

1.2. StyleThe following describes the general style of this book:- 1.2.1. Teaches by example. Its simply the only way you really learn Java. Explains Java terminology and metaphors with programming, rather than explaining programming with terminology and meta-phors.- 1.2.2. Step-by-step instructions. This book describes every step required.- 1.2.3. Few words as possible. This book avoids unneeded text.- 1.2.4. Emphasis on the tools. - 1.2.5. Emphasis on software reuse. - 1.2.6. Tells you where to go for more info.1.2.1. Teaches by exampleThis book teaches by showing you first how to do an example program, and then explains the con-cepts. This is simply the best way to learn what Java.

Java Getting Started 19of 501

Chapter 1. This book / CD ROMSection 1.1. Core concept

Page 24: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

20of 501

Part O. Overviews

O123

HUHUcHW

LRURW

VDEZ

This book explains Java terminology and OO metaphors with programming examples. Knowing the Java terminology is very important. However, teaching Java with terminology simply doesnt work. Experienced Java programmers (remember, the author is more or less himself a beginner!) like to teach this way, however, because they have simply forgotten how confusing the terminology is (and teaching with the terminology is easier than teaching with examples) and they have forgotten how they themselves learned Java (by doing many examples).The OO metaphors themselves can be quite misleading. For example, the word “inheritance” (one the basic OO concepts) has a wide range of meanings, and what it describes in the context of OO pro-gramming doesnt really match exactly any of the traditional meanings. But once you have created a program that demonstrates “inheritance”, then you can really understand what the word means in the context of OO.1.2.2. Step-by-step instructions

The examples in this book are described step-by-step. This allows you to start programming as quickly as possible and to avoid those “little” problems that can take days to solve.The most important thing for a beginner trying to learn a program-ming language with a book is to avoid simple, but not obvious pro-gramming errors that you yourself can not debug (and thus avoid hours or days of hacking and debugging).

1.2.3. Few words as possibleThis book teaches you Java with as little text as possible. You will not learn Java by reading a lot of text. I have many times myself started reading a technical book loaded with text and with few step-by-step examples, diagrams, or code samples. Such a book is easy for an expert to write, and may be very interesting to read, but really doesn’t offer the begin-ner the info needed to start programming.

This book also treats Java simply as a tool used to get a job done. There is no "philosophy" about Java. The reasons that the various Java mechanisms were created may be an interesting subject for Java experts, but not for (most) beginners. 1.2.4. Emphasis on the tools

This book teaches you how to use various Java tools.Productivity means automation. Not only in a factory, but also in programming. There are a vast number of tools for Java that simplify the programming task. And knowing how to use these tools is just as important as knowing the lan-guage itself.

1.2.5. Emphasis on software reuseThis book emphasizes the reuse of available software.A good programmer never tries to reinvent the wheel, and a good OO language (like Java) makes it easy to reuse code.1.2.6. Tells you where to go for more infoThis book, having introduced you to a Java technology, tells you where to go for more information.After you have completed this book, you will have the skills to start exploring advanced topics and to start "hacking". Thats where things really start to get interesting!

1.3. OrganizationThis section describes the organization of the book and CD ROM.

Java Getting StartedChapter 1. This book / CD ROMSection 1.3. Organization

Page 25: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

O123UUcWLUWVDEZ

Part O. Overviews

HHHRR

1.3.1. BookThe book is organized into parts and chapters.1.3.1.1. PartsThe book is organized into 12 parts. Parts are described in 1. Parts description (pg. 11).The parts that involve programming are not technology-based, but rather based on a specific “platform” and whether or not user input is supported. Platforms include

•A single PC•A single “consumer device” (CD) (such as a mobile phone)•2 PC’s (client / server) connected via internet

Note: The exception to this is Part L. Language(s) (pg. 265), which describes the Java language itself.For example, Part HU. "Hello to User!" on a PC (pg. 39) demonstrates how to write programs using various Java technologies that generate the phrase “Hello User!” on a single local PC. Part RU. “Reply from User!” on a PC (pg. 373) introduces many of the same technologies, but with support for user input.The parts are shown on a page in the outer margin. The current part is highlighted.

Figure 1. Book parts in outer page margins 1.3.1.2. ChaptersEach part contains several chapters. The chapters are described in detail at the beginning of each part.

Figure 2. Description of chapters at beginning of part The chapters are workbook-style, in that the chapters are usually short and introduce a single specific topic. For example, Ch. 11. Applet (pg. 67) introduces applets.

Current part (in red)

Other parts (HU...Z)

Current part (in red)

Description of chapters in part

Java Getting Started 21of 501

Chapter 1. This book / CD ROMSection 1.3. Organization

Page 26: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

22of 501

Part O. Overviews

O123

HUHUcHW

LRURW

VDEZ

The chapters in a part are shown on the page in the outer margin. The current chapter is highlighted.

Figure 3. Book chapters in outer page margins 1.3.2. CD ROMThis book includes a CDROM that contains the following:

•Acrobat Reader (just in case you dont have it installed (it can be downloaded for free))•Winzip81.exe (for unzipping (it can also be downloaded for free)) •Installation files (in directory /jgs/_installers)•Example files (in directories /jgs/04,05...XX)

The following describes the installation and example files in detail.1.3.2.1. Installation files (dir /jgs/_installers)Directory /jgs/_installers contains all required software for completing the examples in this book. Note that all of this software can also be downloaded for free from the internet (how to download is described in the chapter that describes the software installation). The contents of /jgs/_installers will be similar to the following:

Current chapter

All chapters in selected part

Java Getting StartedChapter 1. This book / CD ROMSection 1.3. Organization

Page 27: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

O123UUcWLUWVDEZ

Part O. Overviews

HHHRR

Figure 4. Dirs in /jgs/_installers on CD ROM The name of a file describes:

•The chapter that describes the file download and installation•The application.•The original name of the downloaded file.

For example, the filename08_javasdk__j2sdk-1_4_0-beta3-win.exe

means•The file is downloaded and installed in Chapter 08.•The application is “javasdk” (Java software development kit).•The name of the original downloaded file was j2sdk-1_4_0-beta3-win.exe.

1.3.2.2. Example files (dirs /jgs/04,05...XX)Directory /jgs contains a directory for each chapter in the book (if code is created in the chapter).

Figure 5. Directories in /jgs on CD ROM Directory /jgs/XX contains all of the source and compiled code for Chapter XX.

Figure 6. Contents of CD ROM dir /jgs/XX For example, J59_61_ForSimple.java is the file that you create in Chapter 59 section 6.1.The file XX_code+commands.txt contains the code and commands for chapter XX. For example, 59_code+commands.txt contains the following for chapter 59:

59.1. 59.2.

Java Getting Started 23of 501

Chapter 1. This book / CD ROMSection 1.3. Organization

Page 28: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

24of 501

Part O. Overviews

O123

HUHUcHW

LRURW

VDEZ

public class J59_1_Simple { public static void main(String[] args) { System.out.println("1"); System.out.println("2"); }}59.3. c:\jgs\59>javac -classpath . J59_1_Simple.java59.4. c:\jgs\59>java -classpath . J59_1_Simple...

1.4. History (how the book got started)A friend of mine decided that he wanted to learn how to program. He had basically no programming experience and we lived far apart, so he therefore needed detailed step-by-step directions for creating the simplest program.Therefore, I decided to write a short text document that described step-by-step how to create the simplest program. It quickly became obvious that he did not really want to be a programmer. But the short text document that I started out with had at this point become a 50-page document.

This document described how to create programs with C++. The sheer complexity of creating even the simplest programs in C++ was discouraging, and at about that time I started working as a technical writer for a Java project. So I decided to switch to Java. At first I was planning on writing a rather short book. But as I learned more and more, and what started out as a small project for my spare time became this book.

1.5. AuthorII received my BSEE (Bachelor of Science in Electrical Engineer-ing) in 1986 from Purdue University. I worked for a Navy engineer-ing center (Naval Avionics Center, Indianapolis) for 7 years as a programmer, tech writer and project manager. Back in those days I only used the PC for creating Word documents, and programmed only in Assembler. I then started travelling a bit, working as a tech writer in Russia, Germany and Japan (I speak fluent Russian and German). I am currently a tech writer for a small Swiss computer telephony software company in St. Petersburg, Russia.

I was basically a Java novice when I started writing this book (and I would still not call myself an expert). Strange enough, but I think that my initial lack of experience really helped me to understand the problems that a Java newcomer experiences. Sometimes a beginner knows best how to teach other beginners.I also get a lot of personal satisfaction by taking a complex topic and explaining it in the simplest man-ner possible. After reading this book, I am sure you will agree that I have done exactly that by writing this book about Java.

Java Getting StartedChapter 1. This book / CD ROMSection 1.4. History (how the book got started)

Page 29: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWLUWVDEZ

HHHRR

Part HU. "Hello to User!" on a PC

In this part (perhaps the most important part of the book) you create simple programs that say “Hello to User” on a PC (HtU_PC) (the classic “Hello World” type of applications).This part contains the following chapters:Chapters 4 and 5 are not actually Java, but introduce important info for inexperienced readers

4. HTML (pg. 41). This chapter shows what Java is not: it introduces HTML (and basic file operations for working with text files). Basic knowledge of HTML is necessary since one of the main applications of Java are applets that require an HTML file to be displayed in a browser.5. JavaScript (pg. 47). This chapter also shows what Java is not: it introduces JavaScript. JavaScript is a language that is interpreted by an script engine in the browser. JavaScript is quite often used with Java, and it therefore introduced in this chapter.Chapters 6-7 are the first important steps into Java

6. J2SDK/JRE: Download / Install (pg. 49). J2SDK is the software development kit for Java. It includes all the software required to compile and run Java programs. The JRE (Java runtime engine) is included in the J2SDK, and is the actual program that executes the compiled Java program.7. Console Application (pg. 55). Describes how to create the simplest console application. A console application runs in a DOS window and is much simpler than a graphical application.Chapters 8-12 introduce more important basic topics for the beginning programmer

8. Debugging (pg. 59). Creating the console and graphic applications was not very complicated. How-ever, the biggest part of development of a real application is debugging. This chapter introduces basic debugging techniques using the simple J2SDK tools.9. Jar (pg. 61). A Jar file is actually a zip file containing Java class files. A Jar is a convenient way to package Java class files into a single file and make these class files accessible from the Jar file.10. Dialog Application (pg. 65). Describes how to create the simplest graphical application. A graph-ics application is the typical Windows style application that appears in its own dialog. Such applications are far more complex than a console application; however, using supplied Java classes, the effort and complexity to create your own graphical application is minimized.11. Applet (pg. 67). Shows how to change the basic console and graphics applications (introduced earlier) into applets. Applets are Java applications that can run in a browser, and therefore allow appli-cations to be distributed via internet.12. Beans (pg. 69). Shows how to create the simplest Java “bean”. Beans are basically classes that are wrapped in a zip file, making them easily to import and utilize in other programs.Chapters 13-20 show how to the tedious programming tasks in the previous sections can be automated to some extent (with Ant and development environments)

13. Ant (pg. 71). Ant is a program that makes compilation of java programs much simpler. Ant is included in this book also because many examples that you will find in Internet (including many exam-ples from Sun) use Ant. Also, you need a goo explanation of how to install and use Ant, because such an explanation will not be found at the Ant website!

Java Getting Started 39of 501

Page 30: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

40of 501

Part HU. "Hello to User!" on PC

OHUHUcHW

LRURW

VDEZ

14. Development Environments (DE): Download / Install (pg. 77). Forte is a development environ-ment from Sun. It is basically a big application that simplifies the rather complicated tasks involved when creating real-world applications.15. Console Application (using DE) (pg. 81). Shows how to create the simplest console application using Forte. Introduces much of the basic functionality of Forte.16. Debugging (using DE) (pg. 87). Shows how to debug a simple console program using the Forte debugger. The Forte debugger is far more advanced than the simple J2SDK debugger and allows you find problems much more easily.Note: At this point there is no chapter about Ant. Basically, Forte takes care of the complicated class-path, etc. tasks, and therefore Ant is not required when using Forte.17. JAR (using DE) (pg. 89). Shows how Jar files can be used to easily integrate Java classes into the Forte development environment.18. Dialog Application (using DE) (pg. 93). Shows how to create the simplest window application with Forte.19. Applet (using DE) (pg. 97). Shows how to create the simplest applet with Forte.20. Beans (using DE) (pg. 101). Shows how to create and utilize in Forte the simplest Java “bean”. Beans are basically classes that are wrapped in a zip file, making them easily to import and utilize in other programs.Chapters 21-23 introduce persistence (stored on the hard disk)

21. File (pg. 105). Shows how to read information from a file and display.22. XML (pg. 107). Shows how to create applications that can create and process XML. XML is similar to HTML, and is a standardized way of sending any type of information as tagged lines in a simple text file.23. Database (using JDBC) (pg. 113). Shows how to create a database in a file and then read the data from the database.Chapters 24-26 introduce security topics

24. Code restriction (pg. 119). Shows how Java code access to your computer can be restricted (and thus to some extent ensure that a destructive program loaded over the internet or copied to your com-puter will not destroy files on your computer).25. Code signing (pg. 131). Shows how Java code can be digitally signed to ensure who the code came from.26. File encryption (pg. 141). Shows how to encrypt information in a file and then decrypt and display. Encryption of information is vital to secure information exchange.

Java Getting Started

Page 31: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OU456789

1011121314151617181920212223242526UcWLUWVDEZ

Part HU. "Hello to User!" on PC

H

HHRR

4. HTML In this chapter you will do examples that dem-onstrate the basics of working with text and HTML files. HTML is not Java, and is not even a program-ming language. It is simply a standard way of including a description of the contents of an HTML page that standard browsers under-stand.However it is important for various reasons to understand how to work with text files and

how HTML works, since (for example) Java source files are text files and web pages are sent in HTML format.

4.1. System requirements (none)There are no special system requirements for this chapter. Notepad and Internet Explorer (IE) are required, but are normally included with Win 2000.

4.2. Create working folders for chapterIn this section you learn how to create folders.4.1. Click on the desktop icon My Computer.4.2. Click on Local Disk (C:). The list of directories on disk C: appears.4.3. In a blank area of the list of directories: Right-click. A context menu appears.4.4. From context menu: Select New / Folder.

Figure 8. Context dir for creating a new folder (directory) At the end of the file list you will see a new folder named New Folder. The name is highlighted in blue and is ready to be changed by simply typing the new name on the keyboard.

Figure 9. New dir name is highlighted and ready to be changed NOTE: You can normally change the dir name by simply clicking twice slowly on the dir, typing in the new name and then pressing ENTER.4.5. Type jgs (= “Java Getting Started”).

Java Getting Started 41of 501

Chapter 4. HTMLSection 4.1. System requirements (none)

Page 32: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

42of 501

Part HU. "Hello to User!" on PC

OHU

4567891011121314151617181920212223242526

HUcHW

LRURW

VDEZ

4.6. Click ENTER. The folder named is changed.

Figure 10. Dir name is changed 4.7. Create dir C:\jgs\04. “04” stands for chapter 04. This is the folder in which you will createall files for this chapter.

4.3. Text file basicsIn this section you learn the basics of working with text files. 4.3.1. Create text file c:\jgs\04\J04_TEXT4.8. In folder c:\jgs\04: Right-click. A context menu appears.4.9. From the context menu: Select New / Text document.

Figure 11. Context menu for creating a new textfile At the end of the file list you will see a file named New Text Document.txt. The name is highlighted in blue and is ready to be changed by simply typing the new name.

Figure 12. New file name is highlighted and ready to be changed NOTE: You can change the filename by simply clicking twice slowly on the filename, entering the new name and pressing ENTER (similar to changing a folder name).4.10. Type J04_TEXT (change the filename to J04_TEXT).4.11. Click ENTER. A message appears warning that the file may be unusable:

Figure 13. Warning that a file may not be usable Why does this message appear? The type of a file is determined by the extension. For example, “txt” is the extension of filename “filename.txt”, and typically is interpreted by the operating system (OS) as the extension for a text file.

Java Getting StartedChapter 4. HTMLSection 4.3. Text file basics

Page 33: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OU456789

1011121314151617181920212223242526UcWLUWVDEZ

Part HU. "Hello to User!" on PC

H

HHRR

Therefore, when a file has no extension (as in the case of the file you just renamed), the OS cant deter-mine the file type. And therefore when you double-click on the file, the OS doesnt know what applica-tion to open the file with.4.12. Click Yes. The file is renamed.Note the icon for the file. Normally, when a file has a type, the OS displays the icon for that file type in front of the file name. The icon comes from the application that is registered with the OS for processing the file type. If the file has no extension, then the OS icon for such files is displayed.4.3.2. Open J04_TEXT with notepadIf the OS doesn’t know what application to open a file with, then you will be asked to specify the appli-cation.4.13. Double-click on J04_TEXT. A dialog appears asking which application to open the filewith.

Figure 14. Dialog for specifying open application Note that the checkbox “Always use this program to open these files” is greyed out. This is because “these files” refers to the file type, and the file has no extension.4.14. Scroll down the list and double-click on Notepad. The file is opened in Notepad.4.3.3. Add text and save as J04_TEXT.txt4.15. Add the following text to the file:

Hello World! (c:\jgs\04\J04_TEXT.txt)

Figure 15. Text “Hello World!” added to file 4.16. Save the file as (menu item File / Save as...) J04_TEXT.txt (with an extension).The file now has the extension txt which (by default) indicates that it is a text file.4.17. Close the file.4.18. Double-click on the file. The file is opened in Notepad (assuming Notepad is registeredwith the OS for txt files).4.3.4. Register Notepad for .txt filesSometimes when you install a certain application, the application will register itself with the OS (per-haps after having asked you during installation if it should do so) for handling a certain file type. For example, if you install a new browser, the browser will typically ask if it should be the default application for opening files with the extension htm and html.This section shows you yourself can register an application for a file type manually.4.19. Right-click on J04_TEXT.txt.4.20. Select Open With.... The Open With dialog appears as before. Note, however, that the

Java Getting Started 43of 501

Chapter 4. HTMLSection 4.3. Text file basics

Page 34: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

44of 501

Part HU. "Hello to User!" on PC

OHU

4567891011121314151617181920212223242526

HUcHW

LRURW

VDEZ

checkbox “Always use this program to open these files” is not greyed out.

Figure 16. Checkbox “Always use this program to open these files” enabled 4.21. Check the checkbox “Always use this program to open these files”.4.22. Double-click on Notepad. The file is opened in Notepad.If you now double-click on any .txt file, the file will be automatically opened in Notepad.4.3.5. Open J04_TEXT.txt with IEOpening a file with a different application is the same as the steps above, except that you dont check the checkbox.4.23. Right-click on J04_TEXT.txt.4.24. Select Open With / Choose Program.4.25. Double-click on Internet Explorer. The file is opened in IE.

Figure 17. Text file opened in IE You have just opened a text file in IE, although IE is normally used to open .html files. However, IE is designed to also open text files (actually, an html file is simply a text file that contains special HTML markers; you will see this in the next section).

4.4. HTML basicsYou will now create an HTML file.4.4.1. Save file via IE as J04_TEXT.htmIf you save the text file as an HTML file using IE, then IE will add the HTML markers automatically to the file.4.26. From IE: Select File / Save as.... The dialog “Save Web Page” appears.4.27. Select directory c:\jgs\04.4.28. For “Save as type”: Select Web Page, HTML only (*.htm, *.html).

Figure 18. Dialog Save Web Page

Java Getting StartedChapter 4. HTMLSection 4.4. HTML basics

Page 35: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OU456789

1011121314151617181920212223242526UcWLUWVDEZ

Part HU. "Hello to User!" on PC

H

HHRR

Note: Normally in Windows a file has a 3-letter extension, such as “htm”. However, HTML files some-times have the extension “html”. Be careful about this while doing examples from this book.4.29. For filename: Enter J04_HTML.htm.4.30. Click Save. The file is now saved as J04_HTML.htm.4.4.2. View source of J04_HTML.htm4.31. Double-click on J04_HTML.htm. J04_HTML.htm is opened in IE.4.32. Right-click on the text area of J04_HTML.htm. A context menu appears.4.33. Select View Source.

Figure 19. Source of J04_HTML.htm Note the extra text that IE added when you saved the file as an HTML file. This extra text is the HTML markers that tell IE how to display the file contents.4.4.3. Modify file contentThe contents of J04_HTML.htm (modified by IE above) are somewhat complicated. You will now change the file contents to something simpler.4.34. Replace the text (using notepad) in J04_HTML.htm with the following text.

<! c:\jgs\04\J04_HTML.htm ><html><title>Hello World title</title><head>Hello World header</head><body><h1>Hello World heading 1</h1><p>Hello World text (c:\jgs\04\J04_HTML.htm)</p></body></html>

Figure 20. Modified J04_TEXT.htm 4.35. Save the file.

Java Getting Started 45of 501

Chapter 4. HTMLSection 4.4. HTML basics

Page 36: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

46of 501

Part HU. "Hello to User!" on PC

OHU

4567891011121314151617181920212223242526

HUcHW

LRURW

VDEZ

4.36. In IE: Click on the button Refresh .

Figure 21. Modified J04_HTML.htm in IE Note how the various items in the html file are displayed.HTML is called a “markup language”, because it is used to “markup” the text (and pics) to be displayed in a browser. The markers are enclosed in brackets “<>”. For example, “<h1>” indicates that the marked text is heading 1 text, and “</h1>” specifies the end of the marker. The browser settings deter-mine how heading 1 text will be displayed.4.4.4. Manually change view formatYou will now change the browser settings for displaying marked text.4.37. Select View / Text Size / Smallest. The marked text is now displayed in small letters.

Figure 22. Html file viewed in smallest text size 4.4.5. Specify format more exactlyYou can also specify the format for displaying marked text within the actual HTML file itself.4.38. In J04_HTML.htm: Change the line:

<p>Hello World text</p>to

<p><font face="Courier" size="3" color="#FF0000"><strong><em><u>Hello World text.</u></em></strong></font></p>

4.39. Refresh the view in IE.

Figure 23. HTML file with specific formatting Note now that the the marked text is displayed as specified in the markers.

4.5. ReviewIn this chapter you learned how to work with text files and html files. HTML is not a programming lan-guage, but rather a markup language for marking the contents of an HTML file that should be displayed in a browser.

Java Getting StartedChapter 4. HTMLSection 4.5. Review

Page 37: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OU456789

1011121314151617181920212223242526UcWLUWVDEZ

Part HU. "Hello to User!" on PC

H

HHRR

5. JavaScript In this chapter you will do examples that demonstrate JavaScript. JavaScript is not Java. It is an interpreted language, which means that the source text file is not compiled, but rather interpreted (by a JavaScript engine in the browser).The advantage of JavaScript is that no compilation required. You can easily create functionality in an HTML page by just adding the JavaScript commands. The big disadvantage (at least from the perspective of the unex-perienced programmer) is that JavaScript can be diffi-cult to debug.

Its important to understand what JavaScript is since JavaScript is included quite often in HTML pages to add limited functionality quickly and easily.

5.1. System requirements (none)There are no special system requirements because IE can interpret JavaScript.

5.2. Create source text fileNote: Some strange problems can occur with JavaScript with certain characters. Something was wrong with the type of quotation marks I was using in a file. I changed “language” to “abc” and it worked!5.1. Create dir c:\jgs\05.5.2. Create text file J05_JScript.htm with following content.

<! c:\jgs\05\J05_JScript.htm ><HTML><BODY><SCRIPT LANGUAGE=”JavaScript”>document.write(“<h2>Hello World! (c:\\jgs\\05\\J05_JScript.htm)</h2>”);</SCRIPT></BODY></HTML>

5.3. Open in a browser5.3. Open in IE J05_JScript.htm:

Figure 24. JavaScript file displayed in IE

5.4. ReviewThis is what is known as client-side JavaScript, since the JavaScript is sent to the client and interpreted there. Later in this book ....

Java Getting Started 47of 501

Chapter 5. JavaScriptSection 5.1. System requirements (none)

Page 38: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

48of 501

Part HU. "Hello to User!" on PC

OHU

4567891011121314151617181920212223242526

HUcHW

LRURW

VDEZ

Java Getting StartedChapter 5. JavaScriptSection 5.4. Review

Page 39: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OU456789

1011121314151617181920212223242526UcWLUWVDEZ

Part HU. "Hello to User!" on PC

H

HHRR

6. J2SDK/JRE: Download / Install2 things are done in this section:- j2sdk is installed. this includes 2 important things - the compiler for creating a byte code program from a text file. - the class libraries required.- jre is installed. jre actually runs the byte code (is in a way a bridge between the byte code and the

operating system... jre required for the particular operating system).

6.1. System requirementsnote: if JRE already installed on computer, you can install another one. you need to note where the newly installed jre is, because in the future a program may find all jre’s and ask you which one you want. if you use the wrong version of a jre, then the results may not be exactly what you want.

6.2. Download J2SDKNote: The downloaded file is included on the CDROM as \jgs\_installers\08_javasdk__j2sdk-1_4_0-beta3-win.exe.6.1. Open http://java.sun.com.6.2. Select Products and APIs.6.3. Select Java 2 Platform, Standard Edition (J2SE).6.4. Click on Java 2 SDK, Standard Edition, V 1.4 (SDK).6.5. Select Microsoft Windows.6.6. Click continue.6.7. Click Accept.6.8. Click HTTP download.6.9. Select Save this file to disk.6.10. In the Saveas dialog: Save the file (j2sdk-1_4_0-beta3-win.exe) to c:\jgs\installers.6.11. Save to your hard disk.6.12. When message Download completed appears: Click on the button Open Folder.6.3. Install6.13. Double-click on c:\jgs\installers\j2sdk-1_4_0-beta3-win.exe (08_javasdk__j2sdk-1_4_0-beta3-win.exe). The files are unpacked.

Figure 25. Files unpacked

Java Getting Started 49of 501

Chapter 6. J2SDK/JRE: Download / InstallSection 6.1. System requirements

Page 40: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

50of 501

Part HU. "Hello to User!" on PC

OHU

4567891011121314151617181920212223242526

HUcHW

LRURW

VDEZ

The Welcome dialog appears:

Figure 26. Welcome dialog 6.14. Click Next. The Software License Agreement appears:

Figure 27. License agreement dialog 6.15. Click Yes. The “Choose Destination location” dialog appears.6.16. Click Browse. The “Choose Folder” dialog appears.6.17. Set folder to C:\jgs\08_j2sdk:

Figure 28. Folder setting 6.18. Click OK. 6.19. Click Next. The dialog “Select components” appears. All are selected. Leave allselected:

Figure 29. All J2SDK components selected 6.20. Click Next. The dialog “Select Browsers” appears:

Java Getting StartedChapter 6. J2SDK/JRE: Download / InstallSection 6.3. Install

Page 41: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OU456789

1011121314151617181920212223242526UcWLUWVDEZ

Part HU. "Hello to User!" on PC

H

HHRR

Figure 30. Select browsers 6.21. Click Next. The files are copied.

Figure 31. Files are copied The JRE is setup.

Figure 32. JRE is setup Finally the “Setup Complete” dialog appears.

Figure 33. Setup complete 6.22. Click Finish.

6.4. View the installed files6.4.1. Start menu6.23. From the Start Menu: Select Programs / Java 2 Runtime Environment / Welcome toJava 2. This opens a web page with some reference information.So where is the SDK? The J2SDK does not provide a graphical development environment. Therefore there is no entry in the Start Menu.6.4.2. Dir C:\jgs\08_j2sdk\bin6.24. Open the dir C:\jgs\08_j2sdk\bin. Note the following files:

•javac.exe: The java compiler.•java.exe: The exec file for running a java application (interpreter).

There are many other files in this dir. They are mainly tools. Some will be introduced in later chapters.6.4.3. Dir C:\jgs\08_j2sdk\jre\bin6.25. Open dir C:\jgs\08_j2sdk\jre\bin. Note the following files:

•java.exe: The java virtual machine.6.5. Set the path to javac.exe, java.exe6.5.1. Attempt to start the java.exe runtime engine6.26. From the Start menu select Programs / Accessories / Command Prompt. A DOS win-

Java Getting Started 51of 501

Chapter 6. J2SDK/JRE: Download / InstallSection 6.4. View the installed files

Page 42: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

52of 501

Part HU. "Hello to User!" on PC

OHU

4567891011121314151617181920212223242526

HUcHW

LRURW

VDEZ

dow appears.

Figure 34. Opened DOS window 6.27. In the DOS window execute:

javaThe java.exe file is NOT found by the OS:

Figure 35. java.exe file not found Do NOT close the DOS window.6.5.2. Set path to java toolsYou will now set the path so that when you try to execute the command java.exe, the OS will know where to look for the file.6.28. Select Settings / Control Panel. The Control Panel appears.

Figure 36. Setup complete 6.29. Double-click on System. The dialog “System Properties” appears.6.30. Click on Advanced.

Figure 37. Tab Advanced

Java Getting StartedChapter 6. J2SDK/JRE: Download / InstallSection 6.5. Set the path to javac.exe, java.exe

Page 43: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OU456789

1011121314151617181920212223242526UcWLUWVDEZ

Part HU. "Hello to User!" on PC

H

HHRR

6.31. Click on Environment Variables.

Figure 38. Environment variables 6.32. Double-click on Path. The dialog “Edit System Variable” appears.6.33. Add the following to the beginning of Value of the variable: ”C:\jgs\08_j2sdk\bin;”.

Figure 39. Variable value set to path 6.34. Click OK. The dialog closes.6.35. Click OK. The dialog closes.6.36. Click OK. The dialog closes.

6.5.3. Start the javac.exe compilerjava.exe must be executed in a DOS window, because java.exe does not have the functionality to be double-clicked and run in a dialog manner.6.37. In the already opened DOS window execute:

javaNote that the file is still not found by the OS:

Figure 40. java.exe file is still not found IMPORTANT: If you open a DOS window and then change system variables, you must open a new DOS window for the changes to take effect (the changes do not take effect for the old DOS window).6.38. Close the DOS window.6.39. Open a new DOS window.6.40. In the DOS window execute:

java

Java Getting Started 53of 501

Chapter 6. J2SDK/JRE: Download / InstallSection 6.5. Set the path to javac.exe, java.exe

Page 44: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

54of 501

Part HU. "Hello to User!" on PC

OHU

4567891011121314151617181920212223242526

HUcHW

LRURW

VDEZ

The java.exe file is found by the OS:

Figure 41. java.exe file found

6.6. Review

Java Getting StartedChapter 6. J2SDK/JRE: Download / InstallSection 6.6. Review

Page 45: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OU456789

1011121314151617181920212223242526UcWLUWVDEZ

Part HU. "Hello to User!" on PC

H

HHRR

7. Console ApplicationThis is the chapter where you finally start programming Java! You will create a very simple program.

7.1. System requirementsj2sdk installed (see Ch. 6. J2SDK/JRE: Download / Install (pg. 49)).

7.2. Create source text file7.1. Create dir C:\jgs\09.7.2. Create text file C:\jgs\09\J09_Console.java.The file is a text file, but the extension is .java which indicates that it contains source code for a java program.7.3. Open the file with Notepad (and specify that this kind of file is always opened with Note-pad).7.4. Enter the following text in the file:

// c:\jgs\09\J09_Console.javaclass J09_Console { public static void main(String[] args) { System.out.println("Hello World! (c:\\jgs\\09\\J09_Console.java)"); }}

Figure 42. J09_Console.java 7.5. Save the file.

7.3. Specify DOS window defaultsDefaults can be specified for the DOS window that free you from having to enter a lot of info each time you open a DOS window.7.6. Display (do not click the mouse) from the Start menu Programs / Accessories / Com-mand Prompt.

Java Getting Started 55of 501

Chapter 7. Console ApplicationSection 7.1. System requirements

Page 46: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

56of 501

Part HU. "Hello to User!" on PC

OHU

4567891011121314151617181920212223242526

HUcHW

LRURW

VDEZ

7.7. Right-click on Command Prompt. A context menu appears.

Figure 43. Opening DOS window defaults dialog 7.8. Click on Properties. The dialog “Properties of DOS window” appears.

Figure 44. DOS window properties dialog 7.9. Change “Execute in” to

c:\jgs\097.10. Click in the textfield for “Key combination”.7.11. Click on the following keys at the same time: CTRL, ALT, Z. This key combination willnow open the DOS window.

Figure 45. DOS window settings 7.12. Click OK.

7.4. Compile J09_Console.java (create J09_Console.class)7.13. Click CTRL-ALT-Z. A DOS window appears.7.14. Execute

Java Getting StartedChapter 7. Console ApplicationSection 7.4. Compile J09_Console.java (create J09_Console.class)

Page 47: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OU456789

1011121314151617181920212223242526UcWLUWVDEZ

Part HU. "Hello to User!" on PC

H

HHRR

javac J09_Console.java

Figure 46. Compiling J09_Console.java Note that the file c:\jgs\09\J09_Console.class was created.

Figure 47. J09_Console.class

7.5. Run J09_Console.class7.15. In the DOS window: Click the up-arrow key. Note that the previous command appears.In general, use the up and down arrow keys to recall previous commands.7.16. A the command prompt: Execute:

java J09_ConsoleNOTE: The class (J09_Console) is executed, not file J09_Console.class.The text "Hello World!" is displayed.

Figure 48. J09_Console.class executed

7.6. Review

Java Getting Started 57of 501

Chapter 7. Console ApplicationSection 7.5. Run J09_Console.class

Page 48: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

58of 501

Part HU. "Hello to User!" on PC

OHU

4567891011121314151617181920212223242526

HUcHW

LRURW

VDEZ

Java Getting StartedChapter 7. Console ApplicationSection 7.6. Review

Page 49: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OU456789

1011121314151617181920212223242526UcWLUWVDEZ

Part HU. "Hello to User!" on PC

H

HHRR

15. Console Application (using DE)In Ch. 7. Console Application (pg. 55) you created a very simple console application.In this chapter you will repeat that, except using Forte. This will be much more complicated (and not necessary for such a simple program as the console application), but will be a good introduction to Forte.

15.1. System requirementsForte installed (Ch. 14. Development Environments (DE): Download / Install (pg. 77)).

15.2. Forte (Sun)15.2.1. Open Forte CE15.1. From the Start Menu: Select Programs / Forte for Java CE / Forte for Java CE. A dia-log appears requesting the directory for individual files.15.2. Enter C:\jgs\15_myForteFiles.

Figure 81. Dir for my individual files 15.3. Click OK. The dialog “Settings import wizard” appears.

Figure 82. Settings import wizard 15.4. Check No, skip the settings import (should already be checked).

Java Getting Started 81of 501

Chapter 15. Console Application (using DE)Section 15.1. System requirements

Page 50: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

82of 501

Part HU. "Hello to User!" on PC

OHU

4567891011121314151617181920212223242526

HUcHW

LRURW

VDEZ

15.5. Click Finish. The dialog “iPlanet setup” appears.

Figure 83. iPlanet setup dialog 15.6. Click Cancel. The “Setup wizard” dialog “General Forte for Java Settings” appears.15.7. For Web Browser: Select External Browser (Windows).

Figure 84. Settings import wizard 15.8. Click Finish. The following Forte dialogs appear:

•A main window at top with toolbars

Figure 85. Main window •Explorer

Figure 86. Explorer •Properties dialog

Figure 87. Properties dialog •Welcome dialog.

15.9. Close the welcome dialog.

Java Getting StartedChapter 15. Console Application (using DE)Section 15.2. Forte (Sun)

Page 51: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OU456789

1011121314151617181920212223242526UcWLUWVDEZ

Part HU. "Hello to User!" on PC

H

HHRR

Note the contents of C:\jgs\15_myForteFiles.

Figure 88. Contents of C:\jgs\15_myForteFiles 15.2.2. Create / mount directory c:\jgs\1515.10. Create dir c:\jgs\15.15.11. In the Explorer: Right-click on Filesystems. A context directory appears.15.12. Select Mount Directory. The “Mount Filesystem” dialog appears.15.13. Click (not double-click) on c:\jgs\15.

Figure 89. Select c:\jgs\15 as the directory to mount 15.14. Click Mount. The dir is now mounted.

Figure 90. c:\jgs\15 added to filesystem 15.2.3. Create code15.2.3.1. Create main15.15. In Explorer: In tab Filesystems: On c:\jgs\15: Right-click.15.16. Select New / Classes / Main. The “New from Template New Object Name” dialogappears.

Java Getting Started 83of 501

Chapter 15. Console Application (using DE)Section 15.2. Forte (Sun)

Page 52: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

84of 501

Part HU. "Hello to User!" on PC

OHU

4567891011121314151617181920212223242526

HUcHW

LRURW

VDEZ

15.17. For Name: Enter J15_Console.

Figure 91. New object J15_Console 15.18. Click Finish. The following is created:

Figure 92. New object code, properties •In the Source Editor. The code created automatically for the new class:/* * J15_Console.java * * Created on 11. Mai 2002, 14:42 */

/** * * @author tt * @version */public class J15_Console {

/** Creates new J15_Console */ public J15_Console() { }

/** * @param args the command line arguments */ public static void main (String args[]) { }

}•In the Explorer: The constructors and methods of the new class.•In the Properties window: The properties of the new class.

Java Getting StartedChapter 15. Console Application (using DE)Section 15.2. Forte (Sun)

Page 53: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OU456789

1011121314151617181920212223242526UcWLUWVDEZ

Part HU. "Hello to User!" on PC

H

HHRR

Note that in dir c:\jgs\15 the following files were created:•J15_Console.java•J15_Console.java~•.nbattrs

15.2.3.2. Add code15.19. In the Source Editor: Change the main routine as shown:

public static void main (String args[]) { System.out.print("Hello World! (c:\\jgs\\15\\J15_Console.class)");}

15.2.4. Compile / execute15.20. In the Source Editor: Right-click. A context menu appears.15.21. Click Execute. The following occurs:

•c:\jgs\15\J15_Console.class is created.•The Output window appears with the output:

Figure 93. Output window •The Execution window appears:

Figure 94. Execution dialog •The tab Running is selected in the main dialog:

Figure 95. Tab Running is selected in the main dialog 15.2.5. Display different windows15.22. Select the tab Editting in the main dialog. Note that the Output dialog disappears andthat the Explorer, Source Editor, and Properties Window appear. Clicking on tab Running willreverse this.Note: In general it takes a while to get used to the Forte windows.

15.3. VisualAge (IBM)15.4. JBuilder (Borland)15.5. Review

Java Getting Started 85of 501

Chapter 15. Console Application (using DE)Section 15.3. VisualAge (IBM)

Page 54: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

86of 501

Part HU. "Hello to User!" on PC

OHU

4567891011121314151617181920212223242526

HUcHW

LRURW

VDEZ

Java Getting StartedChapter 15. Console Application (using DE)Section 15.5. Review

Page 55: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWLUWVDEZ

HHHRR

Part HUc. "Hello to User!" on a CD

In Part HU. "Hello to User!" on a PC (pg. 39), you created Java programs that ran on a PC and gener-ated the phrase “Hello User”. However, a Java application can theoretically run on any device that has a JRE for that device.In this part you create simple programs that say “Hello to User” on a consumer device (CD). A “con-sumer device” is a term that includes non-PC devices that have a JRE and can therefore run a Java program. Such devices include mobile phones, PDA’s (personal digital assistant), and smartcards.However, since you probably dont have a CD, and in any case developing software on a CD is not pos-sible, you will use a PC to

•develop the applications•test the applications (using an emulator that provides a “virtual” CD for testing).

This part contains the following chapters:Chapters 27-30 introduce Java on mobile consumer devices

27. Install Wireless Toolkit (pg. 145). You install the wireless toolkit. This toolkit provides the SDK and the JRE for the CD platform.28. Handheld: Mobile Phone (pg. 151). You create a simple “Hello User!” application for a mobile phone and “test” this application using a mobile phone emulator on the PC.29. Handheld: PDA (pg. 159). You create a simple “Hello User!” application for a mobile phone and “test” this application using a mobile phone emulator on the PC.30. NexGen (pg. 161). You create a simple “Hello User!” application for a next-generation device and “test” this application using a mobile phone emulator on the PC.Chapters 31-32 introduce Personal Java

31. Install Personal Java Emulation (pg. 163). You install the Personal Java emulation tool.32. Personal Java (pg. 165). You create a simple “Hello User!” application for personal Java.Chapters 33-34 introduce Smartcards

33. Install JavaCard (pg. 167). You install the JavaCard toolkit. The JavaCard toolkit provides the SDK and the JRE for application running on a smart card.34. Java Card (pg. 169). You create and test (with an emulator) a simple “Hello World!” application for a smart card.

Java Getting Started 143of 501

Page 56: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

144of 501

Part HUc. "Hello to User!" on CD

OHUHUcHW

LRURW

VDEZ

Java Getting Started

Page 57: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUc2728293031323334WLUWVDEZ

Part HUc. "Hello to User!" on CD

HH

HRR

27. Install Wireless Toolkit27.1. System requirements27.2. DownloadNote: The downloaded file is included on the CDROM as \jgs\_installers\27_j2me__wireless_toolkit-1_0_3-win.exe.27.1. Open http://java.sun.com.27.2. Select Products and APIs.27.3. Select Java2 Platform, MicroEdition.27.4. Select J2ME Wireless Toolkit.27.5. Under Download J2ME, Wireless Toolkit 1.0.3: Click Continue. License agreement.27.6. Click Accept.27.7. Click HTTP download.27.8. Select Save this file to disk.27.9. In the Saveas dialog: Save the file (j2me_wireless_toolkit-1_0_3-win.exe) to the appro-priate directory. 27.10. When message Download completed: Click on the button Open Folder.27.3. Installation27.3.1. Standalone27.11. Double-click. The files are unpacked. The Welcome dialog appears:

Figure 163. Welcome dialog 27.12. Click Next. The License Agreement appears.

Figure 164. License Agreement 27.13. Click Yes. The installer searches for a Java Machine. After finding a machine, it will belisted.

Figure 165. Java Virtual Machine found

Java Getting Started 145of 501

Chapter 27. Install Wireless ToolkitSection 27.1. System requirements

Page 58: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

146of 501

Part HUc. "Hello to User!" on CD

OHUHUc

2728293031323334

HWL

RURW

VDEZ

27.14. Click Next. The directory for installation appears.

Figure 166. Directory for installation 27.15. Click Next. The dialog Setup Type appears.

Figure 167. Setup type dialog 27.16. Select Standalone.27.17. Select Next. The program folder dialog appears.

Figure 168. Program folder 27.18. Click Next. The dialog Start Copying files appears.

Figure 169. Start copying files dialog 27.19. Click Next. The files are copied. The dialog Install Wizard complete appears.

Figure 170. InstallShield Wizard complete dialog 27.20. Click Finish.27.3.2. Integrated

Java Getting StartedChapter 27. Install Wireless ToolkitSection 27.3. Installation

Page 59: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUc2728293031323334WLUWVDEZ

Part HUc. "Hello to User!" on CD

HH

HRR

Only those dialogs that differ from the previous dialogs are shown.27.21. Double-click. The files are unpacked. The Welcome dialog appears.27.22. Click Next. The License Agreement appears.27.23. Click Yes. The installer searches for a Java Machine. After finding a machine, it will belisted.27.24. Click Next. The directory for installation appears.27.25. Click Next. The dialog Setup Type appears.Select Integrated.

Figure 171. Integrated setup 27.26. Select Next. The Forte for Java location dialog appears.

Figure 172. Forte4j location 27.27. Select Next. The program folder dialog appears.27.28. Click Next. The dialog Start Copying files appears.

Figure 173. Start copying files dialog 27.29. Click Next. The files are copied. The dialog Install Wizard complete appears.27.30. Click Finish.

27.4. Test install: Integrated

Java Getting Started 147of 501

Chapter 27. Install Wireless ToolkitSection 27.4. Test install: Integrated

Page 60: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

148of 501

Part HUc. "Hello to User!" on CD

OHUHUc

2728293031323334

HWL

RURW

VDEZ

27.31. Start Forte. Note the directory that has been mounted.

Figure 174. Forte4j wireless examples 27.32. Right-click on Demos. Context dialog appears.27.33. Right-click on Execute. An emulation phone appears.

Figure 175. Emulation phone 27.34. Click on the Launch button to start.

Figure 176. Launch button The application is launched.

Figure 177. Application is launched 27.35. Click on Exit to exit application.

Java Getting StartedChapter 27. Install Wireless ToolkitSection 27.4. Test install: Integrated

Page 61: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUc2728293031323334WLUWVDEZ

Part HUc. "Hello to User!" on CD

HH

HRR

27.36. Click on the down/up buttons to select different applications.

Figure 178. Selecting different apps

27.5. Review

Java Getting Started 149of 501

Chapter 27. Install Wireless ToolkitSection 27.5. Review

Page 62: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

150of 501

Part HUc. "Hello to User!" on CD

OHUHUc

2728293031323334

HWL

RURW

VDEZ

Java Getting StartedChapter 27. Install Wireless ToolkitSection 27.5. Review

Page 63: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWLUWVDEZ

HHHRR

Part HW. “Hello to World!” PC->PC

In Part HU. "Hello to User!" on a PC (pg. 39) you created PC applications that generated the phrase “Hello User”. In this part you create a server PC application that sends the phrase “Hello World” via the internet to a client PC application (that you also create).Thus, the applications in this section can send the greeting “Hello” via internet to the entire world (not just to the user of the PC the application is running on). Therefore this part is called “Hello to World” PC->PC. Note, however, that you do not require 2 PC’s connected via internet to test the applications in this part. You can simply run both the server and the client on the same computer (things would work the same way if the server and client were both connected to internet).This part includes the following chapters:Chapters 35-39 introduce the basic types of client-server communication

35. Server -> Client (pg. 179). You create the simplest server and client applications that can exchange message via internet. Does not require a server application or a browser. Demonstrates how the Java class library supplies everything required to create such programs.36. Server -> Client (http) (pg. 181). You create client and server applications that transfer data (to the client) using the http protocol. You analyze the information exchange between the server and client (for a better understanding of what is actually going on when information is exchanged via the internet).37. Server -> Client (udp) (pg. 185). You create client and server applications that transfer data (to the client) using the UDP protocol.38. Server -> ClientS (multicast) (pg. 187). You create client and server applications that transfer data to multiple clients simultaneously.39. Server -> Client (secure (SSL)) (pg. 189). You create client and server applications that transfer encrypted data (to the client). This makes it possible how create a “secure” connection between the server and the client.Chapters 40-41 introduce commercial servers

40. Commerical Server: Download / Install (pg. 199). You install various “commercial” server “appli-cations” on the server PC. In the previous chapters, the server-side program managed the connection to the internet itself. For most real-word applications, however, a separate server application runs on the server. You can run a single or multiple servers simultaneously on the PC.41. Commercial Server -> Client (pg. 215). You add the Java server applications you created in:

•Ch. 35. Server -> Client (pg. 179)•Ch. 36. Server -> Client (http) (pg. 181)•Ch. 37. Server -> Client (udp) (pg. 185)•Ch. 38. Server -> ClientS (multicast) (pg. 187)•Ch. 39. Server -> Client (secure (SSL)) (pg. 189)

to commerical servers.Chapters 42-46 introduce client (Script, Applet) and server (JSP, tags, servlets) -side technolo-gies

Java Getting Started 177of 501

Page 64: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

178of 501

Part HW. "Hello to World!" PC->PC

OHUHUcHW

LRURW

VDEZ

42. JavaScript (pg. 217). In Ch. 5. JavaScript (pg. 47), you created a simple JavaScript application that was simply interpreted in the browser. In this chapter, you create a server-side JavaScript applica-tion that is sent to and interpreted on the client-side. The interpreted application generates the phrase “Hello World” (on the client).43. JSP (pg. 219). You create a server-side JSP application that is interpreted on the server-side. The interpreted application generates the phrase “Hello World” (on the server), which is then sent by the commercial server to the client.44. Tags (pg. 223). You create server-side “tags” that are used to automatially generate script code.45. Applet (pg. 229). In Ch. 11. Applet (pg. 67) and Ch. 19. Applet (using DE) (pg. 97), you created simple applets. In this chapter, you add an applet to the commercial server. The applet is sent to the cli-ent and generates the phrase “Hello World”.46. Servlets (pg. 231). You create a server-side Servlet application (compiled) that is executed on the server-side. The executed application generates the phrase “Hello World” (on the server), which is then sent by the commercial server to the client.Chapters 47-50 introduce persistence in the internet environment

47. File (pg. 235). You create server-side applications that send file data (on the server) to the client.48. XML (pg. 237). You create server-side applications that send XML file data (on the server) to the client.49. Database (JDBC) (pg. 243). You create server-side applications that send database data (on the server) to the client.Chapters 50-52 introduce remote (via internet) objects

50. RMI (pg. 245). You create an application that makes remote objects available from a server to a cli-ent using RMI (Remote Method Invocation). The remote object sends the message “Hello World” to the client.51. IDL (pg. 249). You create an application that makes remote objects available from a server to a cli-ent using IDL. The remote object sends the message “Hello World” to the client.52. EJB (pg. 257). You create an application that makes available from a server to a client using EJB (Enterprise Java Beans). The remote object sends the message “Hello World” to the client.

Java Getting Started

Page 65: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcW353637383940414243444546474849505152LUWVDEZ

Part HW. "Hello to World!" PC->PC

HHH

RR

35. Server -> Client35.1. System requirements35.2. Create Server application35.1. Create dir c:\jgs\35.35.2. Create file c:\jgs\35\J35_2_Server.java:

// c:\jgs\35\J35_2_Server.javaimport java.net.*;import java.io.*;import java.util.*;public class J35_2_Server extends Thread{ ServerSocket srvsocket; boolean flag=true; int port = 8888; public J35_2_Server() { try { srvsocket = new ServerSocket(port); System.out.println("myserver is running"); } catch (IOException e) { System.err.println(e.getMessage()); System.exit(0); } } public void run() { Socket clientsocket; while (flag) { try { clientsocket = srvsocket.accept(); PrintWriter out = new PrintWriter(clientsocket.getOutputStream()); out.println("myserver response:"); out.println("Connection at: " + new Date().toString()); out.flush(); out.close(); clientsocket.close(); srvsocket.close(); flag = false; } catch (IOException e) { System.err.println(e.getMessage()); System.exit(0); } } } public static void main(String[] args) { J35_2_Server msrv = new J35_2_Server(); msrv.start();

Java Getting Started 179of 501

Chapter 35. Server -> ClientSection 35.1. System requirements

Page 66: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

180of 501

Part HW. "Hello to World!" PC->PC

OHUHUcHW

353637383940414243444546474849505152L

RURW

VDEZ

}}

35.3. Compile.c:\jgs\35>javac -classpath . J35_2_Server.java

35.3. Create Client application35.4. Create file c:\jgs\35\J35_3_Client.java:

// c:\jgs\35\J35_3_Client.javaimport java.net.*;import java.io.*;

public class J35_3_Client{ public static void main(String[] args) { System.out.println("Client is running"); try { InetAddress local = InetAddress.getLocalHost(); int port = 8888; Socket mysocket = new Socket(local, port); InputStream in = mysocket.getInputStream(); int ch; while ((ch = in.read()) != -1) System.out.print((char)ch); mysocket.close(); in.close(); } catch (IOException e) {} }}

35.5. Compile.c:\jgs\35>javac -classpath . J35_3_Client.java

35.4. Test35.6. Execute.

c:\jgs\35>java -classpath . J35_2_ServerThe message “myserver is running” appears.35.7. In a second DOS window: Execute.

c:\jgs\35>java -classpath . J35_3_ClientFollowing appears:

Figure 206. J35_Client / J35_Server

35.5. Review

Java Getting StartedChapter 35. Server -> ClientSection 35.3. Create Client application

Page 67: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcW353637383940414243444546474849505152LUWVDEZ

Part HW. "Hello to World!" PC->PC

HHH

RR

36. Server -> Client (http)36.1. System requirements36.2. Create Server application36.1. Create dir c:\jgs\36.36.2. Create file c:\jgs\36\J36_2_Server.java:

// c:\jgs\36\J36_2_Server.javaimport java.net.*;import java.io.*;import java.util.*;class J36_2_Server{ public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(Integer.parseInt(args[0])); while(true) new J36_2_HttpConnect(ss.accept()); } catch (ArrayIndexOutOfBoundsException ae) { System.err.println("Usage: Server port"); System.exit(0); } catch (IOException e) { System.out.println(e); } }}class J36_2_HttpConnect extends Thread { private Socket sock; J36_2_HttpConnect(Socket s) { sock = s; setPriority(NORM_PRIORITY - 1); start(); } public void run() { try { PrintWriter pw = new PrintWriter (new OutputStreamWriter(sock.getOutputStream()),true); BufferedReader br = new BufferedReader (new InputStreamReader(sock.getInputStream())); String req = br.readLine(); System.out.println("Request: " + req); StringTokenizer st = new StringTokenizer(req); if ((st.countTokens() >= 2) && st.nextToken().equals("GET")) { req = st.nextToken(); if (req.endsWith("/") || req.equals("")) req += "index.html"; if (req.startsWith("/")) req = req.substring(1);

Java Getting Started 181of 501

Chapter 36. Server -> Client (http)Section 36.1. System requirements

Page 68: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

182of 501

Part HW. "Hello to World!" PC->PC

OHUHUcHW

353637383940414243444546474849505152L

RURW

VDEZ

System.out.println(req); try { File f = new File(req); BufferedReader bfr = new BufferedReader(new FileReader(f)); char[] data = new char[(int)f.length()]; bfr.read(data); pw.println("HTTP/1.1 200 OK\n"); pw.write(data); pw.flush(); } catch (FileNotFoundException fe) { pw.println("HTTP/1.1 404 Not Foundxx\n"); } catch (IOException ioe) { System.out.println("3"); System.out.println(ioe); } } else pw.println("HTTP/1.1 400 Bad requestxx\n"); sock.close(); } catch (IOException e) { System.out.println(e); } }}

36.3. Compile.c:\jgs\36>javac -classpath . J36_2_Server.java

36.3. Create Client application36.4. Create file c:\jgs\36\J36_3_Client.java:

// c:\jgs\36\J36_3_Client.javaimport java.net.*;import java.io.*;import java.util.*;class J36_3_Client{ public static void main(String[] args) { if (args.length != 3) { System.err.println("usage: J36_3_Client host port file"); System.exit(0); } String host = args[0]; int port = Integer.parseInt(args[1]); String file = args[2]; try { Socket sock = new Socket(host,port); PrintWriter pw = new PrintWriter (new OutputStreamWriter(sock.getOutputStream()),true); pw.println("POST " + file + " HTTP/1.1\n"); BufferedReader br = new BufferedReader

Java Getting StartedChapter 36. Server -> Client (http)Section 36.3. Create Client application

Page 69: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcW353637383940414243444546474849505152LUWVDEZ

Part HW. "Hello to World!" PC->PC

HHH

RR

(new InputStreamReader(sock.getInputStream())); String line = null; line = br.readLine(); StringTokenizer st = new StringTokenizer(line); String code = null; if ((st.countTokens() >= 2) && st.nextToken().equals("POST")) { if ((code = st.nextToken()) != "200") { System.err.println("File not found, code = " + code); System.exit(0); } } while ((line = br.readLine()) != null) System.out.println(line); sock.close(); } catch (Exception e) { System.err.println(e); } }}

36.5. Compile.c:\jgs\36>javac -classpath . J36_3_Client.java

36.4. Create HTML file36.6. Create text file c:\jgs\36\J36_4_HTML.html:

<html><head><title>Titletext</title></head><body><h1>heading1</h1><h2>heading2</h2><p>Paragraphtext</p><ul> <li>Listitem1</li> <li>Listitem2</li></ul></body></html>

36.5. Test36.7. Execute.

c:\jgs\36>java -classpath . J36_2_Server 4444No message appears.36.8. In a second DOS window: Execute.

c:\jgs\36>java -classpath . J36_3_Client localhost 4444 J36_4_HTML.html

Java Getting Started 183of 501

Chapter 36. Server -> Client (http)Section 36.4. Create HTML file

Page 70: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

184of 501

Part HW. "Hello to World!" PC->PC

OHUHUcHW

353637383940414243444546474849505152L

RURW

VDEZ

Following appears:

Figure 207. Client / Server via HTTP

36.6. Receive with browser client (IE)36.9. In IE: Open the following URL:

http://localhost:4444/J36_HTML.htmlThe htmll file is displayed:

Figure 208. Receiving page with IE as client

36.7. Analyze request params36.8. Review

Java Getting StartedChapter 36. Server -> Client (http)Section 36.6. Receive with browser client (IE)

Page 71: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWLUWVDEZ

HHHRR

Part L. Language(s)

At this point you have completed (or passed) the following parts:•Part HU. "Hello to User!" on a PC (pg. 39)•Part HUc. "Hello to User!" on a CD (pg. 143)•Part HW. “Hello to World!” PC->PC (pg. 177)•Part HWc. @@@ "Hello to World!" PC->CD (pg. 263)

The above parts introduced various Java technologies. Thus, at this point you have an excellent idea (and more importantly, first hand experience) of how to setup a basic program for the above technolo-gies.However, the above parts did not go into the details of the Java code that was used. This part does. In this part, you will gain a good basic understanding of the fundamentals of the language itself and in par-ticular of what exactly object-oriented programming is.And, as always, you learn hands on by following step-by-step instructions.This part includes the following chapters:Chapters 54-57 introduce the basics of working with classes

54. Classes: Basics (define, instantiate, send messages (pg. 267). You create the most basic classes and send messages.55. Classes: Extensions (class/methods) (pg. 275). You create classes and methods that are exten-sions of other classes and methods.56. Classes: Location (packages, jars) (pg. 281). You create classes and use classes that are orga-nized into packages and packaged into jars.57. Classes / Methods / Vars : Require / Restrict (pg. 289). You create classes, methods and vari-ables whose implementation, access, etc. are required or restricted.Chapters 59-65 introduce the basics mechanisms of real-world programs

59. Flow control (pg. 299). You control the flow in a class using Java constructs such as if, switch, while, etc.60. Processes / Threads (subprocesses) (pg. 309). You create classes that can run multiple pro-cesses (basically programs) simultaneously.61. Input / Output (pg. 321). You create classes that can input (from keyboard, mouse, etc.) and out-put (to screen, printer, etc.) data.62. Exceptions (pg. 335). You create exception handlers (basically routines) that “handle” “excep-tions” (errors) that are generated.63. Graphical Interface (pg. 339). You create classes that have the standard graphical interface struc-tures (frames, buttons, text fields, etc.).64. Events (pg. 349). You create classes that are “listeners” for certain “events” (mouse is clicked, a window is closed, etc.) and that perform the required action when the event occurs.65. Sound (pg. 359). You create classes that generate sound (a sound board is required).Chapters 66-67 introduce client (JavaScript) and server (JSP) -side interpreted languages (tech-nically not Java)

66. JavaScript (pg. 365). You create basic programs (not classes) that are interpreted on the client side computer.67. JSP (Java Server Pages) (pg. 367). You create basic programs (not classes) that are interpreted on the server side computer.

Java Getting Started 265of 501

Page 72: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

266of 501

Part L. Language(s): Step-by-step

OHUHUcHW

LRURW

VDEZ

Chapters 68-69 introduce Java low-level topics

68. Collections (pg. 369). You create various types of collections (hash tables, etc.). Collections are used frequently in real-world programs that must process data.69. Math / Data types (pg. 371). You create programs that demonstrate basic Java math functions (add, modulus, etc.) and the differences between the various data types (integer, byte, etc.).

Java Getting Started

Page 73: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

54. Classes: Basics (define, instantiate, send messages

Assumes that the JDK has been installed as described in chapter 6. J2SDK/JRE: Download / Install (pg. 49).

54.1. Create and run classSee also chapter 7. Console Application (pg. 55).54.1.1. Create class source file (.java)53.1. Create dir C:\jgs\54.53.2. Create c:\jgs\54\J54_1_Class.java:

class J54_1_Class { public void main(String[] args) {}}

54.1.2. Compile source file (create class byte code(.class))53.3. Compile

javac -classpath . J54_1_Class.javaFile c:\jgs\54\J54_1_Class.class was created.

54.1.3. Run class file (JRE interprets .classbyte code)53.4. A the command prompt: Execute:

java -classpath . J54_1_ClassNothing appears, because the class doesnt do anything.

Figure 273. J54_1_Class.class executed (does nothing) 54.1.C. CONCEPTSxxxx.

Figure 274. Concepts: Basic class

54.2. Creating objects and references to them

Java Getting Started 267of 501

Chapter 54. Classes: Basics (define, instantiate, send messagesSection 54.1. Create and run class

Page 74: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

268of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

54.2.1. Arguments (sending to myclass from command line)Add also passing reference to file (“external” object)53.5. Createc:\jgs\54\J54_21_Args.java:

class J54_21_Args { public static void

main(String[] args)

{ String a0 = args[0]; System.out.println("Arg 0: \"" + a0 + "\""); }}

53.6. Compile.53.7. Execute:

Figure 275. Arguments entered 54.2.2. Class descriptionsAPI docs describe how to find and use supplied classes.54.2.2.1. Install Java docs53.8. Unzip 54_java_api__j2sdk-1_3_0-update1-doc.zip to c:\jgs\54_javaapi with Userfolder names selected.53.9. Open C:\jgs\54_javaapi\jdk1.3\docs\index.html. The general docs page is opened.

Figure 276. Java docs

Java Getting StartedChapter 54. Classes: Basics (define, instantiate, send messagesSection 54.2. Creating objects and references to them

Page 75: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

54.2.2.2. Find JDK classdescription

53.10. Click on Java 2 PlatformAPI . The API page is opened.

Figure 277. Java API docu 53.11. In All Classes: Click on System. Description of the System class is displayed.

Figure 278. Class System description Note the field “out”.53.12. In the description of “out”: Click on PrintStream. Description of PrintStream class is

Java Getting Started 269of 501

Chapter 54. Classes: Basics (define, instantiate, send messagesSection 54.2. Creating objects and references to them

Page 76: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

270of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

displayed.

Figure 279. Class PrintStream description Note the description for method “void println(String x)”.54.2.3. Reference JDK Singletondont really add, since only 1 instance exists.In J54_21_Args (above), you sent the “out” message to the “System” Singleton object.

class J54_21_Args { public static void

main(String[] args) { String a0 = args[0]; System.out.println("Arg 0:

\"" + a0 + "\""); }}

You dont have to create an instance of the Singleton, because only a single instance can exist.54.2.4. Creating with constructorsFor non-Singletons, you have to create an instance.54.2.4.1. Instantiate JDK class53.13. Createc:\jgs\54\J54_241_InstJDK.java:

class J54_241_InstJDK { public static void

main(String[] args)

{ String s; s = new String(); s = "abc"; }}

53.14. Compile.53.15. Execute (nothing is output to the screen).54.2.4.2. Add instance of own class53.16. Createc:\jgs\54\J54_242_InstOwn.java:

class J54_242_InstOwn { public static void main(String[]

args) { J54_242_Own my; my = new J54_242_Own();

Java Getting StartedChapter 54. Classes: Basics (define, instantiate, send messagesSection 54.2. Creating objects and references to them

Page 77: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

} static class J54_242_Own {}}

53.17. Compile. Note the created files:•J54_242_InstOwn.class•J54_242_InstOwn$J54_242_Own.class

53.18. Execute (nothing is output to the screen).54.2.5. Receiving references to objectsback from methodsObjects can also be created when sending a message to a method that returns an object.53.19. Createc:\jgs\54\J54_25_Method.java:

import java.io.*;class J54_25_Method { public static void main(String[]

args) { PrintStream pw = System.out; pw.println("from printwriter"); }}

53.20. Compile.53.21. Execute.54.2.C. CONCEPTSxxxx.

Figure 280. Concetps: Objects and references to them

54.3. Methods and “messages” (commands)54.3.1. Message description in Java docs53.22. Open the class description of PrintStream.53.23. Click on the method summary link for “void println(String x)”. Note the method

Java Getting Started 271of 501

Chapter 54. Classes: Basics (define, instantiate, send messagesSection 54.3. Methods and “messages” (commands)

Page 78: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

272of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

description.

Figure 281. Method “void println(String x) description in PrintStream description 54.3.2. Messages to JDK Singletons/objects54.3.2.1. No object refs sent / returnedfind jdk class that does the same thing.

54.3.2.2. Object ref sentNote in c:\jgs\54\J54_25_Method.java the method println.

pw.println("from print-writer");

This method returns no object. Note, how-ever, that if something goes wrong, then an exception will be generated. This is the way of basically avoiding having return codes sent.

54.3.2.3. Object ref returnedNote the method “out” sent to “System” in c:\jgs\54\J54_25_Method.java:

PrintStream pw = System.out;It returns a reference to a PrintStream object.

Java Getting StartedChapter 54. Classes: Basics (define, instantiate, send messagesSection 54.3. Methods and “messages” (commands)

Page 79: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

54.3.3. Messages to instances of own class54.3.3.1. No object refs sent / returned53.24. Create c:\jgs\54\J54_331_InstOwn.java:

class J54_331_InstOwn { public static void main(String[] args) { J54_331_Own my = new J54_331_Own(); my.sayHello(); } static class J54_331_Own { void sayHello() { System.out.println(“Hello”); } }}

53.25. Compile. 53.26. Execute.54.3.3.2. Object ref sent53.27. Create c:\jgs\54\J54_332_InstOwn.java:

class J54_332_InstOwn { public static void main(String[] args) { J54_332_Own my = new J54_332_Own(); my.sayHello("ref sent"); } static class J54_332_Own { void sayHello(String s) { System.out.println(s); } }}

53.28. Compile. 53.29. Execute.54.3.3.3. Object ref returned53.30. Create c:\jgs\54\J54_333_InstOwn.java:

class J54_333_InstOwn { public static void main(String[] args) { J54_333_Own my = new J54_333_Own(); String s = my.getHello(); System.out.println(s); } static class J54_333_Own { String getHello() { return ("ref returned"); } }}

53.31. Compile.

Java Getting Started 273of 501

Chapter 54. Classes: Basics (define, instantiate, send messagesSection 54.3. Methods and “messages” (commands)

Page 80: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

274of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

53.32. Execute.54.3.C. CONCEPTSxxxx.

Figure 282. Concepts: Methods and messages

Java Getting StartedChapter 54. Classes: Basics (define, instantiate, send messagesSection 54.3. Methods and “messages” (commands)

Page 81: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

55. Classes: Extensions (class/meth-ods)

Assumes that the JDK has been installed as described in chapter 6. J2SDK/JRE: Download / Install (pg. 49).

55.1. 833 Create subclass

55.1.1. Of JDK class54.1. Create dir C:\jgs\55.54.2. Create c:\jgs\55\J55_11_ExtendClassJDK.java:

import java.util.*;class J55_11_ExtendClassJDK { public static void main(String[] args) { J55_11_MyDate md = new J55_11_MyDate(); }}class J55_11_MyDate extends Date {}

54.3. Compile.54.4. Execute.55.1.2. Of own class54.5. Create c:\jgs\55\J55_12_ExtendClassMy.java:

import java.util.*;class J55_12_ExtendClassMy { public static void main(String[] args) { J55_12_ClassExtension ce = new J55_12_ClassExtension(); }}class J55_12_ClassExtension extends J55_12_ClassExtended {}class J55_12_ClassExtended {}

55.1.C. CONCEPTS

Java Getting Started 275of 501

Chapter 55. Classes: Extensions (class/methods)Section 55.1. 833 Create subclass

Page 82: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

276of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

55.2. Use methods/constructors of parent class in sub-class

55.2.1. Of JDK class54.6. Create c:\jgs\55\J55_21_ExtendClassJDK.java:

import java.util.*;class J55_21_ExtendClassJDK { public static void main(String[] args) { J55_21_MyDate md = new J55_21_MyDate(); System.out.println(md.getTime()); }}class J55_21_MyDate extends Date {}

54.7. Compile.54.8. Execute.

Figure 283. Use parent JDK class method 55.2.2. Of own class54.9. Create c:\jgs\55\J55_22_ExtendClassMy.java:

import java.util.*;class J55_22_ExtendClassMy { public static void main(String[] args) { J55_22_ClassExtension ce = new J55_22_ClassExtension(); ce.sayHello(); }}class J55_22_ClassExtension extends J55_22_ClassExtended {}class J55_22_ClassExtended { void sayHello() { System.out.println("Hello from extended (super) class"); }}

54.10. Compile.

Java Getting StartedChapter 55. Classes: Extensions (class/methods)Section 55.2. Use methods/constructors of parent class in subclass

Page 83: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

54.11. Execute.

Figure 284. Use parent own class method 55.2.C. CONCEPTS

55.3. Create new methods (in own class)

54.12. Createc:\jgs\55\J55_3_MyMethod.java (similar toc:\jgs\55\J55_21_ExtendClassJDK.java):

import java.util.*;class J55_3_MyMethod { public static void main(String[]

args) { J55_3_MyDate md = new

J55_3_MyDate(); System.out.println(md.getMyTime()); }}class J55_3_MyDate extends Date { String getMyTime() { return ("The time is " + getTime()); }}

54.13. Compile.54.14. Execute.

Figure 285. New method

Java Getting Started 277of 501

Chapter 55. Classes: Extensions (class/methods)Section 55.3. Create new methods (in own class)

Page 84: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

278of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

55.3.C. CONCEPTS

55.4. Override methods

54.15. Create c:\jgs\55\J55_4_MethodOverride.java:import java.util.*;class J55_4_MethodOverride { public static void main(String[] args) { J55_4_MyDate md = new J55_4_MyDate(); md.getTime(); }}class J55_4_MyDate extends Date { public long getTime() { System.out.println("Next line is the time from Date"); System.out.println(super.getTime()); return 0; }}

54.16. Compile.54.17. Execute:

Java Getting StartedChapter 55. Classes: Extensions (class/methods)Section 55.4. Override methods

Page 85: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

Figure 286. Override method 55.4.C. CONCEPTS

55.5. Overload constructors / methods55.5.1. Overload constructors

54.18. Create c:\jgs\55\J55_51_ConstructorOverload.java:import java.util.*;class J55_51_ConstructorOverload { public static void main(String[] args) { J55_51_MyDate md = new J55_51_MyDate("overloaded constructor"); }}class J55_51_MyDate extends Date { J55_51_MyDate(String s) { super(); System.out.println(s); }}

54.19. Compile.54.20. Execute:

Figure 287. Overloaded constructor

Java Getting Started 279of 501

Chapter 55. Classes: Extensions (class/methods)Section 55.5. Overload constructors / methods

Page 86: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

280of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

55.5.2. Overload methods

54.21. Create c:\jgs\55\J55_52_MethodOverload.java:import java.util.*;class J55_52_MethodOverload { public static void main(String[] args) { J55_52_MyDate md = new J55_52_MyDate(); md.getTime("Next line is the time from Date"); }}class J55_52_MyDate extends Date { public long getTime(String s) { System.out.println(s); System.out.println(super.getTime()); return 0; }}

54.22. Compile.54.23. Execute:

Figure 288. Overload method 55.5.C. CONCEPTS

Java Getting StartedChapter 55. Classes: Extensions (class/methods)Section 55.5. Overload constructors / methods

Page 87: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

56. Classes: Location (packages, jars)Its important to understand the basics of how java can find the class source files and class files when compiling and executing. And how to package class files into Jar files.Contrary to popular (in most books), this is rather complicated. This chapter, however, walks you thru the basics.The best way to learn all of the ins and outs: Experiment.Assumes that the JDK has been installed as described in chapter 6. J2SDK/JRE: Download / Install (pg. 49).

56.1. Single class56.1.1. Compilationjavac must find

•your source file (.java)•JDK classes (.jar)

56.1.1.1. No package55.1. Create dir c:\jgs\56.55.2. Create c:\jgs\56\J56_111_NoPackage.java:

class J56_111_NoPackage { public static void main(String[] args) { System.out.print("J56_111: no package specified"); }}

55.3. Compile.55.4. Execute (no errors).56.1.1.2. PACKAGE specified55.5. Create c:\jgs\56\J56_112_Package.java:

package p1;class J56_112_Package { public static void main(String[] args) { System.out.print("J56_112: PACKAGE p1 specified"); }}

55.6. Compile.c:\jgs\56>javac -classpath . J56_112_Package.java

55.7. Create c:\jgs\56\J56_1123_PackageP1P2P3.java:package p1.p2.p3;class J56_1123_PackageP1P2P3 { public static void main(String[] args) { System.out.print("J56_1123: PACKAGE p1.p2.p3 specified"); }}

55.8. Compile.c:\jgs\56>javac -classpath . J56_1123_PackageP1P2P3.java

56.1.2. Packaging (creating jar)You could deliver files as a group of class files and specify the required dirs. However, it would be eas-ier to pack into a zip file with the dirs included. Even easier: You dont have to unpack with Java.To package, you can use

•Jar tool (easier (?) to use)•Winzip (shows better what is being done)

56.1.2.1. no package

Java Getting Started 281of 501

Chapter 56. Classes: Location (packages, jars)Section 56.1. Single class

Page 88: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

282of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

If NO package is specified, then NO directory location is required in the jar.56.1.2.1.1. Jar toolCreate a jar of the class created earlier with no package.55.9. Execute (in “-C” the letter “C” is capitalized):

c:\jgs\56>jar -cfi J56_1211_NoPackageJarTool.jar -C ./J56_111_NoPackage.classThe file c:\jgs\56\J56_1211_NoPackageJarTool.jar is created.55.10. Open C:\jgs\56\J56_1211_NoPackageJarTool.jar with winzip. Note that no dir isspecified for the .class file.

Figure 289. Class in jar with no package specified 56.1.2.1.2. WinzipIf no package is specified (and thus no dir is required in the .jar file), then it is easy to create the jar using winzip.55.11. RIght-click on J56_111_NoPackage.class.55.12. Select Add to zip. The dialog “Add” appears.55.13. In “Add to archive”: Enter C:\jgs\56\J56_1211_NoPackageWinzip.jar.55.14. Uncheck under “Options” checkbox Save full path info.

Figure 290. Winzip setting to create jar with no dir 55.15. Click Add. The jar (zip) is created:

Figure 291. Jar with no dir created with Winzip 56.1.2.2. PACKAGE specifiedIf a package is specified, then the directory location must be included in the jar.56.1.2.2.1. Jar toolCreate a jar of the class created earlier with package p1 specified.

Java Getting StartedChapter 56. Classes: Location (packages, jars)Section 56.1. Single class

Page 89: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

55.16. Execute (in “-C” the letter “C” is capitalized):c:\jgs\56>jar -cfi J56_1221_P1JarTool.jar -C ./p1/J56_112_Package.class

The file c:\jgs\56\J56_1221_P1JarTool.jar is created.55.17. Open C:\jgs\56\J56_1221_P1JarTool.jar with winzip. Note that the dir /p1 is specifiedfor the .class file.

Figure 292. Class in jar with package p1 specified 56.1.2.2.2. WinzipUsing winzip is difficult when package has been specified, since you cannot modify the path of a com-pressed file in winzip. Therefore, you have to create the required dir and copy the files there.55.18. Create dir c:\p1.55.19. Copy c:\jgs\56\p1\J56_112_Package.class to c:\p1.55.20. Right-click on c:\p1\J56_112_Package.class.55.21. Select Add to zip. The dialog “Add” appears.55.22. In “Add to archive”: Enter C:\jgs\56\J56_1222_P1Winzip.jar.55.23. Check under “Options” checkbox Save full path info.

Figure 293. Winzip setting to create jar with dir 55.24. Click Add. The jar (zip) is created:

Figure 294. Jar with dir created with Winzip 56.1.3. ExecutionTo execute your file, java must find

•your class (in a .class OR .jar file)•JDK classes (.jar) (your class simply extends the JDK classes, which actually implement the corelow level functionality)

56.1.3.1. class in class file

Java Getting Started 283of 501

Chapter 56. Classes: Location (packages, jars)Section 56.1. Single class

Page 90: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

284of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

If the class is in a .class file, then the class file can be used to generate the instance. Note, however, if a package has been specified for the class, then the class must be in a dir path corresponding to the package.56.1.3.1.1. no packageThis situation has been demonstrated many times in the previous sections.55.25. Execute:

c:\jgs\56>java -classpath . J56_111_NoPackage56.1.3.1.2. packageThis situation requires

•.class file must in the dirs specified by the package (the parent dirs can be anything)•the package must be specified when running the .class

55.26. Executec:\jgs\56>java -classpath . J56_112_Package

An error is generated.

Figure 295. Error: Wrong package 55.27. Create dir c:\jgs\56\p1.55.28. Copy J56_112_Package.class to c:\jgs\56\p1.55.29. Attempt (error occurs):

c:\jgs\56\p1>java -classpath . J56_112_Package55.30. Attempt (error occurs):

c:\jgs\56>java -classpath ./p1 J56_112_Package55.31. Attempt (error occurs):

c:\jgs\56>java -classpath ./p1 p1.J56_112_Package55.32. Execute

c:\jgs\56>java -classpath . p1.J56_112_PackageNo error occurs.

Figure 296. Package is specified correctly 55.33. Execute (no errors):

c:\jgs\56>java -classpath . p1.p2.p3.J56_1123_PackageP1P2P3

Java Getting StartedChapter 56. Classes: Location (packages, jars)Section 56.1. Single class

Page 91: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

56.1.3.2. class in Jar fileThis situation requires that the JAR is specified in one of the following ways

•specified in the command line•specified in CLASSPATH system variable•located in the JDK /lib dir

56.1.3.2.1. Jar in classpathThe jar can be specified in the classpath via the

•command line•sysvar classpath

56.1.3.2.1.1. Classpath specified in COMMAND LINE56.1.3.2.1.1.1. no package55.34. Execute:

c:\jgs\56>java -classpath J56_1211_NoPackageJarTool.jar J56_111_NoPackageThe class executes correctly.56.1.3.2.1.1.2. packageThe package must be included when calling the class.55.35. Execute (error occurs):

c:\jgs\56>java -classpath J56_1221_P1JarTool.jar J56_112_Package55.36. Execute:

c:\jgs\56>java -classpath J56_1221_P1JarTool.jar p1.J56_112_PackageThe class executes correctly.56.1.3.2.1.2. Classpath specified in SYSVAR CLASSPATH56.1.3.2.1.2.1. Add to sysvar classpathYou must first add the jar to the system variable CLASSPATH and then the class can be executed from any dir.55.37. From the Start Menu: Select Start / Settings / Control Panel.55.38. Click on System.55.39. Select tab Advanced.55.40. Click on Environment Variables. The dialog “Environment variables” appears.55.41. Under System variables: Double-click on CLASSPATH.55.42. Add “C:\jgs\56\J56_1211_NoPackageJarTool.jar;” to the variable value:

Figure 297. Jar added to sysvar CLASSPATH

Java Getting Started 285of 501

Chapter 56. Classes: Location (packages, jars)Section 56.1. Single class

Page 92: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

286of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

55.43. Click OK.55.44. Click OK.56.1.3.2.1.2.2. no package55.45. Open a NEW DOS window (the new classpath does not take effect in the old window).55.46. Execute (in any dir):

c:\>java J56_111_NoPackageThe class executes correctly.56.1.3.2.1.2.3. packageXXX56.1.3.2.2. Jar in JDK LIBXXX56.1.3.2.2.1. copy jar to JDK libXXX56.1.3.2.2.2. no packageXXX56.1.3.2.2.3. packageXXX

56.2. Multiple classesActually, when creating a single class above you were creating a subclass of java classes, and there-fore required multiple classes. However, the java classes are already a “part” of the system, and there-fore this is all transparent. In this section you create 2 classes that require each other.56.2.1. Compilation56.2.1.1. Same source filejavac must find

•your source file (.java) (contains code for both classes)•JDK classes (.jar)

Both classes will by default be in the same package.55.47. Create c:\jgs\56\J56_211_Class1.java:

package p1;class J56_211_Class1 { public static void main(String[] args) { J56_211_Class2 c = new J56_211_Class2(); }}class J56_211_Class2 {}

55.48. Compile.c:\jgs\56>javac -classpath . J56_211_Class1.java

The following files are created:•J56_211_Class1.class•J56_211_Class2.class

56.2.1.2. Different source fileswhen compiling source file 1: javac must find

•your source file 1 (.java)•the java or class code for class 2 (in a java, class or jar file)•JDK classes (.jar)

56.2.1.2.1. file 2 is javaWhen compiling source file 1, if javac requires a class, and that class exists only as a .java source file, then javac will compile the other class also.56.2.1.2.1.1. No package

Java Getting StartedChapter 56. Classes: Location (packages, jars)Section 56.2. Multiple classes

Page 93: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

55.49. Create c:\jgs\56\J56_21211_Java1.java:class J56_21211_Java1 { public static void main(String[] args) { J56_21211_Java2 c = new J56_21211_Java2(); }}

55.50. Create c:\jgs\56\J56_21211_Java2.java:class J56_21211_Java2 {}

55.51. Compile c:\jgs\56\J56_21211_Java1.java.c:\jgs\56>javac -classpath . J56_21211_Java1.java

The following files are created (J56_21211_Java2 was automatically compiled):•c:\jgs\56\J56_21211_Java1.class•c:\jgs\56\J56_21211_Java2.class

56.2.1.2.1.2. PackageWhen Class2 has a package specified, then javac will search for the source file in a subdir of the dirs specified in the -classpath option, where the subdir matches the package specification.Note: Be very careful about the dirs in the following example.55.52. Create c:\jgs\56\J56_21212_JavaP1.java:

import p2.J56_21212_JavaP2;class J56_21212_JavaP1 { public static void main(String[] args) { J56_21212_JavaP2 c = new J56_21212_JavaP2(); }}

55.53. Create c:\jgs\56\p1\p2\J56_21212_JavaP2.java:package p2;public class J56_21212_JavaP2 {}

55.54. Compile c:\jgs\56\J56_21211_JavaP1.java.c:\jgs\56>javac -classpath .;./p1 J56_21212_JavaP1.java

The following files are created (J56_21212_JavaP2 was automatically compiled):•c:\jgs\56\J56_21212_JavaP1.class•c:\jgs\56\p1\p2\J56_21212_JavaP2.class

56.2.1.2.2. file 2 is classThis would be identical to the situation above (56.2.1.2.1. file 2 is java (pg. 286))if, instead of a .java file, there was already a class file created.56.2.1.2.2.1. No packageSame as 56.2.1.2.1.1. No package (pg. 286).56.2.1.2.2.2. PackageSame as 56.2.1.2.1.2. Package (pg. 287).56.2.1.2.3. file 2 (class file) is in JARIn this situation, javac must do 2 things:

•find the jar•find the class file (with the dir corresponding to the package (if a package was specified)) in the jar

The Jar can be found in 1 of 2 ways:•in the classpath (specified on the command line or with the sysvar CLASSPATH)•by placing the jar in the JDK /lib dir

This is similar to 56.1.3.2. class in Jar file (pg. 285).56.2.1.2.3.1. Jar in CLASSPATHIf class 2 is in a Jar, then the jar can be made findable via the classpath variable. This is similar to 56.1.3.2.1. Jar in classpath (pg. 285).56.2.1.2.3.2. Jar in JDK LIB

Java Getting Started 287of 501

Chapter 56. Classes: Location (packages, jars)Section 56.2. Multiple classes

Page 94: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

288of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

If class 2 is in a Jar, then the jar can be made findable by placing in the JDK /lib dir. This is similar to 56.1.3.2.2. Jar in JDK LIB (pg. 286).56.2.2. Packaging (creating jar)Class 1 and class 2 can be in same or different jars. The only important thing is that both jars can be found.56.2.3. Executionjava must find

•1.class (.class OR .jar)•2.class (.class OR .jar)•JDK classes (.jar)

You already made class 2 findable for compilation in section 56.2.1. Compilation (pg. 286). In order to execute class 1, class 1 and 2 must simply be findable.

Java Getting StartedChapter 56. Classes: Location (packages, jars)Section 56.2. Multiple classes

Page 95: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

63. Graphical Interface63.1. Containers63.1.1. Frame63.1.1.1. Basic63.1. Create dir c:\jgs\63.63.2. Create file c:\jgs\63\J63_111_FrameBasic.java:

import java.awt.*;class J63_111_FrameBasic extends Frame{ public static void main(String[] args) { Frame fr = new J63_111_FrameBasic(); fr.setSize(400,150); fr.setVisible(true); }}

63.3. Compile.c:\jgs\63>javac -classpath . J63_111_FrameBasic.java

63.4. Execute.c:\jgs\63>java -classpath . J63_111_FrameBasic

Frame appears.

Figure 355. Basic Frame Note that the Frame cannot be closed.Note that the process is still running in the DOS window.63.5. Click CTRL-C in the DOS window. The Frame is closed.63.1.1.2. Frame that closesEvents are covered in next chapter... but need this one now.63.6. Create file c:\jgs\63\J63_112_FrameClosing.java:

import java.awt.*;import java.awt.event.*;class J63_112_FrameClosing extends Frame{ J63_112_FrameClosing() { super(); setSize(400,150); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { System.exit(0); }

Java Getting Started 339of 501

Chapter 63. Graphical InterfaceSection 63.1. Containers

Page 96: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

340of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

}); } public static void main(String[] args) { new J63_112_FrameClosing(); }}

63.7. Compile.c:\jgs\63>javac -classpath . J63_112_FrameClosing.java

63.8. Execute.c:\jgs\63>java -classpath . J63_112_FrameClosing

Click on the X in the upper right. The frame closes.63.1.1.3. Title and text63.9. Create file c:\jgs\63\J63_113_FrameTitleText.java:

import java.awt.*;import java.awt.event.*;class J63_113_FrameTitleText extends Frame{ public void paint(Graphics g) { g.setFont(new Font("Serif",Font.ITALIC|Font.BOLD,30)); g.drawString("Body text",20,100); } J63_113_FrameTitleText(String s) { super(s); setSize(400,150); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { System.exit(0); } }); } public static void main(String[] args) { new J63_113_FrameTitleText("Title text"); }}

63.10. Compile.c:\jgs\63>javac -classpath . J63_113_FrameTitleText.java

63.11. Execute.c:\jgs\63>java -classpath . J63_113_FrameTitleText

Frame with title and body text appears.

Figure 356. Frame with title and body text 63.1.2. Panel

Java Getting StartedChapter 63. Graphical InterfaceSection 63.1. Containers

Page 97: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

63.1.3. ScrollPanel63.1.4. Window63.1.5. Dialog63.1.6. FileDialog63.2. Components63.2.1. StandardThe events for the components will be reviewed in the next chapter.63.12. Create file c:\jgs\63\J63_21_ComponentsStandard.java:

import java.awt.*;import java.awt.event.*;class J63_21_ComponentsStandard extends Frame { J63_21_ComponentsStandard(String s) { super(s); setLayout(null); setFont(new Font("Serif",Font.BOLD, 15));

Label l1 = new Label("Label 1"); l1.setBounds(10,30,100,30); add(l1);

Button b1 = new Button("Button 1"); b1.setBounds(120,30,100,30); add(b1);

Checkbox ch1 = new Checkbox("Checkbox 1"); ch1.setBounds(10,60,100,30); add(ch1); Checkbox ch2 = new Checkbox("Checkbox 2"); ch2.setBounds(10,90,100,30); add(ch2);

CheckboxGroup grp = new CheckboxGroup(); Checkbox chg1 = new Checkbox("GroupCheckbox 1",grp, true); chg1.setBounds(120,60,200,30); add(chg1); Checkbox chg2 = new Checkbox("GroupCheckbox 2",grp, false); chg2.setBounds(120,90,200,30); add(chg2);

List l = new List(2,true); l.add("ListChoice1"); l.add("ListChoice2"); l.setBounds(10,120,100,70); add(l);

Choice ch = new Choice(); ch.add("Choice1"); ch.add("Choice2"); ch.setBounds(120,120,100,70); add(ch);

TextField tf1 = new TextField(30); tf1.setBounds(10,200,100,30); add(tf1);

Java Getting Started 341of 501

Chapter 63. Graphical InterfaceSection 63.2. Components

Page 98: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

342of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

TextField tf2 = new TextField(30); tf2.setBounds(120,200,100,30); tf2.setEchoChar('*'); add(tf2);

TextArea ta1 = new TextArea("TextArea text",5,50); ta1.setBounds(10,240,220,80); ta1.setEditable(false); add(ta1);

setSize(300,350); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent ev) { System.exit(0); } }); } public static void main(String[] args) { new J63_21_ComponentsStandard("Title text"); }}

63.13. Compile.c:\jgs\63>javac -classpath . J63_21_ComponentsStandard.java

63.14. Execute.c:\jgs\63>java -classpath . J63_21_ComponentsStandard

Frame with components appears.

Figure 357. Frame with components 63.15. Check the checkboxes.63.16. Check the group checkboxes. Note that only 1 can be selected.63.17. Select ListChoices. Note that multiple can be selected.63.18. Select the Choices. Note that only 1 can be selected.63.19. Enter “Text field” in the left TextField.63.20. Enter “Text field” in the right TextField (with echo). Note that only asterisks are shown.

Java Getting StartedChapter 63. Graphical InterfaceSection 63.2. Components

Page 99: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

63.21. Attempt to edit the TextArea text. Note that you cant.

Figure 358. Text component results 63.2.2. Custom63.2.2.1. ?? From Canvas (heavy)63.2.2.2. ?? From Component / Container (light)63.3. Layouts63.3.1. No layout63.22. Create file c:\jgs\63\J63_31_LayoutNull.java:

import java.awt.*;public class J63_31_LayoutNull extends Frame { public J63_31_LayoutNull() { Button b1 = new Button("Cancel"); Button b2 = new Button("OK"); setLayout(null); b1.setBounds(50,70,50,24); b2.setBounds(110,70,50,24); add(b1); add(b2); setSize(50,100); setVisible(true); } public static void main(String[] args) { new J63_31_LayoutNull(); }}

63.23. Compile.c:\jgs\63>javac -classpath . J63_31_LayoutNull.java

63.24. Execute.c:\jgs\63>java -classpath . J63_31_LayoutNull

Frame appears.

Figure 359. Frame with no layout 63.3.2. FlowLayout

Java Getting Started 343of 501

Chapter 63. Graphical InterfaceSection 63.3. Layouts

Page 100: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

344of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

63.25. Create file c:\jgs\63\J63_32_LayoutFlow.java:import java.awt.*;public class J63_32_LayoutFlow extends Frame { public J63_32_LayoutFlow() { setLayout(new FlowLayout(FlowLayout.CENTER,0,0)); add(new Button("button1")); add(new Button("button2")); add(new Button("button3")); add(new Button("button4")); add(new Button("button5")); setSize(50,100); setVisible(true); } public static void main(String[] args) { new J63_32_LayoutFlow(); }}

63.26. Compile.c:\jgs\63>javac -classpath . J63_32_LayoutFlow.java

63.27. Execute.c:\jgs\63>java -classpath . J63_32_LayoutFlow

Frame appears.

Figure 360. Frame with FlowLayout 63.3.3. BorderLayout63.28. Create file c:\jgs\63\J63_33_LayoutBorder.java:

import java.awt.*;public class J63_33_LayoutBorder extends Frame { public J63_33_LayoutBorder() { setLayout(new BorderLayout()); add("West" , new Button("W")); add("South" , new Button("S")); add("East" , new Button("E")); add("North" , new Button("N")); add("Center", new Button("C")); setSize(50,100); setVisible(true); } public static void main(String[] args) { new J63_33_LayoutBorder(); }}

63.29. Compile.c:\jgs\63>javac -classpath . J63_33_LayoutBorder.java

63.30. Execute.c:\jgs\63>java -classpath . J63_33_LayoutBorder

Java Getting StartedChapter 63. Graphical InterfaceSection 63.3. Layouts

Page 101: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

Frame appears.

Figure 361. Frame with BorderLayout 63.3.4. GridLayout63.3.5. CardLayout63.3.6. GridbagLayout63.3.7. Layout in a layout63.4. Menu63.4.1. Bar menu63.4.1.1. MenuBar63.31. Create file c:\jgs\63\J63_411_MenuBar.java:

import java.awt.*;import java.awt.event.*;public class J63_411_MenuBar extends Frame { public J63_411_MenuBar(String s) { super(s); MenuBar mb = new MenuBar(); setMenuBar(mb); setSize(200,100); setVisible(true); } public static void main(String[] args) { new J63_411_MenuBar("title"); }}

63.32. Compile.c:\jgs\63>javac -classpath . J63_411_MenuBar.java

63.33. Execute.c:\jgs\63>java -classpath . J63_411_MenuBar

Frame appears.

Figure 362. MenuBar 63.4.1.2. Menu63.34. Create file c:\jgs\63\J63_412_Menu.java:

import java.awt.*;import java.awt.event.*;public class J63_412_Menu extends Frame { public J63_412_Menu(String s) {

Java Getting Started 345of 501

Chapter 63. Graphical InterfaceSection 63.4. Menu

Page 102: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

346of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

super(s); MenuBar mb = new MenuBar(); setMenuBar(mb); Menu menu1 = new Menu("menu1text"); mb.add(menu1); setSize(200,100); setVisible(true); } public static void main(String[] args) { new J63_412_Menu("title"); }}

63.35. Compile.c:\jgs\63>javac -classpath . J63_412_Menu.java

63.36. Execute.c:\jgs\63>java -classpath . J63_412_Menu

Frame appears with menu:

Figure 363. Menu 63.4.1.3. MenuItem63.37. Create file c:\jgs\63\J63_413_MenuItem.java:

import java.awt.*;import java.awt.event.*;public class J63_413_MenuItem extends Frame { public J63_413_MenuItem(String s) { super(s); MenuBar mb = new MenuBar(); setMenuBar(mb); Menu menu1 = new Menu("menu1text"); mb.add(menu1); MenuItem menuItem1 = new MenuItem("menuItem1text",new MenuShortcut(KeyEvent.VK_O)); menu1.add(menuItem1); menu1.addSeparator(); Menu subMenu1 = new Menu("subMenu1text"); menu1.add(subMenu1); MenuItem subMenuItem1 = new MenuItem("subMenuItem1text"); subMenu1.add(subMenuItem1); setSize(400,140); setVisible(true); } public static void main(String[] args) { new J63_413_MenuItem("title"); }}

63.38. Compile.c:\jgs\63>javac -classpath . J63_413_MenuItem.java

Java Getting StartedChapter 63. Graphical InterfaceSection 63.4. Menu

Page 103: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWL545556575960616263646566676869UWVDEZ

Part L. Language(s): Step-by-step

HHH

RR

63.39. Execute.c:\jgs\63>java -classpath . J63_413_MenuItem

Frame appears.

Figure 364. MenuItem 63.4.2. Popup menu63.40. Create file c:\jgs\63\J63_42_MenuPopup.java:

import java.awt.*;import java.awt.event.*;public class J63_42_MenuPopup extends Frame { PopupMenu pum = new PopupMenu("popupmenutext"); public J63_42_MenuPopup(String s) { super(s); add(pum); MenuItem menuItem1 = new MenuItem("menuItem1text",new MenuShortcut(KeyEvent.VK_O)); pum.add(menuItem1); MenuItem menuItem2 = new MenuItem("menuItem2text"); pum.addSeparator(); pum.add(menuItem2); setSize(200,100); setVisible(true); pum.show(this,this.getX(),this.getY()); } public static void main(String[] args) { new J63_42_MenuPopup("title"); }}

63.41. Compile.c:\jgs\63>javac -classpath . J63_42_MenuPopup.java

63.42. Execute.c:\jgs\63>java -classpath . J63_42_MenuPopup

A frame and a popup menu appear.

Figure 365. MenuPopup

Java Getting Started 347of 501

Chapter 63. Graphical InterfaceSection 63.4. Menu

Page 104: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

348of 501

Part L. Language(s): Step-by-step

OHUHUcHW

L545556575960616263646566676869

RURW

VDEZ

Java Getting StartedChapter 63. Graphical InterfaceSection 63.4. Menu

Page 105: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWLUWVDEZ

HHHRR

Part RU. “Reply from User!” on a PC

In Part HU. "Hello to User!" on a PC (pg. 39) you created PC applications that generated the phrase “Hello User”.In this part you modify the PC applications so that the user can “reply” (ie, the application accepts user input).This part includes the following chapters:70. Console Application (pg. 375). You modify the console applications you created in chapter 7. Console Application (pg. 55) and chapter 15. Console Application (using DE) (pg. 81) so that the appli-cations can accept user input.71. Dialog Application (pg. 377). You modify the dialog applications you created in chapter 10. Dialog Application (pg. 65) and chapter 18. Dialog Application (using DE) (pg. 93) so that the applications can accept user input (add text fields, buttons, checkboxes, etc.).72. XML (pg. 379). You modify the XML applications you created in chapter 22. XML (pg. 107) so that the applications can accept user input.73. File (pg. 381). You modify the file applications you created in chapter 21. File (pg. 105) so that the applications can accept user input.74. JDBC (pg. 383). You modify the JDBC applications you created in chapter 23. Database (using JDBC) (pg. 113) so that the applications can accept user input.

Java Getting Started 373of 501

Page 106: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

374of 501

Part RU. "Reply from User!" on PC

OHUHUcHW

LRURW

VDEZ

Java Getting Started

Page 107: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWLUWVDEZ

HHHRR

Part RW. “Reply from World!” PC<->PC

In Part HW. “Hello to World!” PC->PC (pg. 177) you created server PC and client PC applications that send the phrase “Hello World” from the server PC through the internet to the client PC. You tested the applications on a single PC (used as the PC server and PC client) without the internet.In this part you modify the [ server PC and client PC ] applications so that the client PC can send a “Reply from World” back to the server PC.This part includes the following chapters:76. JavaScript (pg. 387). You modify the JavaScript applications you created in chapter 42. JavaScript (pg. 217) so that the client PC can reply to the server PC.77. Applet (pg. 389). You modify the applets you created in chapter 45. Applet (pg. 229) so that the cli-ent PC can reply to the server PC.78. JSP (pg. 391). You modify the JSP applications you created in 43. JSP (pg. 219) so that the client PC can reply to the server PC.79. Servlet (pg. 393). You modify the Servlets you created in 46. Servlets (pg. 231) so that the client PC can reply to the server PC.80. Beans (pg. 397). You create a bean that allows the client PC to reply to the server PC.81. Sessions (pg. 399). You create a PC server application that can server multiple PC clients and keep track of which client an http request is received from by using sessions.82. Cookies (pg. 413). You create a PC server application that places a small file (a “cookie”) on the PC client that contains information about the previous information exchanges between the server and the client.83. XML (pg. 417). You modify the XML applications you created in 48. XML (pg. 237) so that the client PC can reply to the server PC.84. File (pg. 429). You modify the file applications you created in 47. File (pg. 235) so that the client PC can reply to the server PC.85. JDBC (pg. 431). You modify the JDBC applications you created in 49. Database (JDBC) (pg. 243) so that the client PC can reply to the server PC.

Java Getting Started 385of 501

Page 108: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

386of 501

Part RW. "Reply from World!" PC<->PC

OHUHUcHW

LRURW

VDEZ

Java Getting Started

Page 109: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWLUWVDEZ

HHHRR

Part V. VersionsIn this part you create and manage versions of your applications. An application can exist in several different versions which differ in supported functionality or in the supported (human) language. A version of an application can also be created after any number of changes have been implemented in the application. This is an extremely important aspect of application development. Any change to an application may introduce bugs, some of which you may not find for quite a while. In such cases it is critical that you can backtrack to previous versions in order to find out what changes created the bug.This part includes the following chapters:87. Resources (pg. 435). You create application versions that provide different supported functionality using resource files.88. Languages (pg. 437). You create application versions that support specific languages.89. CVS Download / Install (pg. 439). You install CVS. CVS is a shareware program for managing versions of files.90. CVS versioning (pg. 441). You manage application files using CVS.

Java Getting Started 433of 501

Page 110: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

434of 501

Part V. Versions

OHUHUcHW

LRURW

VDEZ

Java Getting Started

Page 111: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWLUWVDEZ

HHHRR

Part D. DocumentationIn this part you document the programs that you create. Documentation (including writing books :) ) is not the most exciting part of software development, but extremely important for enabling others to use your applications as a building block in their own applications (and thus increasing the market value of your own applications).This part includes the following chapters:91. JavaDoc (pg. 445). You create standardized class descriptions using the JavaDoc tool from Sun. The created documentation is similar in format to the JDK documentation installed in chapter 54.2.2.1. Install Java docs (pg. 268).92. Java2Html (pg. 447). You create HTML files from Java sources files. These html files allow you to make your Java code available via internet (you could also do this with simple text files; however, Java2Html creates html files that include the preferred html formatting).93. JavaHelp (pg. 449). You create Java help (with TOC, index, and search) and integrate the help files into an application (ie, the help can be called up from within the application).

Java Getting Started 443of 501

Page 112: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

444of 501

Part D. Documentation

OHUHUcHW

LRURW

VDEZ

Java Getting Started

Page 113: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWLUWVDEZ

HHHRR

Part E. Examples (advanced)In the previous parts, you learned how to utilize basic Java technologies and how to program using the Java languages.In this part you will install and test advanced examples. These example demonstrate the capabilities of Java and are frequently referenced in Sun descriptions of Java technologies.Note: Documentation already exists for these advanced examples. However, the documentation in this book is far more detailed and makes it much easier to install the examples.This part includes the following chapters:94. Java PetStore (pg. 455). Demonstrates a variety of Sun technologies. A complex example that is referenced often in Sun documentation.95. Java Management Extension (JMX) (pg. 475). Demonstrates the Java Management Extensions.96. Java Media Framework (JMF) (pg. 477). Demonstrates the complete media framework proposed by Sun.97. Java 3D (pg. 485). Demonstrates how complex 3D drawings can be generated with Java.

Java Getting Started 453of 501

Page 114: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

454of 501

Part E. Examples

OHUHUcHW

LRURW

VDEZ

Java Getting Started

Page 115: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

OUUcWLUWVDEZ

HHHRR

Part Z. The last partAt this point you have completed all of the programming in the book (or have at least completed those sections of interest to you), and have a very good basic understanding of Java technologies and the language itself. This last part provides a lot of helpful information (but no programming).This part includes the following chapters:98. Where to go from here (pg. 489). Describes sources of information (URL’s, books, etc.) for con-tinuing your Java education.99. Trouble-shooting (pg. 491). Describes how to solve the typical problems you may encounter while doing the examples in this book.100. FAQ (pg. 493). Answers the most frequently asked questions of a beginning Java programmer.101. List of figures (pg. 495).102. Index (pg. 501).

Java Getting Started 487of 501

Page 116: TO THE PUBLISHER - slowchinese.com · complete workbook for intelligent people who value their time)” (being written by Terry Taylor). It is intended to give you (the publisher)

488of 501

Part Z. The last part

OHUHUcHW

LRURW

VDEZ

Java Getting Started