Download Java

Document related concepts
no text concepts found
Transcript
CS340100
JAVA Programming Language
Ch.1 Overview and Installation of
JAVA
[email protected]
1
Outline
• Overview of Java
• Java Environment Setup
• Android Environment Setup
[email protected]
2
History of Java
Year
Note
1991
Green project 推出 Oak語言
1995
Sun命名為Java
1996
Java 1.0
1998
Java 1.2, 此時將Java更名為Java2
1999
三個版本: J2SE, J2EE, J2ME
2005
取消Java2, 一律以Java通稱, 並將Java 1.5更名為Java5
2006
推出Java SE 6.0 (JDK 6), 目前最穩定的版本
2008
採用Java的第一支Android手機上市
2009
Sun被Oracle收購
2011
推出Java 7, 本課程以此版本授課 (2012被抓到安全漏洞)
[email protected]
3
Edition of Java
• Java Standard Edition (Java SE)
標準版
• 適用於開發用戶端程式
• Java Enterprise Edition (Java EE)
企業版
• 適用於開發伺服器端程式
• Java Micro Edition (Java ME)
手持設備版
• 適用於開發手機、無線設備程式
[email protected]
4
Compiler and Interpreter of Java
• Compiler(編譯器): 將Java原始碼編譯成bytecode
• Interpreter(直譯器): 載入並執行bytecode的內容
[email protected]
5
Portability of Java
• 一個編譯好的Java bytecode(位元檔), 可以跑在
各種硬體架構上, 前提是這些架構有對應的直
譯器
Java所用的直譯器稱為Java虛
擬機(Java Virtual Machine)
[email protected]
6
JRE & JDK
• JRE (Java Runtime Environment)
– Java虛擬機, 讀入Java bytecode並
執行程式
• JDK (Java Development Kit)
– 給Java開發者使用的工具包, 包含
了compiler與Java虛擬機
[email protected]
7
Java on Android
• Android 基於Java 6 ME
– 額外提供Android特有的Java library,
像是touch event, activity等等
• Android採用自己設計的直譯器
– Dalvik Virtual Machine
[email protected]
8
Java Environment Setup
CS340100
JRE
• The Java SE Runtime Environment (JRE) contains the
Java virtual machine, runtime class libraries, and Java
application launcher that are necessary to run
programs written in the Java programming language.
JDK
• The Java SE Development Kit (JDK) includes tools
useful for developing and testing programs written in
the Java programming language and running on the
Java platform.
JRE vs JDK
Installation
• First, google “java Oracle” !
Installation
• Click “Download Java for Developer”
Installation
• Click “Java SE (includes JavaFX)”
Installation
• Click “Java Platform (JDK) 7u11”
Installation
• Click “Accept License Agreement”, then choose the
download link depends on your OS type.
Installation
• Double click the file you download, choose “Next”
Installation
• Just click “Next”
Installation
• Wait a second
Installation
• Click “Close”
Installation
• Select Computer from the Start menu
• Right click on “Computer”
• Choose “Properties”
Installation
• Click “Advanced system settings”
• Choose “Advanced tab”
Installation
• Click “Environment Variables”
Installation
• Click “PATH”
• Click “Edit”(If “PATH” does not exist, click Add)
Installation
• Add “C:\Program Files (x86)\Java\jdk1.7.0_11\bin” to value
• If there already exists a previous value, don’t forget to add
“;” before you add the new one.
• Click “ok” to complete setting
Installation
• Open Start menu
• Type “cmd”
• Press “Enter”
Installation
• Type “java” and press “Enter”
• If everything works fine, usage of java will show
below the command
Installation
• Type “java -version” to check the version of Java
Runtime Environment
• In this tutorial, the version is “1.7.0_11”
Installation
• Type “javac” to check Java Compiler
• Type “javac -version” to check version
Java Compiler
Compile
HelloWorld.java
HelloWorld.c
javac
gcc
Execute
HelloWorld.class
HelloWorld.exe
OS
JVM
OS
First Java Program
• Open a text file and rename it to HelloWorld.java
• Copy the code below to it
Compile the Program
• Open cmd.exe
• Type “javac HellowWorld.java”
• Java compiler will create a file called “HelloWorld.class”,
which is the Java bytecode of this program
• A Java bytecode instructions is executed on the Java
virtual machine
Run the Program
• Type “java HelloWorld”
• The result will show on the screen
• Here is closer look to this program
Eclipse
• Eclipse is a commonly used Java integrated
development environment(IDE)
Get Eclipse
• Google it!
Get Eclipse
• Go to the download page
Get Eclipse
• Choose the Eclipse version base on java version
• 32 Bit for x86, 64 Bit for x64
Eclipse
• Choose the workspace
Eclipse
• Close the welcome message
Build Java Project
• Choose “File” -> “New”
• Click “Java Project”
Build Java Project
• Specify Project name
• Click “finish”
Add Class
• Right click on “src”
• Choose “New”
• Click “Class”
Add Class
• Choose “public static void main” for main Class
• Specify Class Name
• Click “Finish”
First Java Program with Eclipse
• If everything works fine, you may start your first Java
program with Eclipse.
First Java Program with Eclipse
• Type “System.out.print(“HelloWorld!”)” as previous
slides .
Run Java Program
• Click “Run” Button to run the program.
• The result will be shown at “Console”.
Eclipse Useful Commands
•
•
•
•
•
•
•
Ctrl + Shift + F = Reformat
Ctrl + Shift + M = Add import
Ctrl + Shift + O = Organize imports
Ctrl + / = Comment line of code
Ctrl + D = Delete line of code
Alt + / = Show template proposal
Ctrl + Shift + G = Find references in workspace
Eclipse Debugger
• If you want to debug a Java program, you may
choose “Debug” mode in the upper right corner
Eclipse Debugger
• For example, the code below will have a “divide by
zero” bug when “i == 0” that cause program failed.
Overview Debugger
Overview Debugger
Step Into (F5) : Debug line by line
Step Over (F6):Executes a method without entering it
in the debugger.
Step Out (F7) :Finishes the execution of the current
method and returns to the caller of this method.
Resume (F8):Execute the program code until is
reaches the next breakpoints
Example
• Toggle breakpoints
• Set Breakpoints arguments
Example
• Press “Debug HelloWorld”
Example
• Step in, Step out
• Check value of variables to Debug
Android App Development
Environment Setup
Outline
•
•
•
•
•
Install ADT
Download and setting SDK
Create emulator
Create Android project
DDMS
Install ADT
• Android Development Tools (ADT) is a plugin
for the Eclipse IDE that is designed to give you
a powerful, integrated environment in which
to build Android applications.
Install ADT
Help -> Install New Software
Install ADT
1
2 Choose
the target
address
3
1 check the software
you want to
download (Developer
Tool is required)
Install ADT
2
Install ADT
1
2
Outline
•
•
•
•
•
Install ADT
Download and setting SDK
Create emulator
Create Android project
DDMS
Download and setting SDK
• The Android SDK provides you the API libraries
and developer tools necessary to build, test,
and debug apps for Android.
• Download location :
– http://developer.android.com/sdk/index.html
Download and setting SDK
1
2 Choose either
link to download
and unpack/install it
Download and setting SDK
Download and setting SDK
1
2 Choose the path of
your sdk
3
Download and setting SDK
Download and setting SDK
1 check the Tools and api level you
want to download
2
Download the android api
Download and setting SDK
1
2
Outline
•
•
•
•
•
Install ADT
Download and setting SDK
Create emulator
Create Android project
DDMS
Emulator
• The Android SDK includes a mobile device
emulator — a virtual mobile device that runs
on your computer. The emulator lets you
develop and test Android applications without
using a physical device.
Create emulator
type name, device
Create emulator 2,and
api level
1
3
Create emulator
3 emulator show up
1
2
Outline
•
•
•
•
•
Install ADT
Download and setting SDK
Create emulator
Create Android project
DDMS
Create Android project
1
4 fill the app name
and Android version
you want to compile
2
3
5
Create Android project
Create Android project
src : 原始碼放置處
gen : 由eclipse自動生成的檔案
Android 4.0.3 : android系統library的位置
assets : 放置App所需檔案的位置
res :放置App所需檔案的位置,會在R.java中
產生對應的id。
AndroidManifest.xml : 用來描述 Android 應用
程式「整體資訊」的設定檔。
1 right click
Run on current device
2
3
4 app is executed
Outline
•
•
•
•
•
Install ADT
Download and setting SDK
Create emulator
Create Android project
DDMS
DDMS
• Android ships with a debugging tool called the
Dalvik Debug Monitor Server (DDMS), which
provides port-forwarding services, screen
capture on the device, thread and heap
information on the device, logcat, process,
and radio state information, incoming call and
SMS spoofing, location data spoofing, and
more.
DDMS : logcat
• The Android logging system provides a
mechanism for collecting and viewing system
debug output. Logs from various applications
and portions of the system are collected in a
series of circular buffers, which then can be
viewed and filtered by the logcat command.
You can use logcat from an ADB shell to view
the log messages.
DDMS : logcat
DDMS : logcat
1 import the required
library
2
Tag
Log content
3 The log shows up
Other
• What is Android ?
– Android(讀音:['ændrɔid],中文俗稱安卓)是
一個以Linux為基礎的半開放原始碼作業系統,
主要用於行動設備,由Google成立的Open
Handset Alliance(OHA,開放手機聯盟)持續
領導與開發中。
Other
• Android App life cycle
• http://code.google.com/
p/androidbmi/wiki/
LifeCycle