CORE JAVA 9Chapter 2.docx
- 文档编号:9248479
- 上传时间:2023-02-03
- 格式:DOCX
- 页数:21
- 大小:895.91KB
CORE JAVA 9Chapter 2.docx
《CORE JAVA 9Chapter 2.docx》由会员分享,可在线阅读,更多相关《CORE JAVA 9Chapter 2.docx(21页珍藏版)》请在冰豆网上搜索。
COREJAVA9Chapter2
Chapter2.TheJavaProgrammingEnvironment
Inthischapter
• InstallingtheJavaDevelopmentKit
• ChoosingaDevelopmentEnvironment
• UsingtheCommand-LineTools
• UsinganIntegratedDevelopmentEnvironment
• RunningaGraphicalApplication
• BuildingandRunningApplets
Inthischapter,youwilllearnhowtoinstalltheJavaDevelopmentKit(JDK)andhowtocompileandrunvarioustypesofprograms:
consoleprograms,graphicalapplications,andapplets.YoucanruntheJDKtoolsbytypingcommandsinashellwindow.However,manyprogrammerspreferthecomfortofanintegrateddevelopmentenvironment.WeshowyouhowtouseafreelyavailabledevelopmentenvironmenttocompileandrunJavaprograms.Althougheasiertolearn,integrateddevelopmentenvironmentscanberesource-hungryandtedioustouseforsmallprograms.Onceyouhavemasteredthetechniquesinthischapterandpickedyourdevelopmenttools,youarereadytomoveonto Chapter3,whereyouwillbeginexploringtheJavaprogramminglanguage.
2.1.InstallingtheJavaDevelopmentKit
Themostcompleteandup-to-dateversionsoftheJavaDevelopmentKit(JDK)areavailablefromOracleforLinux,MacOSX,Solaris,andWindows.Versionsinvariousstatesofdevelopmentexistformanyotherplatforms,butthoseversionsarelicensedanddistributedbythevendorsofthoseplatforms.
2.1.1.DownloadingtheJDK
TodownloadtheJavaDevelopmentKit,visitthewebsiteat bepreparedtodecipheranamazingamountofjargonbeforeyoucangetthesoftwareyouneed.See Table2.1 forasummary.
Table2.1.JavaJargon
YoualreadysawtheabbreviationJDKforJavaDevelopmentKit.Somewhatconfusingly,versions1.2through1.4ofthekitwereknownastheJavaSDK(SoftwareDevelopmentKit).Youwillstillfindoccasionalreferencestotheoldterm.ThereisalsoaJavaRuntimeEnvironment(JRE)thatcontainsthevirtualmachinebutnotthecompiler.Thatisnotwhatyouwantasadeveloper.Itisintendedforenduserswhohavenoneedforthecompiler.
Next,you’llseethetermJavaSEeverywhere.ThatistheJavaStandardEdition,incontrasttoJavaEE(EnterpriseEdition)andJavaME(MicroEdition).
YouwilloccasionallyrunintothetermJava2thatwascoinedin1998whenthemarketingfolksatSunfeltthatafractionalversionnumberincrementdidnotproperlycommunicatethemomentousadvancesofJDK1.2.However,becausetheyhadthatinsightonlyaftertherelease,theydecidedtokeeptheversionnumber1.2forthedevelopmentkit.Subsequentreleaseswerenumbered1.3,1.4,and5.0.The platform,however,wasrenamedfromJavatoJava2.Thus,wehadJava2StandardEditionSoftwareDevelopmentKitVersion5.0,orJ2SESDK5.0.
Forengineers,allofthiswasabitconfusing,butthat’swhywenevermadeitintomarketing.Mercifully,in2006,sanityprevailed.TheuselessJava2monikerwasdropped,andthecurrentversionoftheJavaStandardEditionwascalledJavaSE6.Youwillstillseeoccasionalreferencestoversions1.5and1.6—thesearejustsynonymsforversions5.0and6.
Finally,whenOraclemakesaminorversionchangetofixurgentissues,itreferstothechangeasanupdate.Forexample,thefirstupdateofthedevelopmentkitforJavaSE7isofficiallycalledJDK7u1andhastheinternalversionnumber1.7.0_01.Anupdatedoesnotneedtobeinstalledoverapriorversion—itcontainsthemostcurrentversionofthewholeJDK.
Sometimes,OraclemakesavailablebundlesthatcontainboththeJavaDevelopmentKitandanintegrateddevelopmentenvironment.Thatintegratedenvironmenthas,atdifferenttimesofitslife,beennamedForte,SunONEStudio,SunJavaStudio,andNetbeans.Wedonotknowwhattheeagerbeaversinmarketingwillcallitwhenyouapproachthedownloadsite.WesuggestthatyoustayawayfromallbundlesandinstallonlytheJavaDevelopmentKitatthistime.IfyoulaterdecidetouseOracle’sintegrateddevelopmentenvironment,simplydownloaditfrom http:
//netbeans.org.
Note
ThesetupprocedureoffersadefaultfortheinstallationdirectorythatcontainstheJDKversionnumber,suchas jdk1.7.0.Thissoundslikeabother,butwehavecometoappreciatetheversionnumber—itmakesiteasiertoinstallanewerJDKreleasefortesting.
UnderWindows,westronglyrecommendthatyoudonotacceptadefaultlocationwithspacesinthepathname,suchas c:
\ProgramFiles\jdk1.7.0.Justtakeoutthe ProgramFiles partofthepathname.
Inthisbook,werefertotheinstallationdirectoryas jdk.Forexample,whenwerefertothe jdk/bindirectory,wemeanthedirectorywithanamesuchas /usr/local/jdk1.7.0/bin or c:
\jdk1.7.0\bin.
2.1.2.SettingtheExecutablePath
AfteryouaredoneinstallingtheJDK,youneedtocarryoutoneadditionalstep:
Addthe jdk/bin directorytotheexecutablepath—thelistofdirectoriesthattheoperatingsystemtraversestolocateexecutablefiles.Directionsforthisstepalsovaryamongoperatingsystems.
•InUNIX(includingLinux,MacOSX,andSolaris),theprocedureforeditingtheexecutablepathdependsontheshellyouareusing.IfyouusetheBourneAgainshell(whichistheLinuxdefault),thenaddalinesuchasthefollowingtotheendofyour ~/.bashrc or ~/.bash_profile file:
exportPATH=jdk/bin:
$PATH
•UnderWindows,loginasadministrator.StarttheControlPanel,switchtoClassicView,andselecttheSystemicon.InWindowsXP,youimmediatelygetthesystempropertiesdialog.InVistaandWindows7,youneedtoselectAdvancedSystemSettings(see Figure2.1).Inthesystempropertiesdialog,clicktheAdvancedtab,thenclickontheEnvironmentbutton.ScrollthroughtheSystemVariableswindowuntilyoufindavariablenamed Path.ClicktheEditbutton(see Figure2.2).Addthe jdk\bin directorytothebeginningofthepath,usingasemicolontoseparatethenewentry,likethis:
jdk\bin;otherstuff
Figure2.1.LaunchingthesystempropertiesdialoginWindowsVista
Figure2.2.Settingthe Path environmentvariableinWindowsVista
Becarefultoreplace jdk withtheactualpathtoyourJavainstallation,suchas c:
\jdk1.7.0_02.IfyouignoredouradvicetodroptheProgramFiles directory,enclosetheentirepathindoublequotes:
"c:
\ProgramFiles\jdk1.7.0_02\bin";otherstuff.
Saveyoursettings.Anynewconsolewindowsthatyoustartwillhavethecorrectpath.
Hereishowyoutestwhetheryoudiditright:
Startashellwindow.Typetheline
javac-version
andpresstheEnterkey.Youshouldgetadisplaysuchasthisone:
javac1.7.0_02
Ifinsteadyougetamessagesuchas“javac:
commandnotfound”or“Thenamespecifiedisnotrecognizedasaninternalorexternalcommand,operableprogramorbatchfile”,thenyouneedtogobackanddouble-checkyourinstallation.
Note
InWindows,followtheseinstructionstoopenashellwindow.IfyouuseWindowsXP,selecttheRunoptionfromtheStartmenuandtype cmd.InVistaandWindows7,simplytype cmd intotheStartSearchfieldintheStartmenu.PressEnter,andashellwindowappears.
Ifyou’veneverseenoneofthese,wesuggestthatyouworkthroughatutorialthatteachesthebasicsaboutthecommandline,suchas
2.1.3.InstallingtheLibrarySourceandDocumentation
ThelibrarysourcefilesaredeliveredintheJDKasacompressedfile src.zip,andyoumustunpackthatfiletogetaccesstothesourcecode.Wehighlyrecommendthatyoudothat.Simplydothefollowing:
1. MakesuretheJDKisinstalledandthatthe jdk/bin directoryisontheexecutablepath.
2. Openashellwindow.
3. Changetothe jdk directory(e.g., cd/usr/local/jdk1.7.0 or cdc:
\jdk1.7.0).
4. Makeasubdirectory src:
mkdirsrc
cdsrc
5. Executethecommand
jarxvf../src.zip
(or jarxvf..\src.zip onWindows).
Tip
The src.zip filecontainsthesourcecodeforallpubliclibraries.Toobtainevenmoresource(forthecompiler,thevirtualmachine,thenativemethods,andtheprivatehelperclasses),goto .
ThedocumentationiscontainedinacompressedfilethatisseparatefromtheJDK.YoucandownloadthedocumentationSimplyfollowthesesteps:
1. MakesuretheJDKisinstalledandthatthe jdk/bin directoryisontheexecutablepath.
2. Downloadthedocumentationzipfileandmoveitintothe jdk directory.Thefileiscalled jdk-version-apidocs.zip,where version issomethinglike 7.
3. Openashellwindow.
4. Changetothe jdk directory.
5. Executethecommand
jarxvfjdk-version-apidocs.zip
where version istheappropriateversionnumber.
2.1.4.InstallingtheCoreJavaProgramExamples
Youshouldalsoinstallthe CoreJava programexamples.Youcandownloadthemfrom Theprogramsarepackagedintoazipfile corejava.zip.Youshouldunzipthemintoaseparatedirectory—werecommendyoucallit CoreJavaBook.Herearethesteps:
1. MakesuretheJDKisinstalledandthe jdk/bin directoryisontheexecutionpath.
2. Makeadirectory CoreJavaBook.
3. Downloadthe corejava.zip filetothatdirectory.
4. Openashellwindow.
5. Changetothe CoreJavaBook directory.
6. Executethecommand
jarxvfcorejava.zip
2.1.5.NavigatingtheJavaDirectories
InyourexplorationsofJava,youwilloccasionallywanttopeekinsidetheJavasourcefiles.And,ofcourse,youwillneedtoworkextensivelywiththelibrarydocumentation. Figure2.3 showstheJDKdirectorytree.
Figure2.3.Javadirectorytree
ThetwomostusefulsubdirectoriesforlearningJavaare docs and src.The docs directorycontainstheJavalibrarydocumentationinHTMLformat.Youcanviewitwithanywebbrowser,suchasFirefox.
Ti
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- CORE JAVA 9Chapter Chapter