1 / 49

The Java Development Tools project

The Java Development Tools project. What we'll cover here. The JDT environment Creating and running a program Automating testing with JUnit Using Ant and javadoc. The JDT environment. The Java Development Tools. A set of tools for writing, compiling, testing, and debugging Java code.

morgan
Download Presentation

The Java Development Tools project

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Java Development Tools project

  2. What we'll cover here • The JDT environment • Creating and running a program • Automating testing with JUnit • Using Ant and javadoc

  3. The JDT environment

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

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

  6. The Java perspective Class outline Syntax-aware Java editor Class hierarchy

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

  8. The Java editor

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

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

  11. Other great features • Generate getters and setters automatically • "Quick Fix" certain problems • Automatically organize import statements • Add imports as necessary, remove unused imports, specify which imports are used • Refactoring code • Rename classes, methods, fields • Create an interface from a class • Move classes, methods, fields

  12. Creating and running a program

  13. Creating and running code • It's a short process: • Create a Java project • Create a Java package • Create a Java class in that package • Set up a run configuration • Run your code This can be confusing to newcomers; compiling and building is not a separate step.

  14. Creating a Java project • Start with FileNew Project… • Choose Java Project, give it a name and click Finish.

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

  16. Creating a Java package • To create a Java package, right-click on your new project in the Package Explorer, then choose NewPackage…

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

  18. Creating a Java package • Your new package appears in the Package Explorer beneath your project.

  19. Creating a Java class • To create a Java class, right-click on your new package in the Package Explorer, then choose NewClass.

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

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

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

  23. Running your code • To run your code, right-click on the Java file, then choose Run AsJava Application.

  24. 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, System.err is displayed in red and System.in shows up in green. • If the Console doesn't appear, you can open it through WindowShow View…

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

  26. 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…

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

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

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

  30. Automating testing with JUnit

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

  32. Creating test cases • Right-click on a Java file and choose New Other…

  33. Creating test cases • Select Java/JUnit on the left and TestCase on the right, then click Next.

  34. Creating test cases • 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.

  35. Creating test cases • 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.

  36. Creating test cases • 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!"

  37. Running test cases • 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.

  38. Running test cases • 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.

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

  40. Using Ant and javadoc

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

  42. Using Ant • 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.

  43. 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…

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

  45. 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)

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

  47. Using javadoc • The generated documentation is put in the docs folder of your project by default.

  48. Summary

  49. 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 • Automating testing with JUnit • Using ant and javadoc inside Eclipse

More Related