You are on page 1of 27

Presented by IBM developerWorks

ibm.com/developerworks/
April – June 2006

The Java Development Tools project

Making the most of


© 2006 IBM Corporation.

Presented by IBM developerWorks

What we'll cover here


ƒ The JDT environment
ƒ Creating and running a program
ƒ Scrapbook pages
ƒ Automating testing with JUnit
ƒ Using Ant and javadoc

3-2 Making the most of © 2006 IBM Corporation.

1
Presented by IBM developerWorks
ibm.com/developerworks/
April – June 2006

The JDT environment

Making the most of


© 2006 IBM Corporation.

Presented by IBM developerWorks

The Java Development Tools

ƒ A set of tools for writing, compiling, testing, and debugging Java


code.
Note: Compiling happens automatically whenever you save your
code. 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.

3-4 Making the most of © 2006 IBM Corporation.

2
Presented by IBM developerWorks

JDT perspectives
ƒ The most useful perspectives
for Java development are Java
and Debug.
There are also the Java
Browsing and Java Type
Hierarchy perspectives.
ƒ We'll look at the Java
perspective now; we'll cover the
Eclipse Debugger later.

3-5 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

The Java perspective

Class outline

Syntax-aware
Java editor
Class hierarchy

3-6 Making the most of © 2006 IBM Corporation.

3
Presented by IBM developerWorks

The Java editor


ƒ As you'd expect from a world-class IDE, Eclipse has a
color-coded Java editor.
ƒ As you type, it automatically highlights the Java syntax
and indents your code.
ƒ If there are errors, they're indicated when you save the
file (if not before).

3-7 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

The Java editor

3-8 Making the most of © 2006 IBM Corporation.

4
Presented by IBM developerWorks

Code generation

3-9 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Code assist

ƒ If you type Ctrl+Space, Eclipse shows you the relevant method


signatures 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 source
code.

3-10 Making the most of © 2006 IBM Corporation.

5
Presented by IBM developerWorks

Quick fix
ƒ For many common problems,
Eclipse can offer fixes for you.
If a package statement doesn't
match a .java file's location,
Eclipse will move the file or
update the package
statement.
If you're missing an import
statement, Eclipse can
automatically add it.
ƒ If a Quick Fix is available, the
red X will have a light bulb icon
behind it.

3-11 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Organize imports
ƒ If you use a Java class without
a corresponding import
statement, Eclipse will add
them for you automatically.
By default Eclipse imports
java.io.OutputStream, not
java.io.*.
ƒ If you remove all instances of a
class and invoke Organize
Imports again, Eclipse removes
the import statements you
don't need.

3-12 Making the most of © 2006 IBM Corporation.

6
Presented by IBM developerWorks

Code refactoring

ƒ Eclipse can refactor your code in several useful ways:


Rename classes, methods, fields
Create an interface from a class
Move classes, methods, fields

3-13 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Globalization
ƒ Eclipse has an "Externalize
Strings" function that helps you
manage translation or
localization of your projects.

3-14 Making the most of © 2006 IBM Corporation.

7
Presented by IBM developerWorks
ibm.com/developerworks/
April – June 2006

Creating and running a program

Making the most of


© 2006 IBM Corporation.

Presented by IBM developerWorks

Creating 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; compiling
and building is not a
separate step.

3-16 Making the most of © 2006 IBM Corporation.

8
Presented by IBM developerWorks

Creating a Java project


ƒ Start with FileÆNewÆ
Project…
ƒ Choose Java Project, give it a
name and click Finish.

3-17 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Creating a Java Project

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

3-18 Making the most of © 2006 IBM Corporation.

9
Presented by IBM developerWorks

Creating a Java package

ƒ To create a Java package, right-click on your new project in the


Package Explorer, then choose NewÆPackage…

3-19 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Creating a Java package


ƒ Enter a name for your package.
ƒ If you break Java style rules
(maybe your package begins
with an uppercase letter),
Eclipse reminds you.

3-20 Making the most of © 2006 IBM Corporation.

10
Presented by IBM developerWorks

Creating a Java package

ƒ Your new package appears in the Package Explorer beneath your


project.

3-21 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Creating a Java class

ƒ To create a Java class, right-click on your new package in the


Package Explorer, then choose NewÆClass.

3-22 Making the most of © 2006 IBM Corporation.

11
Presented by IBM developerWorks

Creating a Java class


ƒ Enter a name for your class.
ƒ Eclipse reminds you of style
rules here as well.
ƒ You can set the details of your
class, including its
superclasses, visibility and
interfaces.

3-23 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Creating a Java class

ƒ Your new class appears in the Package Explorer beneath your


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

3-24 Making the most of © 2006 IBM Corporation.

12
Presented by IBM developerWorks

A shortcut
ƒ You can create a new package
and a new class at the same
time.
ƒ Simply create a new class and
enter a new package name in
the wizard.

3-25 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Running your code


ƒ To run your code, right-click on
the Java file, then choose Run
AsÆJava Application.

3-26 Making the most of © 2006 IBM Corporation.

13
Presented by IBM developerWorks

Running your code

ƒ Because this is a console application (it uses


System.out.println), you'll see the output in the Console view.
By default, System.out is displayed in black,
black System.err is
displayed in red and System.in shows up in green.
green
ƒ If the Console doesn't appear, you can open it through
WindowÆShow View…

