Standard Java Applications

advertisement
Module Road Map
1.
2.
3.
4.
5.
6.
Overview
Installing and Running
Building and Running Java Classes
Refactoring
Debugging
Testing with JUnit
7.
Version Control with Subversion
 Introduction
Lecture 4
 Terminology
 Adding a repository to Eclipse Workbench
 Sharing a project
 Checking out a project from repository
 Synchronizing with respository
v1.6
08/02/2006
1
The Problem of Version Control

Suppose two programmers are working on a
project.



(See http://svnbook.red-bean.com/en/1.4/svn-book.html#svn.basic)
They edit the same file.
Then they both save their changes.


What will happen?
One approach: Programmer P1 locks the file,
then unlocks it when done.

v1.6
What is wrong with this?
08/02/2006
2
Subversion (SVN)



An open-source based version-control system
Allows coordination of source code changes made by
individual developers working on a team
Client-server method lets developers access code from
anywhere there’s an internet connection.



Central server maintains history of all changes made to directory
trees, called repositories
Client machines have a copy of all files that developers are working
on.
The changes are then reported to the central server using a client
that can access and update repositories.
v1.6
08/02/2006
3
Subversion — What it does

Subversion operations

Checking out from a repository


Sharing a resource


Retrieves a directory from a Subversion repository to the Workbench on
local machine
Creates a copy of the resource on the Subversion repository
Synchronizing with a repository

Update


Commit

v1.6
Before you can commit changes, Subversion requires your sources to be in
sync with any changes committed by other developers.
To make the changes available to other developers, you must commit them
to the repository.
08/02/2006
4
Download Subversion
v1.6
08/02/2006
5
Installing Subversion
I chose the
latest
stable
version of
Windows
binary
v1.6
08/02/2006
6
Unzipping Subversion
v1.6
08/02/2006
7
Put Subversion in C:\Program Files
v1.6
08/02/2006
8
Install Subversive, the Eclipse Client
v1.6
08/02/2006
9
Installing Subversive
In Eclipse: Help > Software Upates > Find and Install
v1.6
08/02/2006
10
Search for Features of New Plug-in
v1.6
08/02/2006
11
Select New Remote Site
v1.6
08/02/2006
12
Fill in the Dialog Box



On the New Update Site
dialog enter the name of
the update site and URL.
Use the following values:
Name: Subversive
URL:
for Subversive 1.1.x:
http://www.polarion.org/projects/subversive/download/1.1
/update-site/
for Subversive 1.0.x:
http://www.polarion.org/projects/subversive/download/up
date-site/
Click on the OK button to store update site information.
v1.6
08/02/2006
13
Select Subversive

v1.6
08/02/2006
14
To start
searching
the update
sites, select
Subversive
and click on
the Finish
button.
Installing Subversive Features

You will then be
asked to select the
features to install.

It is safe to install
all features. (If you
want to save disk
space, see the
discussion at
http://www.polarion.org/ind
ex.php?page=installation&
project=subversive#notes
v1.6
08/02/2006
15
License Agreement

Accept the
terms of the
license
agreement.
v1.6
08/02/2006
16
Install Features

Then
install the
features.
v1.6
08/02/2006
17
Install Features, cont.

Install
the
feature,
despite
the
warning
.
v1.6
08/02/2006
18
Restart for Safety

I suggest you
restart Eclipse
v1.6
08/02/2006
19
Configure Subversive

After installation, choose Help > Software Upgrades >
Manage Configuration
v1.6
08/02/2006
20
Select Subversive Team Provider

Click to open “Eclipse SDK”; then select
SVN Team Provider.
v1.6
08/02/2006
21
Connecting with Subversive

Select Window > Open perspective > Other
v1.6
08/02/2006
22
Select “Subversion Repository Exploring”
v1.6
08/02/2006
23
Click “New Repository Location”
v1.6
08/02/2006
24
Select a Repository

Give the URL,
user-ID, and
password,
then click
“Finish”.
v1.6
08/02/2006
25
Expand the Repository
Click on the name of the repository to expand it
v1.6
08/02/2006
26
Check out the Project

Rightclick on
the name
of the
project to
check it
out.
v1.6
08/02/2006
27
Project Checked Out
The project at the top of the pane was checked out.
v1.6
08/02/2006
28
Modify a Class
v1.6
08/02/2006
29
Committing Changes
Save the file, then right-click on the filename and choose Team > Commit
v1.6 08/02/2006
30
Subversion

A conflict occurs when


you have locally modified a resource
for which a more recent revision is available


» Conflicts
in the branch in the repository.
3 methods to resolve conflicts:
 Discard local copy by overwriting it with the newer
version on the repository.
 Overwrite the copy on the repository with the local copy.


Use extreme caution since you will be erasing changes made by
others.
Merge your work and the repository resource, saving
locally the merged resource.
v1.6
08/02/2006
31
CVS

» Conflicts
Conflict types:
 An auto-mergeable conflicting change occurs when an
source-code file



has been changed both remotely and locally (i.e. has noncommitted local changes)
but the changes are in different lines.
A non-auto-mergeable conflicting change occurs when


one or more of the same lines of a source-code file have been
changed or
when a binary file has been changed both remotely and locally.

v1.6
Binary files are never auto-mergeable.
08/02/2006
32
Introduction to NetBeans v 5.0
Integrated Development
Environment
v1.6
08/02/2006
Lecture Overview






NetBeans Overview
Installing and Running NetBeans
NetBeans Projects
Building and Running Java Classes
Building Running Web Applications
Refactoring Applications
v1.6
08/02/2006
34
Lecture Road Map

NetBeans Overview







Introduction to NetBeans
Features
NetBeans Projects
Installing and Running NetBeans
Building and Running Java Classes
Building and Running Web Applications
Refactoring Applications
v1.6
08/02/2006
35
NetBeans Overview »
Introduction to NetBeans

A comprehensive, modular Java-based IDE (Integrated
Development Environment).

Free and open-source based.


Open source since June 2000
Large community of users and developers

Provides a platform for building rich-client Java applications, Web
applications, Web services etc.

Supports plug-in architecture. Large set of third-party modules (plugin’s) are included in the downloadable version.
v1.6
08/02/2006
36
NetBeans Overview »
Features

Ant-based project system







Apache Ant is a Java-based build tool used to
standardize and automate build and run environments
for development.
It is not necessary to know Ant to work with the IDE.
See http://ant.apache.org/resources.html for more
information on Ant.
Matisse GUI Builder - provides rich framework of
windows, menus, toolbars and actions.
Plug-in (module) and rich client application
development wizard.
CVS version control.
Enhanced Refactoring.
v1.6
08/02/2006
37
NetBeans Overview »
Features

Enterprise Development


Developer Collaboration





JBoss 4 and Weblogic 9 Support.
Discuss/share code with other developers remotely.
Conduct code review using chat capabilities.
Allow others to make changes which are then represented to the
rest of the group via visual cues.
Send messages in plain text, XML, HTML, or Java code format.
Application Profiling


CPU performance profiling
Memory profiling and memory leak detection
v1.6
08/02/2006
38
Lecture Road Map


NetBeans Overview
Installing and Running







Where to get NetBeans?
Installing NetBeans
Running NetBeans
NetBeans Projects
Building and Running Java Classes
Building and Running Web Applications
Refactoring Applications
v1.6
08/02/2006
39
Installing and Running NetBeans »
Where to get NetBeans ?

http:// www.netbeans.org provides
downloads, articles and tutorials related to
NetBeans.

IDE can be downloaded from
http://www.netbeans.info/downloads/index.php
v1.6
08/02/2006
40
Installing and Running NetBeans »
Installing NetBeans

NetBeans is available for Windows, MacOS, Linux and
Solaris operating systems.

Choose the NetBeans IDE 5.5.1 installer for the operating
system that is installed on your development system.

For Windows operating system, download the IDE 5.5.1
installer ‘netbeans-5_5_1-windows.exe’.

Prior to NetBeans installation, make sure JVM (Java
Virtual Machine) is installed. It is recommended that JDK
5.0 be downloaded from
http://java.sun.com/j2se/1.5.0/download.jsp and installed.

Execute the NetBeans self-extracting installer to start
NetBeans installation. Follow the install shield
instructions.
v1.6
08/02/2006
41
Installing and Running NetBeans »
Running NetBeans

On Windows
XP, choose
Start » All
Programs »
NetBeans 5.0 »
NetBeans IDE
to launch
NetBeans IDE.
v1.6
08/02/2006
42
Lecture Road Map



NetBeans Overview
Installing and Running NetBeans
NetBeans Projects





About Projects
Project Templates
Building and Running Java Applications
Building and Running Web Applications
Refactoring Applications
v1.6
08/02/2006
43
NetBeans Projects »
About Projects

A project is a group of source files and the settings with
which you build, run, and debug those source files.

The NetBeans IDE stores all of the information about a
project in an Ant script, a properties file, and a few XML
configuration files.

The IDE includes support for two types of projects:


Standard Projects: The IDE uses a NetBeans-generated Ant build
script to build, run, clean, test, and debug your application.
Free-form Projects: The IDE uses targets in an existing Ant script to
build, run, clean, test, and debug your application.
v1.6
08/02/2006
44
NetBeans Projects »
Project Templates

The IDE includes several project templates designed to support different types of
development including Web applications, general Java applications, and so forth.

Standard Templates: The IDE controls all source and classpath settings, compilation,
running, and debugging. The IDE generates an Ant script in which all project settings
are stored.

Standard Java Applications: Java Application, Java Class Library, Java Project
with Existing Sources.

Web Applications: Web Application, Web Application with Existing Sources.

Enterprise Applications: Enterprise Application, Enterprise Application with
Existing Sources.

EJB Modules: EJB Module, EJB Module with Existing Sources

NetBeans Plug-in Modules: Module Project, Module Suite Project, Library
Wrapper Module Project.

Free-Form Project Templates: The IDE relies on an existing Ant script for instructions
on how to compile, run, and debug applications.

Java Project with Existing Ant Script: A free-form Java project.

Web Project with Existing Ant Script: A free-form project containing a web
application.

EJB Module with Existing Ant Script: A free-form project containing an
Enterprise JavaBeans module.
v1.6
08/02/2006
45
Lecture Road Map




NetBeans Overview
Installing and Running NetBeans
NetBeans Projects
Building and Running Java Applications






Setting up a Project
Creating and Editing Java Source Code
Compiling, Running and Debugging a Project
Testing with JUnit
Building and Running Web Applications
Refactoring Applications
v1.6
08/02/2006
46
Java Applications »
Setting up a New Project

From the main menu,
choose
File » New Project.

This opens up the New
Project window.

In Categories pane, select
General.

In Projects pane, select
Java Application and click
Next.
v1.6
08/02/2006
47
Java Applications »
Setting up a New Project

Enter the name of the project
in the Project Name text box.

Choose a folder for the Project
Location.

Ensure that Set as Main Project
and Create Main Class
checkboxes are checked.

Enter the fully qualified name
of the main class (without the
.java extension) in the text
box adjacent to the Create
Main Class checkbox.
The class name may be
qualified with a package
name and a dot (for example,
mypackage.MainClass).

Click Finish.
v1.6
08/02/2006
48
Java Applications »
Setting up a New Project

The project will be
displayed in the Projects
window.

The Java file that contains
the skeleton code for the
main class opens in the
Source Editor.

Expand the project name
(a.k.a. project node) in the
Projects window to view the
Java files that belong to
the project.
v1.6
08/02/2006
49
Java Applications »
Setting up a New Project

Click on the Files
tab to view the
project’s file
structure in the
Files window.
v1.6
08/02/2006
50
Java Applications » Setting up a New Project »
Project Properties

Right-click on the Project node in
the Projects window.

This will open the Project Properties
window.

In Categories pane, click on Libraries.

Click Manage Platforms button to
specify a different JDK platform.

Compile-time and run-time class
paths may be specified using the
Compile tab and the Run tab
respectively.
v1.6
08/02/2006
51
Exercise 1

Create a New Java Application Project titled
EgApp with a main class titled MainClass in a
new package titled csc517.
v1.6
08/02/2006
52
Lecture Road Map




NetBeans Overview
Installing and Running NetBeans
NetBeans Projects
Building and Running Java Applications






Setting up a Project
Creating and Editing Java Code
Compiling, Running and Debugging a Project
Testing with JUnit
Building and Running Web Applications
Refactoring Applications
v1.6
08/02/2006
53
Java Applications »
Creating and Editing Java code

In this module, you will learn how to





Create a new class
Navigate a Project node
Add a new method to a class
Create a new field to a class
Use editing shortcuts
v1.6
08/02/2006
54
Java Applications » Creating and Editing Java code »
Creating a new Java class

To create a new Java class, right-click on
the project node in the Projects window
and choose
New » Java class.

The New Java Class window will be
displayed.

Enter the name of the new Java class in
the Class Name text box.

If the new class needs to be part of a
package, type the name of a new
package in the Package text box or
choose an existing package from the
drop-down list.

Click Finish.

The IDE generates the skeleton source
code of the new Java class and opens it
up in a new Java source window.
v1.6
08/02/2006
55
Java Applications » Creating and Editing Java code »
Navigating the project node

In the Projects window, expand
Source packages node under
the Project node to view the
package nodes and Java file
nodes.

Expand the Java file node to
view the classes defined in the
Java file.

Double-click on the Java file to
open the Java file in the
Source editor.

Expand the class node to view
the fields, constructors and
methods of the class.
v1.6
08/02/2006
56
Java Applications » Creating and Editing Java code »
Adding a new method to a class

Right-click on the Methods node
of the class and click Add Method
to create a new Java method.

This opens up the Add New
Method window.

The Name, Return Type, Access,
Modifiers and Parameters of the
new method may be specified in
the Add New Method window.
v1.6
08/02/2006
57
Java Applications » Creating and Editing Java code »
Adding a new field to a class

Right-click on the Fields node of
the class and click Add Field to
add a new field.

This opens up the Add New Field
window.

The Name, Type, Access,
Modifiers and Initial Value of the
field may be specified in the Add
New Field window.
v1.6
08/02/2006
58
Java Applications »
Creating and Editing Java Source
Editing Shortcuts:

Ctrl+Shift+F: Reformat the whole source code
window.

Ctrl+Space: Code-completion box. (The IDE
offers code completion for all classes and
methods in the project's compilation classpath).
v1.6
08/02/2006
59
Exercise 2





Create a new class titled NewClass and add it to the package csc517 in the
project EgApp you have created in Exercise 1.
Add a private integer field (member variable) mVar to the class NewClass.
Change the default constructor to accept integer parameter to initialize the
private integer field mVar.
Add a public method printField that takes no parameters and returns void.
Add code to the printField method to print the value of the private variable
mVar to standard output. (Hint: To use code-completion feature for the print
routine, type “sys” and hit Ctrl-Space. The IDE pops-up the code completion box
with a list of methods. Choose System.out.println() print routine).
v1.6
08/02/2006
60
Lecture Road Map





NetBeans Overview
Installing and Running NetBeans
NetBeans Projects
Refactoring Applications
Building and Running Java Applications





Setting up a Project
Creating and Editing Java Source Code
Compiling, Running and Debugging a Project
Testing with JUnit
Building and Running Web Applications
v1.6
08/02/2006
61
Java Applications »
Compiling, Running and Debugging a project

In this module, you will learn how to



Build a Java project
Run a Java project
Debug a Java project
v1.6
08/02/2006
62
Java Applications » Compiling, Running and Debugging a Project »
Project

Choose Build » Build Main Project (F11)
from the main menu to build (compile) a
project.

Choose Build » Clean and Build Main
Project (Shift+F11) from the main menu
to clean and build a project

Alternatively, right-click on the project
node in the project window and click
Build Project or Clean and Build Project
from the contextual menu.

The Output window displays the build
results.
v1.6
08/02/2006
63
Building a
Java Applications » Compiling, Running and Debugging a Project »
Project

Choose Run » Run Main Project
(F6) from the main menu to run
a project.

Alternatively, right-click on the
project node in the Project
window and click Run Project
from the contextual menu.

The Output window displays the
project output.
v1.6
08/02/2006
64
Running a
Java Applications » Compiling, Running and Debugging a project »
project

To set a breakpoint, place the
caret in any of the methods in
the Java file and press
Ctrl+F8.

The selected line will be
highlighted indicating that the
breakpoint has been set.

Choose Run » Debug Main
Project (F5) from the main
menu to debug the project.

The IDE runs the project in
the debugger until the
breakpoint is reached.

The IDE open the Debugger
windows at the bottom of the
IDE.
v1.6
08/02/2006
65
Debugging a
Java Applications » Compiling, Running and Debugging a Project »
Debugging
a Project

To step over a line of code, choose Run » Step Over
(F8) from the main menu.

To step into a method, choose Run » Step Into (F7)
from the main menu.

To step out of a method, choose Run » Step Out
(Ctrl + F7) from the main menu.

To terminate a debugging session, choose Run »
Finish Debugger Session (Shift+F5).
v1.6
08/02/2006
66
Java Applications » Compiling, Running and Debugging a Project » Debugging a
Project »
Debugger Windows

The Local Variables, Watches, and Call Stack windows
open by default with every debugging session.

You can open any debugger window by choosing Window »
Debugging » window-name (for example, Window » Debugging
» Breakpoints).
v1.6
08/02/2006
67
Java Applications » Compiling, Running and Debugging a Project » Debugging a
Project »
Debugger Windows

Local Variables Window




The Local Variables window lists
the local variables in the current
call.
The information given for each
variable includes the variable
name, type, and value.
You can click the control to the left
of the name to expand or collapse
the variable.
Call Stack Window


The Call Stack window lists the
sequence of calls made during
execution of the current thread.
The information given for each
call includes the name of the
method, followed by the filename
and line number of the call's
currently executing statement.
v1.6
08/02/2006
68
Java Applications » Compiling, Running and Debugging a Project » Debugging a
Project »
Debugger Windows

Watches Window





The Watches window lists all variables and
expressions that you have specified to watch
while debugging.
The information given for each watch includes
the variable or expression name, type, and
value.
When you add a new variable or expression to
the Watches window, the value of the variable or
expression is immediately evaluated and
displayed.
The value of a watch is based on the current
context. As you move through your program
code, the Watches window is updated to show the
value of the watch for that context.
Creating a Watch:



v1.6
Select the variable or expression in the Source
Editor, right-click, and choose New Watch
(Ctrl+Shift+F7).
The New Watch dialog box opens with the variable
or expression entered in the text field. Click OK.
The Watches window opens with the new watch
selected.
08/02/2006
69
Java Applications » Compiling, Running and Debugging a Project » Debugging a
Project »
Debugger Windows

Classes Window




The Classes window displays the hierarchy of
all classes that have been loaded into the
virtual machine hosting the program that is
being debugged.
You can click the turner control (+) to the left
of the class name to expand or collapse the
class.
You can inspect the fields, constructors, and
methods in your program as well as inspect
the value of static variables.
Breakpoints Window


The Breakpoints window lists the breakpoints
set in the current project.
By default, each entry contains a short text
description of the breakpoint and a boolean
flag indicating whether the breakpoint is
enabled or disabled.
v1.6
08/02/2006
70
Java Applications » Compiling, Running and Debugging a Project » Debugging a
Project »
Debugger Windows

Session Window





The Sessions window lists the debugging
sessions currently running in the IDE.
The information given for each session
includes the session name and state.
One session is always considered the
current session, unless no sessions are
running.
By default, the current session is the session
that you most recently started.
Threads Window



The Threads window lists all threads in the
current debugging session.
For Java applications, this list also includes
the thread groups in the Java virtual
machine.
The information given for each thread is the
thread name and state.
v1.6
08/02/2006
71
Exercise 3


In the project EgApp, based on the class NewClass that was created in
Exercise 2, instantiate an object of type NewClass in the main() routine
of the MainClass.
Call the printField routine of the NewClass object.
(Use the code-completion feature of the IDE.)

Build (compile) the project EgApp.

Run the project EgApp.

What do you see in the output window ?
v1.6
08/02/2006
72
Download