Debugging - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Debugging

Description:

Assert post-conditions in right place when you have conditions. You can use the URL class to: ... System Properties. GetMailURL.java //enumerates over a ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 32
Provided by: usersC1
Category:
Tags: debugging

less

Transcript and Presenter's Notes

Title: Debugging


1
Debugging
  • Chapter 5 (ALBINGs)
  • Section 4.3 (ALBINGs)
  • Section 4.4 (ALBINGs)

2
FetchURL example
  • Assert post-conditions in right place when you
    have conditions
  • You can use the URL class to
  • Download an HTTP document from a Web server. This
    document can be a static file or can be
    dynamically constructed by the Web server
  • Send an e-mail message
  • A URL has four parts
  • Protocol name
  • Host address
  • Port (80 by default for http)
  • port number to which the TCP connection is made
    on the remote host machine
  • Path to resource file
  • http//java.sun.com80/index.html

3
URL to Read Web Pages
  • When you start the client connects to a Web
    server. In the client code, you
  • Create a URL object
  • Import java.net.
  • URL x new URL(http//www.google.com)
  • URL y new URL(mailtoirahal_at_csbsju.edu)
  • Create an InputStream object from the URL object
  • X.openStream()
  • Shortcut for x.openConnection().getInputStream()
  • opens a connection to the server and allows us to
    read its response to our clients request (get
    page)
  • Returns an InputStream object which we pass to
    the InputStreamReader to convert bytes to
    characters
  • Then pass result to BufferedReader so that we can
    read it one line at a time
  • BufferedReader rdr new BufferedReader(new
    InputStreamReader( x.openStream())
  • BufferedReader rdr new BufferedReader(new
    InputStreamReader( x.openConnection().
    getInputStream()))

4
FetchURL example
  • while ((line rdr.readLine()) ! null)
  • Why do we see HTML?
  • Because we are not using a browser so we output
    the HTML tags as is

5
URL to Send EMails
  • Before starting, client must connect to a mail
    server, such as sendmail
  • Create a URL object
  • System.getProperty("mail.host",
    "unix.csbsju.edu")
  • URL doesn't contain any information about the
    mail server ? need to specify it explicitly by
    setting a Java system property
  • "unix.csbsju.edu is the default in case non is
    found
  • URL url new URL("mailtoirahal_at_csbsju.com")
  • Create a URLConnection object
  • url.openConnection()
  • Returns a URLConnection object that represents a
    connection to the remote object referred to by
    the URL
  • Create an OutputStream object from the URL object
  • PrintStream out new PrintStream(url.openConnect
    ion(). getOutputStream(), true)
  • Write the mail using out object
  • Close out
  • A connection to a server is required for these
    actions

6
list
  • main1 list
  • 31
  • 32 e.printStackTrace()
  • 33 rc null
  • 34
  • 35 gt return line.toString()
  • 36 //return rc
  • 37
  • 38
  • 39 public String callToString()
  • 40
  • main1

7
locals
  • main1 locals
  • Method arguments
  • Local variables
  • rc "lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML
    3.2//EN"gt
  • ltHTMLgt
  • ltHEADgt
  • ltMETA NAME"Generator" CONTENT"Cosmo Create
    1.0.3"gt
  • ltBASE HREF"http//www2.csbsju.edu/index.html"
    gt
  • ltTITLEgtCollege of Saint Benedict and Saint
    John's Universitylt/TITLEgt
  • ltlink REV"made" HREF"mailtoadmissions_at_csbsj
    u.edu"gt
  • ltmeta HTTP-EQUIV"Content-Type"
    CONTENT"text/html charsetISO-8859-1"gt
  • ltmeta HTTP-EQUIV"Content-language"
    CONTENT"en"gt
  • ltmeta HTTP-EQUIV"Reply-to"
    CONTENT"admissions_at_csbsju.edu"gt
  • . lt/HTMLgt
  • "
  • line null
  • rdr null

8
Navigating the call chain
  • main1 where
  • 1 FetchURL.toString (FetchURL.java35)
  • 2 FetchURL.callToString (FetchURL.java41)
  • 3 FetchURL.main (FetchURL.java51)
  • main1 up (go up the caller of 1 which is
    2)
  • main2 locals
  • No local variables
  • main2
  • main2 up (go up the caller of 2 which is 3)
  • main3 locals
  • Method arguments
  • args instance of java.lang.String1 (id424)
  • Local variables
  • i 0
  • f instance of FetchURL(id425)
  • main3

9
Finding the call site in main
  • main3 list
  • 47 for (int i 0 i lt args.length
    i)
  • 48
  • 49 System.out.println(argsi
    " ")
  • 50 f new
    FetchURL(argsi)
  • 51 gt System.out.println(f.callToS
    tring())
  • 52
  • 53
  • 54
  • 55
  • main3

10
Examining Values
  • main3 dump f
  • f requestURL instance of java.net.URL(id426)
  • main3 dump i
  • i 0

11
More Navigation
  • main3 down
  • main2 down
  • main1
  • main1 exit

12
System Properties
  • GetMailURL.java
  • //enumerates over a collection of objects
  • Enumeration enm System.getProperties().propertyN
    ames()
  • while (enm.hasMoreElements())
  • String propName (String) enm.nextElement()Sys
    tem.out.println(propName""System.getProperty(pr
    opName))
  • Output
  • java.runtime.nameJava(TM) 2 Runtime Environment,
    Standard Edition
  • sun.boot.library.path/usr/local/jdk1.5.0/jre/lib/
    i386
  • java.vm.version1.5.0-b64
  • java.vm.vendorSun Microsystems Inc.
  • java.vendor.urlhttp//java.sun.com/
  • path.separator
  • java.vm.nameJava HotSpot(TM) Client VM

13
System Properties
  • file.encoding.pkgsun.io
  • user.countryUS
  • sun.os.patch.levelunknown
  • java.vm.specification.nameJava Virtual Machine
    Specification
  • user.dir/usr/people/faculty/cs/irahal/csci230bin/
    FetchURL
  • java.runtime.version1.5.0-b64
  • java.awt.graphicsenvsun.awt.X11GraphicsEnvironmen
    t
  • java.endorsed.dirs/usr/local/jdk1.5.0/jre/lib/end
    orsed
  • os.archi386
  • java.io.tmpdir/tmp
  • line.separator
  • sun.cpu.isalist

14
System Properties
  • java.vm.specification.vendorSun Microsystems
    Inc.
  • os.nameLinux
  • sun.jnu.encodingUTF-8
  • java.library.path/usr/local/jdk1.5.0/jre/lib/i386
    /client/usr/local/jdk1.5.0/jre/lib/i386/usr/loca
    l/jdk1.5.0/jre/../lib/i386/usr/local/fortran/lib
    /usr/local/oracle/lib
  • java.specification.nameJava Platform API
    Specification
  • java.class.version49.0
  • sun.management.compilerHotSpot Client Compiler
  • os.version2.6.9-34.0.1.ELsmp
  • user.home/usr/people/faculty/cs/irahal
  • user.timezone
  • java.awt.printerjobsun.print.PSPrinterJob
  • file.encodingUTF-8
  • java.specification.version1.5
  • user.nameirahal
  • java.class.path/usr/people/faculty/cs/irahal/Java
    Packages/usr/people/classes/Java/mysql-connector-
    java-3.1.10-bin.jar/usr/people/classes/Java/objec
    tdraw/objectdraw.jar/usr/people/classes/Java.

15
System Properties
  • java.vm.specification.version1.0
  • sun.arch.data.model32
  • java.home/usr/local/jdk1.5.0/jre
  • java.specification.vendorSun Microsystems Inc.
  • user.languageen
  • java.vm.infomixed mode, sharing
  • java.version1.5.0
  • java.ext.dirs/usr/local/jdk1.5.0/jre/lib/ext
  • sun.boot.class.path/usr/local/jdk1.5.0/jre/lib/rt
    .jar/usr/local/jdk1.5.0/jre/lib/i18n.jar/usr/loc
    al/jdk1.5.0/jre/lib/sunrsasign.jar/usr/local/jdk1
    .5.0/jre/lib/jsse.jar/usr/local/jdk1.5.0/jre/lib/
    jce.jar/usr/local/jdk1.5.0/jre/lib/charsets.jar/
    usr/local/jdk1.5.0/jre/classes
  • java.vendorSun Microsystems Inc.
  • file.separator/
  • java.vendor.url.bughttp//java.sun.com/cgi-bin/bu
    greport.cgi
  • sun.cpu.endianlittle
  • sun.io.unicode.encodingUnicodeLittle

16
System Properties
  • Still leaves out so many other properties
    (environment variables)
  • Security reasons !

17
The Eclipse IDE
  • Section 10.4 (ALBINGs)
  • http//help.eclipse.org/help32/index.jsp

18
Introduction
  • Eclipse is a generic, extensible, open source IDE
    (integrated development environment)
  • completely platform- and language-neutral
  • diverse mix of languages supported by the Eclipse
    (Java, C/C, Cobol)
  • projects underway to add support for languages as
    diverse as Python, Eiffel, PHP, Ruby on Rails,
    and C to Eclipse
  • The project was originally launched in November
    2001, when IBM donated 40 million worth of
    source code from WebSphere Studio Workbench
  • Eclipse includes a number of unique features such
    as
  • code refactoring
  • support for unit testing with JUnit

19
Eclipse Workbench
  • Perspective Switcher
  • Toolbar
  • Editor Area
  • Views
  • If you close any of the views and would like to
    reopen it
  • Windows gt Show View gt select it

20
Perspectives in Eclipse
  • Click on the Perspective Switcher and select the
    Java perspective
  • Each perspective defines a selection of views and
    their layout within the workbench window
  • Each is configured for a particular task such
    Java development, debugging, etc
  • Windows gt Open Perspective gt Java
  • Other perspectives
  • Resource useful for browsing your workspace
  • Java
  • Debug
  • CVS Repository Exploring used for exploring CVS
    repositories
  • Etc

21
Wizards
  • Look at the middle of your toolbar
  • New Java Project
  • New Java Package
  • New Java Class
  • Others

22
Create a New Project
  • Click on the New Java Project wizard
  • Enter a qualified name
  • It is common to use qualified names for projects
    in the Eclipse Workspace
  • This reflects the primary Java package contained
    in each Project
  • edu.csbsju.catalogue
  • Insures unique self-describing names for projects
    shared in a source control repository such as CVS
  • It is advisable that you check the Create
    separate source and output folders
  • Creates a src folder for source code and bin
    folder for complied class files

23
Create a New Package
  • Select the project folder in the Package Explorer
  • A Java project folder is indicated by an open
    folder icon with an overlaid J
  • Click the New Java Package wizard to create a new
    Java Package
  • Enter the package name
  • edu.csbsju.catalogue
  • Within the workspace directory on your file
    system, three nested directories are created
    inside the project folder
  • edu
  • csbsju
  • catalogue

24
Create a New Class
  • Select the created package in the src folder in
    the Package Explorer view
  • A Java package is indicated by an icon resembling
    a box wrapped with a string
  • Enter class name, Catalogue
  • Notice the options for creating inner classes,
    specifying a superclass, implementing interfaces,
    and generating method stubs and comments

25
Customizing Code Templates
  • Window gt Preferences
  • Java gt Code Style gt Code Templates
  • Code gt New Java files
  • Comments gt Types
  • Delete and recreate class

26
Compile Run
  • Redo Catalogue and CatalogueItem example from
    Eclipse Distilled by Carlson
  • Compile is done while creating code!
  • How do I choose my own compiler?
  • Window gt Preferences gt Java gt Compiler
  • To run using the Console view
  • Windows gt Show View gt Console
  • From Package Explorer, right click Class to run
    and select Run gt Java Application
  • When moving to a new project, make sure you close
    the first one first
  • File gt Close All

27
Run a Project with Command-line Arguments
  • In Package Explorer
  • Select file to run
  • Click on Run gt Run
  • Or Right click file and Run As gt Run
  • Go to Arguments Tab and provide arguments each on
    a separate like
  • Click on Run

28
Import a Project to Eclipse
  • Create a new project
  • Type project name
  • Select Create project from existing source
  • Browse and select your project folder
  • Under Default output folder
  • Browse
  • Click on the Project Name
  • Create a new Folder called bin
  • Now your project works as if it has been created
    within Eclipse but it may not be under your
    workspace

29
Switching Workspaces
  • I started eclipse but I dont see any of my
    projects !
  • You can Switch Workspace to default location
  • In the Java perspective, File gt Switch Workspace
  • If the above doesnt work, then, in the Package
    Explorer, right click and Import
  • Select Existing Projects into Workspace

30
Multiple Workspaces
  • By default, all projects are stored in your
    default workspace (/Eclipse)
  • If you create a new workspace then you might need
    to switch between them
  • In the Java perspective, File gt Switch Workspace

31
Generating Getters and Setters
  • Generating Code
  • Getters and Setters
  • Right click in the editor
  • Select Source
  • Generate Getters and Setters
  • Constructor
  • Right click in the editor
  • Select Source
  • Generate Constructor
Write a Comment
User Comments (0)
About PowerShow.com