3-27 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Re-running your code

ƒ Once you've run your code, a reference to it appears in the Run


menu. You can click your program's name in the Run History menu
to run it again.
ƒ Run Last Launched (Ctrl+F11) does the same thing.

3-28 Making the most of © 2006 IBM Corporation.

14
Presented by IBM developerWorks

Creating a run configuration


ƒ In some cases you need a run
configuration.
ƒ This lets you set command-line
parameters, JVM options, etc.
ƒ Select your project in the
Package Explorer, then choose
RunÆRun…

3-29 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Creating a run configuration

ƒ Click Java Application, then New. You'll see a dialog that lets you
set all the details of your application:
The Arguments tab defines command-line arguments, The Classpath
tab lets you add JAR files to your classpath, and so on.
ƒ The run configuration must point to a Java class with a main()
method. If it doesn't, Eclipse can search your project for a class that
does.

3-30 Making the most of © 2006 IBM Corporation.

15
Presented by IBM developerWorks
ibm.com/developerworks/
April – June 2006

Scrapbook pages

Making the most of


© 2006 IBM Corporation.

Presented by IBM developerWorks

Scrapbook pages
ƒ You can create a scrapbook
page with the Java tools. A
scrapbook page lets you enter
and execute lines of Java code
without building a class to hold
them.
ƒ The wizard to create a new
scrapbook page is under NewÆ
JavaÆJava Run/Debug.

3-32 Making the most of © 2006 IBM Corporation.

16
Presented by IBM developerWorks

Scrapbook 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 this
example, we've highlighted the entire line of code; executing it writes
to the console.

3-33 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Scrapbook pages
ƒ If you choose Inspect, the
scrapbook page shows you the
value of whatever you've
highlighted.
In this example, we've only
highlighted Math.PI, not the
whole line of code.
ƒ Display inserts the value of
whatever you've highlighted.

3-34 Making the most of © 2006 IBM Corporation.

17
Presented by IBM developerWorks
ibm.com/developerworks/
April – June 2006

Automating testing with JUnit

Making the most of


© 2006 IBM Corporation.

Presented by IBM developerWorks

Automating testing with JUnit


ƒ 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.

3-36 Making the most of © 2006 IBM Corporation.

18
Presented by IBM developerWorks

Creating a test case


ƒ Right-click on a Java file and
choose NewÆ Other…

3-37 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Creating a test case

ƒ Select Java/JUnit on the left and TestCase on the right, then click
Next.

3-38 Making the most of © 2006 IBM Corporation.

19
Presented by IBM developerWorks

Creating a test case

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

3-39 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Creating a test case

ƒ Eclipse gives you a list of all the public methods in your class and its
superclasses. You decide which ones should be part of the JUnit test
class.

3-40 Making the most of © 2006 IBM Corporation.

20
Presented by IBM developerWorks

Creating a test case


ƒ In this example, we ask Eclipse to generate a JUnit
TestCase 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 return


the string "Hello, World!"

3-41 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Running a test case

ƒ Our test case is the Java class TestHelloWorld.


ƒ To run the class, select the test class in the Package Explorer, then
choose Run AsÆJUnit Test.

3-42 Making the most of © 2006 IBM Corporation.

21
Presented by IBM developerWorks

Running 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 is
an ordered collection of TestCases.

3-43 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Using JUnit
ƒ You define more TestCases and TestSuites as your
project progresses.
ƒ You run the JUnit tests to make sure any changes you've
made haven't broken your code.

3-44 Making the most of © 2006 IBM Corporation.

22
Presented by IBM developerWorks
ibm.com/developerworks/
April – June 2006

Using Ant and javadoc

Making the most of


© 2006 IBM Corporation.

Presented by IBM developerWorks

Using Ant
ƒ Ant (ant.apache.org) is an XML- and Java-based
build tool.
Designed to have the same functionality as make 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 (named build.xml by default) can
define a number of targets.
ƒ You can define which target gets built from the command
line (or the Eclipse equivalent), or let Ant figure out which
one should be created.

3-46 Making the most of © 2006 IBM Corporation.

23
Presented by IBM developerWorks

Using Ant
ƒ Once you've created your
build.xml file (or whatever
you choose to call it), you can
right-click on it and choose Run
Ant…

3-47 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Using javadoc
ƒ You can export your project to
javadoc.
ƒ When you do this, Eclipse runs
javadoc against your code
and exports the generated files
to the directory you choose.

3-48 Making the most of © 2006 IBM Corporation.

24
Presented by IBM developerWorks

Using javadoc
ƒ When you generate the
javadocs, you specify which
packages and classes should
be processed.
ƒ You can also decide which
class members are processed
(public, protected, private)

3-49 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks

Using javadoc
ƒ You can customize the files that
are generated, such as index
pages or navigation bars.
ƒ If you want, you can create links
to the standard Java libraries.

3-50 Making the most of © 2006 IBM Corporation.

25
Presented by IBM developerWorks

Using javadoc

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


by default.

3-51 Making the most of © 2006 IBM Corporation.

Presented by IBM developerWorks


ibm.com/developerworks/
April – June 2006

Summary

Making the most of


© 2006 IBM Corporation.

26
Presented by IBM developerWorks

Summary
ƒ We've covered (although very quickly) the Java
development 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

3-53 Making the most of © 2006 IBM Corporation.

27

You might also like