03-Java Development Tools

Embed Size (px)

Citation preview

  • 8/7/2019 03-Java Development Tools

    1/27

    1

    Presented by IBM developerWorks

    ibm.com/developerworks/April June 2006

    2006 IBM Corporation.

    Making the most ofMaking the most of

    The Java Development Tools projectThe Java Development Tools project

    3-2 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    What we'll cover hereWhat we'll cover here

    The JDT environment

    Creating and running a program

    Scrapbook pages

    Automating testing with JUnit

    Using Ant and javadoc

  • 8/7/2019 03-Java Development Tools

    2/27

    2

    Presented by IBM developerWorks

    ibm.com/developerworks/April June 2006

    2006 IBM Corporation.

    Making the most ofMaking the most of

    The JDT environmentThe JDT environment

    3-4 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    The Java Development ToolsThe Java Development Tools

    A set of tools for writing, compiling, testing, and debugging Javacode.

    Note: Compiling happens automatically whenever you save yourcode. It's not a separate step.

    The Eclipse SDK includes the Java tools. See eclipse.org/jdt if

    you want to learn more about the project.

  • 8/7/2019 03-Java Development Tools

    3/27

    3

    3-5 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    JDT perspectivesJDT perspectives

    The most useful perspectivesfor Java development are Javaand Debug.

    There are also the JavaBrowsing and Java TypeHierarchy perspectives.

    We'll look at the Javaperspective now; we'll cover theEclipse Debugger later.

    3-6 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    The Java perspectiveThe Java perspective

    Syntax-awareJava editor

    Class hierarchy

    Class outline

  • 8/7/2019 03-Java Development Tools

    4/27

  • 8/7/2019 03-Java Development Tools

    5/27

    5

    3-9 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Code generationCode generation

    3-10 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Code assistCode assist

    If you type Ctrl+Space, Eclipse shows you the relevant methodsignatures and the javadoc for each.

    This works for code you write as well as the standard Java libraries.

    You don't have to run javadoc against your code for this to work.

    The documentation above comes from the comment in the sourcecode.

  • 8/7/2019 03-Java Development Tools

    6/27

    6

    3-11 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Quick fixQuick fix

    For many common problems,Eclipse can offer fixes for you.

    If apackage statement doesn'tmatch a .java file's location,

    Eclipse will move the file orupdate thepackage

    statement.

    If you're missing an import

    statement, Eclipse canautomatically add it.

    If a Quick Fix is available, thered X will have a light bulb iconbehind it.

    3-12 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Organize importsOrganize imports

    If you use a Java class withouta corresponding importstatement, Eclipse will addthem for you automatically.

    By default Eclipse importsjava.io.OutputStream, not

    java.io.*.

    If you remove all instances of aclass and invoke OrganizeImports again, Eclipse removesthe import statements you

    don't need.

  • 8/7/2019 03-Java Development Tools

    7/27

    7

    3-13 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    CodeCode refactoringrefactoring

    Eclipse can refactor your code in several useful ways:

    Rename classes, methods, fields

    Create an interface from a class

    Move classes, methods, fields

    3-14 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    GlobalizationGlobalization

    Eclipse has an "ExternalizeStrings" function that helps youmanage translation orlocalization of your projects.

  • 8/7/2019 03-Java Development Tools

    8/27

    8

    Presented by IBM developerWorks

    ibm.com/developerworks/April June 2006

    2006 IBM Corporation.

    Making the most ofMaking the most of

    Creating and running a programCreating and running a program

    3-16 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating and running codeCreating and running code

    It's a short process:

    1. Create a Java project

    2. Create a Java package

    3. Create a Java class in that package

    4. Set up a run configuration

    5. Run your code

    This can be confusing to

    newcomers; compilingand building is not a

    separate step.

  • 8/7/2019 03-Java Development Tools

    9/27

    9

    3-17 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a Java projectCreating a Java project

    Start with FileNewProject

    Choose Java Project, give it aname and click Finish.

    3-18 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a Java ProjectCreating a Java Project

    If you click Next after you give your project a name, you'll see otheroptions. You can use these to set the classpath of your project,among other things.

  • 8/7/2019 03-Java Development Tools

    10/27

    10

    3-19 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a Java packageCreating a Java package

    To create a Java package, right-click on your new project in thePackage Explorer, then choose NewPackage

    3-20 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a Java packageCreating a Java package

    Enter a name for your package.

    If you break Java style rules(maybe your package beginswith an uppercase letter),Eclipse reminds you.

  • 8/7/2019 03-Java Development Tools

    11/27

    11

    3-21 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a Java packageCreating a Java package

    Your new package appears in the Package Explorer beneath yourproject.

    3-22 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a Java classCreating a Java class

    To create a Java class, right-click on your new package in thePackage Explorer, then choose NewClass.

  • 8/7/2019 03-Java Development Tools

    12/27

    12

    3-23 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a Java classCreating a Java class

    Enter a name for your class.

    Eclipse reminds you of stylerules here as well.

    You can set the details of yourclass, including itssuperclasses, visibility andinterfaces.

    3-24 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a Java classCreating a Java class

    Your new class appears in the Package Explorer beneath yourpackage.

    Eclipse also opens the source file for your class in the Java editor.

  • 8/7/2019 03-Java Development Tools

    13/27

    13

    3-25 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    A shortcutA shortcut

    You can create a new packageand a new class at the sametime.

    Simply create a new class andenter a new package name inthe wizard.

    3-26 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Running your codeRunning your code

    To run your code, right-click onthe Java file, then choose RunAsJava Application.

  • 8/7/2019 03-Java Development Tools

    14/27

    14

    3-27 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Running your codeRunning your code

    Because this is a console application (it usesSystem.out.println ), you'll see the output in the Console view.

    By default, System.outSystem.out is displayed in blackin black, System.errSystem.err isdisplayed in redin red and System.inSystem.in shows up in greenin green.

    If the Console doesn't appear, you can open it throughWindowShow View

    3-28 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    ReRe--running your coderunning your code

    Once you've run your code, a reference to it appears in the Runmenu. You can click your program's name in the Run History menuto run it again.

    Run Last Launched (Ctrl+F11) does the same thing.

  • 8/7/2019 03-Java Development Tools

    15/27

    15

    3-29 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a run configurationCreating a run configuration

    In some cases you need a runconfiguration.

    This lets you set command-lineparameters, JVM options, etc.

    Select your project in thePackage Explorer, then chooseRunRun

    3-30 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a run configurationCreating a run configuration

    Click Java Application, then New. You'll see a dialog that lets youset all the details of your application:

    The Arguments tab defines command-line arguments, The Classpathtab lets you add JAR files to your classpath, and so on.

    The run configuration must point to a Java class with amain()

    method. If it doesn't, Eclipse can search your project for a class thatdoes.

  • 8/7/2019 03-Java Development Tools

    16/27

    16

    Presented by IBM developerWorks

    ibm.com/developerworks/April June 2006

    2006 IBM Corporation.

    Making the most ofMaking the most of

    Scrapbook pagesScrapbook pages

    3-32 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Scrapbook pagesScrapbook pages

    You can create a scrapbookpage with the Java tools. Ascrapbook page lets you enterand execute lines of Java codewithout building a class to holdthem.

    The wizard to create a newscrapbook page is under NewJavaJava Run/Debug.

  • 8/7/2019 03-Java Development Tools

    17/27

    17

    3-33 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Scrapbook pagesScrapbook pages

    You can highlight some code, right-click on it, then choose Inspect,Display or Execute.

    Our sample code here is System.out.println("Here's the value of PI: " + Math.PI);

    If you choose Execute, the selected code is executed. In thisexample, we've highlighted the entire line of code; executing it writesto the console.

    3-34 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Scrapbook pagesScrapbook pages

    If you choose Inspect, thescrapbook page shows you thevalue of whatever you'vehighlighted.

    In this example, we've onlyhighlightedMath.PI, not the

    whole line of code.

    Display inserts the value ofwhatever you've highlighted.

  • 8/7/2019 03-Java Development Tools

    18/27

    18

    Presented by IBM developerWorks

    ibm.com/developerworks/April June 2006

    2006 IBM Corporation.

    Making the most ofMaking the most of

    Automating testing withAutomating testing with JUnitJUnit

    3-36 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Automating testing withAutomating testing with JUnitJUnit

    JUnit was created by programming legends Kent Beck

    and Erich Gamma.

    It makes it easy to implement Test-Driven Development

    (TDD), (sometimes called Test First Development).

    Eclipse has JUnit support built in.

  • 8/7/2019 03-Java Development Tools

    19/27

    19

    3-37 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a test caseCreating a test case

    Right-click on a Java file andchoose New Other

    3-38 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a test caseCreating a test case

    Select Java/JUnit on the left and TestCase on the right, then clickNext.

  • 8/7/2019 03-Java Development Tools

    20/27

    20

    3-39 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a test caseCreating a test case

    When you create a JUnit test case, you name the test case (it's aJava class) as well as the Java class tested by the test case.

    3-40 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a test caseCreating a test case

    Eclipse gives you a list of all the public methods in your class and itssuperclasses. You decide which ones should be part of the JUnit testclass.

  • 8/7/2019 03-Java Development Tools

    21/27

    21

    3-41 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Creating a test caseCreating a test case

    In this example, we ask Eclipse to generate a JUnitTestCase for the getGreeting() method.

    The complete testGetGreeting() method is:

    public void testGetGreeting() {

    HelloWorld hw = new HelloWorld();

    assertEquals("Hello, World!",

    hw.getGreeting());

    }

    We're saying that getGreeting() should always returnthe string "Hello, World!"

    3-42 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Running a test caseRunning a test case

    Our test case is the Java class TestHelloWorld.

    To run the class, select the test class in the Package Explorer, thenchoose Run AsJUnit Test.

  • 8/7/2019 03-Java Development Tools

    22/27

    22

    3-43 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Running a test caseRunning a test case

    The results of running your test case appear in the JUnit view.

    Green is good

    You can also create and run JUnit TestSuites. A TestSuite isan ordered collection of TestCases.

    3-44 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    UsingUsing JUnitJUnit

    You define more TestCases and TestSuites as your

    project progresses.

    You run the JUnit tests to make sure any changes you'vemade haven't broken your code.

  • 8/7/2019 03-Java Development Tools

    23/27

    23

    Presented by IBM developerWorks

    ibm.com/developerworks/April June 2006

    2006 IBM Corporation.

    Making the most ofMaking the most of

    Using Ant andUsing Ant and javadocjavadoc

    3-46 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Using AntUsing Ant

    Ant (ant.apache.org) is an XML- and Java-based

    build tool.

    Designed to have the same functionality asmake without its quirks

    You don't need a tab character at the start of each line, for example.

    You can extend Ant to do other tasks if you want.

    An Ant build file (namedbuild.xml by default) can

    define a number of targets.

    You can define which target gets built from the commandline (or the Eclipse equivalent), or let Ant figure out whichone should be created.

  • 8/7/2019 03-Java Development Tools

    24/27

    24

    3-47 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    Using AntUsing Ant

    Once you've created yourbuild.xml file (or whatever

    you choose to call it), you canright-click on it and choose RunAnt

    3-48 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    UsingUsing javadocjavadoc

    You can export your project tojavadoc.

    When you do this, Eclipse runsjavadoc against your code

    and exports the generated filesto the directory you choose.

  • 8/7/2019 03-Java Development Tools

    25/27

    25

    3-49 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    UsingUsing javadocjavadoc

    When you generate thejavadocs, you specify which

    packages and classes shouldbe processed.

    You can also decide whichclass members are processed(public, protected, private)

    3-50 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    UsingUsing javadocjavadoc

    You can customize the files thatare generated, such as indexpages or navigation bars.

    If you want, you can create linksto the standard Java libraries.

  • 8/7/2019 03-Java Development Tools

    26/27

    26

    3-51 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    UsingUsing javadocjavadoc

    The generated documentation is put in the docs folder of your project

    by default.

    Presented by IBM developerWorks

    ibm.com/developerworks/

    April June 2006

    2006 IBM Corporation.

    Making the most ofMaking the most of

    SummarySummary

  • 8/7/2019 03-Java Development Tools

    27/27

    3-53 Making the most of 2006 IBM Corporation.

    Presented by IBM developerWorks

    SummarySummary

    We've covered (although veryquickly) the Javadevelopment functions in Eclipse, including:

    Various automatic coding features

    How to create and run Java code

    Using scrapbook pages

    Automating testing with JUnit

    Using ant and javadoc inside Eclipse