View
227
Download
1
Embed Size (px)
Citation preview
1交通大學資訊工程學系
How To Java?• 使用 JDK
– Microsoft Windows– FreeBSD / Linux
• 使用 Java IDE ( 注意 ! 很多仍需 JDK 配合 )– VisualAge for Java (IBM) (Eclipse 前身 )– Eclipse ( 蝕 ; 蓋過 ) – IntelliJ IDEA (JetBrains)– JBuilder (Borland/Codegear)– JDeveloper (Oracle ; technet.oracle.com) (JDeveloper 是 JBuilder 補充改寫的 )– JCreator, Forte for Java, Sun Java Studio, Sun NetBeans, …– Visual J++ (Microsoft), Visual J# (J Sharp), Visual Studio– Visual Café (WebGain, Symantec+BEA; closed at 2002)
2交通大學資訊工程學系
Write Java program , you need…
1. 用編輯器 (editor) 寫 Java 程式2. 用編譯器 (compiler) 把 Java 程式轉成 Java b
ytecode ,也就是 class 檔3. 用解譯器 (Interpreter) 來執行 Java 的 class
檔4. 用除錯器 (debugger) 來協助找出程式的問題
Sun 提供一套 Java 程式發展工具集,稱為 JDK (Java Development Kit) ,具有 2,3,4 的功能。
同時具有上述 4 項功能者,稱為整合發展環境 (IDE, Integrated Development Environment)
3交通大學資訊工程學系
Download and install JDK• http://java.sun.com• Download the standard version(J2SDK/Java SDK)• Simply double click and then follow the instructions• Configure / prepare your Develop Environment
– Set correct command PATH before starting– Batch file / Short cut / Edit System environment variable
• Download Java Documentation/Reference manual• Java is good for GUI + Event driven programming • GUI, Graphical User Interface
– A way for the computer and the user to communicate via graphics (pictures) on the screen
4交通大學資訊工程學系
JDK Installed Directory Tree• The JDK has the directory structure shown below.
jdk_<version> _______________________|______________________ | | | | | | | | | | | | | bin lib | demo || | | LICENSE | | | jre| | COPYRIGHT … … | __|__| README.txt include | |
readme.html bin lib
Sun 把 JDK 1.5 改名 5.0; 所以 JDK 1.6.1 就是 JDK 6.1
5交通大學資訊工程學系
JDK 的基本組成份子/bin Java binary programs
/docs Java documents
/jre/bin Run-time 用的 binary programs
/jre/lib 或 /lib
Run-time library
重要的是 /jre/lib/rt.jar 這個檔案早期版本是 /lib/classes.zip
6交通大學資訊工程學系
JDK Versions ( 版本 )• Java 1.1
– jdk 1.1.8_16 and older versions
• Java 2 ( 別搞混 Language version 與 JDK version)– Standard Edition (J2SE)
• j2sdk 1.2 ( 主要加入 Swing )• j2sdk 1.3.1_14• j2sdk 1.4.2_06• j2sdk 1.5.xx (5.0); 1.6 (6.0), 1.7(7.0), …
– Enterprise Edition (J2EE)– Micro Edition (J2ME)
Sun 自 JDK1.6 (6.0) 開始已改稱 Java 不再稱 Java 2
JDK 1.5 (5.0) 開始有支援 Generic type ( 即 template)
7交通大學資訊工程學系
JDK vs. JRE• JRE (Java Runtime Environment)
– 用來執行 Java 程式• JDK (Java Development Kit)
– 用來開發、測試或執行 Java 程式– 已經包含 JRE 了
• 使用 JDK 開發程式,就得有相對應版本的 JRE 來執行。– 使用 j2sdk 1.5 開發,那必須要有 jre 1.5 以上版本
才能執行。 (JDK 版本 1.5 就是 5.0)
8交通大學資訊工程學系
Types of Java programs• Java Application
– Standalone program
• Java Applet– 搭配 HTML 網頁
• Java Servlet– 放在網站且直接會在網站上執行
• JSP -- Java Server Page ( 類似 ASP, PHP)– HTML 網頁內夾著片片段段的 Java 程式
• Java MIDLet -- 在手機或手持裝置• Java ???Let
9交通大學資訊工程學系
CLASSPATH 說明• 使用 java, javac 等工具時,如果沒有指
定 classpath 的話,會自動使用環境變數CLASSPATH 所指定的路徑。
• CLASSPATH 中可以指定目錄、 zip 檔、jar 檔,用分號隔開,例: .;C:\MyLIB;C:\JDK\jre\lib\rt.jar用點 . 表示目前所在目錄
10交通大學資訊工程學系
Microsoft Windows 2000/XP/…
1. 先到 Sun 的網站去抓 JDK ,如: http://java.sun.com
2. 假設把 JDK 安裝在 C:\JDK3. 控制台 > 系統 > 進階 > 環境變數4. 找到變數 PATH ,加入 C:\JDK\bin 找到變數 CLASSPATH ,加入 .;C:\JDK\jre\lib\rt.jar
16交通大學資訊工程學系
JDK 中的 Tools
這些 tools 都擺在 JDK 的 \bin 裡面:• javac.exe : Java compiler ( 編譯器 )
• java.exe : Java interpreter ( 解譯器 )
• appletviewer : Java applet viewer
• javap : Class file disassembler ( 反組譯 )
17交通大學資訊工程學系
Java Application
•編譯:javac MyTest.java
•執行:java MyTest
•反組譯:javap MyTest
注意執行與反組譯都不要打
.class 因為那是 class 名稱不是檔案名稱 !
18交通大學資訊工程學系
Java Applet• 編譯:
javac MyApplet.java• 執行:
Win XP 後直接打 MyPage.htm appletviewer MyPage.htm 放網站後用瀏覽器看 MyPage.htm
<HTML><BODY><applet code=MyApplet.class
name=MyAppletwidth=320height=200>
</applet></body></html>
19交通大學資訊工程學系
編碼 (Encoding) 的問題
• 如果程式印出亂碼,那可能是編譯時,沒有指定正確的內部編碼,試試這個:– javac -encoding BIG5 MyTest.java
•常用 encoding– BIG5– MS950– UNICODE– UTF8
20交通大學資訊工程學系
如何知道系統預設編碼?
// FileEncoding.java -- find the default encodingpublic class FileEncoding{ public static void main(String[] args) { System.out.println(System.getProperty("file.encoding")); }}
D:\jtest> javac FileEncoding.java
D:\jtest> java FileEncoding
22交通大學資訊工程學系
有很多檔案要編譯時• 使用 makefile (example in next slide)
– Makefile 是一個文字檔 , 描述執行檔要由哪些機器碼檔 (object file) 組成 , 各 object file 與那些原始檔(source file) 和標題檔 (header file) 有關 , 以及要使用什麼命令產生 object file
• 使用 Ant (用 google.com 找找)– http://ant.apache.org– 以前是 Apache 的 Jakarta 計畫下的子計畫– Apache 軟體基金會 (ASF) 正式成立於 1999 年 (see wikipedia)
• 使用 IDE 來開發 (with project 專案 )– 有些 IDE 有提供 PRJ2MAK 工具可以把 project 檔案轉換成
makefile
23交通大學資訊工程學系
Unix 下的 makefile 範例.SUFFIXES: .class .java
JAVAC=/usr/local/jdk1.4.1/bin/javac -encoding Big5 -deprecation -classpath .:/usr/local/jdk1.4.1/jre/lib/rt.jar
#JAVAC=/usr/local/jdk1.4.1/bin/javac -encoding Big5 -deprecation -classpath ..:/usr/local/jdk1.4.1/jre/lib/rt.jar
#JAVAC=/usr/local/jdk1.4.1/bin/javac -encoding Big5 -deprecation -classpath ../..:/usr/local/jdk1.4.1/jre/lib/rt.jar
all: FileEncoding.classcd test; make
.java.class:$(JAVAC) $<
clean:rm -f *.classchmod 644 *.java makefile
24交通大學資訊工程學系
dosMake /make/nmake• http://java.csie.nctu.edu.tw/~icwu/classes/java/doc/dosMake.zip• 所有檔案拷貝到 JDK 安裝目錄的 bin 裡面,就可以共用 PAT
H 設定,使用 make 來編譯、執行程式• 修改 make.batset JAVASDK_HOME=C:\j2sdk1.4.2\binset JAVAC=%JAVASDK_HOME%\javac.exeset JAVAC_OPTION=-classpath ".;%JAVASDK_HOME%\jre\lib\rt.jar"set JAVA=%JAVASDK_HOME%\java.exeset JAVA_OPTION=-classpath ".;%JAVASDK_HOME%\jre\lib\rt.jar"rem default make action, could be "new", "all", or "clean"set DEFAULT_ACTION=allrem default main class for executing.rem Leave it blank if you don't want to run your program after make.set DEFAULT_MAIN_CLASS=
25交通大學資訊工程學系
Using IDEs• An IDE is an Integrated Development Environment
• All professional IDEs are similar
• Different IDEs meet different needs– BlueJ, DrJava are designed as teaching tools
• Emphasis is on ease of use for beginners
• Little to learn, so students can concentrate on learning Java
– Eclipse, JBuilder, NetBeans are designed as professional-level work tools
• Emphasis is on supporting professional programmers
• More to learn, but well worth it in the long run
想要節省時間 , 必須先花一些時間好好學習使用工具
26交通大學資訊工程學系
Java IDEs on Windows
1. Microsoft Visual J++ / Visual Studio2. Borland(CodeGear) JBuilder: http://www.borland.
com/products/downloads/download_jbuilder.html
3. Eclipse: http://www.eclipse.org/downloads/index.php
4. JetBrains IntelliJ IDEA:(30 天試用 ) http://www.jetbrains.com/idea/download/index.html
Ps. 後三者均有 Unix 、 Mac 的對應版本
27交通大學資訊工程學系
IDE Benefit
使用 IDE 環境,寫程式會更為方便,比起純粹使用 JDK 來說,主要有下列優點:– 「寫程式、編譯、測試」一套軟體就可搞定– Look for help: 快速 API 說明– Check syntax: 自動文法檢查– Reformat code: 自動排版 , 程式結構井然一致– Debug/test code: 容易除錯– Generate code: 快速產生程式– Navigate code: 快速瀏覽程式– Refactor code: 快速重整程式
28交通大學資訊工程學系
A good IDE should .. (1/2)• Include a “Syntax aware” text editor
– different tokens can be colored differently• for catching runaway comments or strings, for
example
– brace{ }, bracket[ ], and parentheses ( ) matching
• whenever a closing brace, bracket or parenthesis is typed the editor shows you the corresponding opening brace, bracket, or parenthesis
• clicking on brace, bracket, or parenthesis will show the corresponding block
29交通大學資訊工程學系
A good IDE should .. (2/2)
• Simplicity– powerful development environment, but the
power is hidden– we do not use most of what is there
• Debugger is useful and generally intuitive– helpful to the students– useful for demonstrations
• Support for the standard libraries
30交通大學資訊工程學系
Debug functions
• Break point :程式執行到中斷點時會暫停
• Step :一行一行追蹤程式• Step into :遇到函式時,進去追蹤• Step over :遇到函式時,不進去追蹤• Watch :觀察變數值、修改變數值
交通大學資訊工程學系
Eclipse
Reference: http://www.eclipse.org/eclipse/presentation/eclipse-slides.ppt
Eclipse (1/58)
33交通大學資訊工程學系
Eclipse.org Eclipse 3.x
• 強悍的開放原始碼軟體( $0 ),可用來開發多種語言的程式( Java, C/C++, … )
• 需要另外安裝 JDK• Pros( 優點 ) :多種程式語言開發環境• Cons( 缺點 ) :各類 Plugin 的安裝設定稍複雜
• Eclipse 指蝕,如日蝕( solar eclipse )、月蝕( lunar eclipse )
Eclipse (3/58)
34交通大學資訊工程學系
Eclipse 中文化 (1/2)
• 英文不是很爛的就不必中文化啦 !• 到 www.eclipse.org 下載後直接將檔案解壓縮至 C:\ 或其他目錄,會自動產生 eclipse 的子目錄
• 到 www.eclipse.org 搜尋中文套件 Language Pack 。• 下載最新的中文套件檔案 NLpack1 。 ( 網站內搜尋 )
– NLpack1-eclipse-SDK-3.2-win32.zip– Around 6.31 MB current version
• 將下載中文套件檔案解壓縮,可以得到 features 目錄和plugins 目錄。將該兩個目錄複製到 eclipse 目錄下覆蓋原有的子目錄。
• 重新開啟 Eclipse ,就成繁體中文版了。
Eclipse (4/58)
35交通大學資訊工程學系
Eclipse 中文化 (2/2)
• 若要啟動不同環境,可在 DOS 介面下輸入:– 英文
• eclipse.exe -nl "zh_US"
– 繁體• eclipse.exe -nl "zh_TW"
– 簡體• eclipse.exe -nl "zh_CN"
• 或是建立 eclipse 的捷徑後,複製多個捷徑並在它們內容的捷徑項目中的目標位置,修改其啟動時的參數 :
• eclipse.exe -nl "zh_US"
• eclipse.exe -nl "zh_TW"
• eclipse.exe -nl "zh_CN"
35
Eclipse (5/58)
36交通大學資訊工程學系
Eclipse Setup again
• 安裝外掛 Jigloo SWT/Swing GUI Builder for Eclipse
– 讓 Eclipse 可以像 JBuilderX 一樣使用拖拉方式建構 GUI 的外掛
– http://cloudgarden.com/jigloo/index.html
36
Eclipse (6/58)
37交通大學資訊工程學系
Eclipse Terminology
• Workbench 工作台 ; 工作桌面 ; 工作窗– 可以打開多個工作窗 Window >> new Window
– 工作窗中的各工作格可移動也可調整大小或關掉– 每個工作格的右上角可點按縮小或放大– 弄亂了可以點按 Window >> Reset Perspective 恢復
• Workspace 工作目錄 ; 工作空間– 可有多個工作目錄
Eclipse (7/58)
38交通大學資訊工程學系
Eclipse Workbench ( 工作台 )
• Represents the desktop development environment– It contains set of tools for resource management– It provides common way of navigating through the
resources
• Multiple workbenches can be opened at the same time
Eclipse (8/58)
39交通大學資訊工程學系
Eclipse Workspace ( 工作空間 )
• Represents users data• It is a set of user defined resources
– Files• Contain arbitrary number of bytes
– Folders• Contain other folders or files
– Projects• Collections of files and folders
Eclipse (9/58)
40交通大學資訊工程學系
Download Eclipse
• http://www.eclipse.org• Main point for finding Eclipse resources
– Downloads
– Articles
– Discussion groups
– Bugs report
• Contains various resources for using Eclipse• Contains references to other Eclipse related sites
Eclipse (10/58)
41交通大學資訊工程學系
Running Different Workspace …
• It is possible to run different workspaces using command prompt– -data argument must be used with eclipse.exe– Workspace location must be specified
• Useful for grouping project specific data
• Multiple workspaces can run at the same time
Eclipse (11/58)
42交通大學資訊工程學系
… Running Different Workspace
• Customize your working directory by creating a shortcut identifying the eclipse.exe and the working directory
Eclipse (12/58)
43交通大學資訊工程學系
啟動 Eclipse• 可點擊之前建好的捷徑 , 也可在 cmd mode 直接下 eclipse 命
令• 或是寫一支 ggyy.bat ,啟動 Eclipse 時直接指定 JDK 的位置 ,
ggyy.bat 檔案內容如下例 : ( 注意你的 VM)start eclipse -vm c:\jdk1.5.0_05\jre\bin\javaw -vmargs -Xmx256M
• 剛啟動就要設定 Project 的存放位置
Eclipse (13/58)
44交通大學資訊工程學系
Workbench Terminology
Tool bar
PerspectiveandFast Viewbar
ResourceNavigatorview
Stackedviews
Propertiesview
Tasksview
Outlineview
Bookmarksview
Menu bar
Messagearea
EditorStatusarea
Texteditor
Eclipse (14/58)
46交通大學資訊工程學系
Workspace 工作目錄• You can have multiple workspace (working directory)• Specify your workspace when starting Eclipse• You can also switch workspace in Eclipse:
File >> Switch Workspace
Eclipse (16/58)
47交通大學資訊工程學系
New a Project• File > New > Project
– 選取檔案 File→ 新建 New→ 專案 Project– 剛啟動要設定 Project 的存放位置 ; 不改的話會存在安裝目
錄的 workspace 中。• 選取 Java Project 專案,按 Next 下一步。
– Project layout 有兩個選項,差別在於第一個會將原始碼與編譯好的程式碼放置在專案目錄下,另一個則是分開放在 src 與 bin 資料夾內
• 輸入專案名稱 Project name ,例如 JavaTest ,按 Next 下一步。
• 選取程式庫 Libraries 設定 JDK ,按 完成 Finish 。• 建立完 JavaTest 專案後,會自動到 Java 顯示視窗。
Eclipse (17/58)
57交通大學資訊工程學系
Run your Java program
點按 Run ( 執行 ) → Run as ( 執行成 ) → Java Application (Java 應用程式 ) 。
這時下面的主控台就會顯示執行結果。
確定你選定有 main() 的 Java 檔
也可以用除錯模式 Debug mode, see next slides..
Eclipse (27/58)
64交通大學資訊工程學系
JDT : Java Development Tools
• State of the art Java development environment
• Included in Eclipse Project releases– Available as separately installable feature
– Part of Eclipse SDK drops
• Built atop Eclipse Platform– Implemented as Eclipse plug-ins
– Using Eclipse Platform APIs and extension points
Eclipse (34/58)
65交通大學資訊工程學系
Java Perspective (1/3)• Java-centric view of files in Java projects
– Java elements meaningful for Java programmers
Javaproject
package
class
field
method
Javaeditor
Eclipse (35/58)
66交通大學資訊工程學系
Java Perspective (2/3)• Browse type hierarchies
– “Up” hierarchy to supertypes– “Down” hierarchy to subtypes
Typehierarchy
Selectedtype’s
members
Eclipse (36/58)
67交通大學資訊工程學系
Java Perspective (3/3)• Search for Java elements
– Declarations or references– Including Libraries and other projects
Hitsflaggedin marginof editor
All search results
Eclipse (37/58)
69交通大學資訊工程學系
Java Editor (2/7)• Method (function) completion in Java editor
List of plausible methods Doc for method
Eclipse (39/58)
70交通大學資訊工程學系
Java Editor (3/7)• On-the-fly spell check catches errors early
Preview
Clickto seefixes
ProblemQuickfixes
Eclipse (40/58)
71交通大學資訊工程學系
Java Editor (4/7)
• Code templates help with drudgery
Statementtemplate Preview
Eclipse (41/58)
72交通大學資訊工程學系
Java Editor (5/7)
Method stub insertionfor inherited methods
Method stub insertion for anonymous inner types
• Java editor creates stub methods
Eclipse (42/58)
73交通大學資訊工程學系
Java Editor (6/7)
Variable namesuggestion
Argument hints andproposed argumentnames
JavaDoccode assist
• Java editor helps programmers write good Java code
Eclipse (43/58)
74交通大學資訊工程學系
Java Editor (7/7)
• Other features of Java editor include– Local method history– Code formatter– Source code for binary libraries– Built-in refactoring
Eclipse (44/58)
76交通大學資訊工程學系
Refactoring (2/4)
• Refactoring actions rewrite source code– Within a single Java source file
– Across multiple interrelated Java source files
• Refactoring actions preserve program semantics– Does not alter what program does
– Just affects the way it does it
• Encourages exploratory programming• Encourages higher code quality
– Makes it easier to rewrite poor code
Eclipse (46/58)
77交通大學資訊工程學系
Refactoring (3/4)
• Full preview of all ensuing code changes– Programmer can veto individual changes
List of changes
“before” vs. “after”
Eclipse (47/58)
78交通大學資訊工程學系
Refactoring (4/4)
• Growing catalog of refactoring actions– Organize imports– Rename {field, method, class, package}– Move {field, method, class}– Extract method– Extract local variable– Inline local variable– Reorder method parameters
Eclipse (48/58)
79交通大學資訊工程學系
Eclipse Java Compiler
• Eclipse Java compiler– JDK-compliant Java compiler – Helpful error messages– Generates runnable code even in presence of errors– Fully-automatic incremental recompilation– High performance– Scales to large projects
• Multiple other uses besides the obvious– Syntax and spell checking– Analyze structure inside Java source file– Name resolution– Content assist– Refactoring– Searches
Eclipse (49/58)
80交通大學資訊工程學系
Eclipse Java Debugger (1/2)• Run or debug Java programs
Threads and stack
frames
Editor with breakpoint
marks
Console I/O
Local variables
Eclipse (50/58)
81交通大學資訊工程學系
Eclipse Java Debugger (2/2)• Run Java programs
– In separate target JVM (user selectable)– Console provides stdout, stdin, stderr– Scrapbook pages for executing Java code snippets
• Debug Java programs– Full source code debugging– Any JPDA-compliant JVM
• Debugger features include– Method and exception breakpoints– Conditional breakpoints– Watchpoints– Step over, into, return; run to line– Inspect and modify fields and local variables– Evaluate snippets in context of method– Hot swap (if target JVM supports)
Eclipse (51/58)
82交通大學資訊工程學系
Eclipse is a Java IDE• Widely regarded as the Java development environment• With all the bells and whistles…
– Language-aware editors, views, …– Refactoring support– Integrated unit testing and debugging– Incremental compilation and build– Team development
support• Out of the box
support for CVS
– …
Eclipse (52/58)
83交通大學資訊工程學系
Eclipse is an IDE Framework• Eclipse + JDT = Java IDE
– First class framework for Java– Language aware editor– Incremental build– Integrated debugging
• Eclipse + CDT = C/C++ IDE– First class framework for C/C++– Language aware editor– Refactoring, search
• Eclipse + PHP = PHP IDE• Eclipse + JDT + CDT + PHP = Java, C/C++, PHP
IDE• …
Eclipse (53/58)
84交通大學資訊工程學系
Eclipse is a Tools Framework• Extensibility through OSGi implementation
– Plug-ins make Eclipse whatever you need it to be
• Focus on developing a universal platform of frameworks and exemplary tools
• Tools extend the Eclipse platform using plug-ins– Business Intelligence and Reporting Tools (BIRT)
– Eclipse Communications Framework (ECF)
– Web Tools Project (WTP)
– Eclipse Modelling Framework (EMF)
– Graphical Editing Framework (GEF)
– Test and Performance Tooling Project (TPTP)Plug-in
Plug-in
Plug-in
Eclipse (54/58)
85交通大學資訊工程學系
Eclipse is an Application Framework
• Remove the IDE elements, Java language support, team development support, … and you’re left with a pretty comprehensive general application framework– Support for multiple platforms
• Linux, Windows, Mac OSX, UNIX, embedded
– Rich widget set, Graphics
– Native-OS integration (drag and drop, OLE/XPCOM integration)
• A platform for rich clients• Some examples of this later…
Eclipse (55/58)
86交通大學資訊工程學系
Eclipse Resources• http://www.eclipse.org
– In Eclipse terms, a “drop” is a build and the “platform” is another name for the software… download the Platform and you get Eclipse. Don’t get the SDK unless you know you want it
• http://www.cfeclipse.org– Wiki, how-to, instructions, etc.
• http://www.myeclipseide.org– Excellent set of tools to flesh out Eclipse
• http://labs.macromedia.com– FlexBuilder2 and RDS plugin
• http://www.dopefly.com/pages/cfeclipse.cfm– WIDE variety of info, how-to, and articles on Eclipse, CFE, plugins, etc.
• http://caterpillar.onlyfun.net/Gossip/JavaGossip-V1/JavaGossip.htm
• The Java Tutorials http://java.sun.com/docs/books/tutorial/
Eclipse (56/58)
87交通大學資訊工程學系
Eclipse Plug-in Architecture
Tool(plug-in)
Eclipse Platform
Platform Runtime
…
Tool(plug-in)
Tool(plug-in)
Workbench
Workspace
Help
Team
Eclipse (57/58)
88交通大學資訊工程學系
IBM’s IDE History
1996VisualAge for Java
2001Eclipse Project
1994VisualAge for Smalltalk
VisualAge Micro Edition
Open Source project
Eclipse (58/58)
91交通大學資訊工程學系
JetBrains IntelliJ IDEA 4.5.4
• 介面最親切、獲獎無數的 IDE
• 需要另外安裝 JDK
• 優點:最貼心的開發環境• 缺點:硬體要求高,售價很貴( $499 ) ( 試用版可用 30 天 )
IntelliJ (1/18)
106交通大學資訊工程學系
設定要執行的 class 檔 (IntelliJ IDEA)
這裡要指定 source 的位置
一定要選擇
含有 main() 的 class
按這個 + 新增
IntelliJ (16/18)