Of course! Generating a UML diagram from your Java code in Eclipse is a very common and useful task for documentation, reverse-engineering, and understanding existing projects. There are two primary ways to do this:

- Using Built-in Eclipse Features (Free & Simple): Best for quick, high-level class diagrams.
- Using a Dedicated Plugin (More Powerful & Customizable): Best for detailed, professional-quality diagrams with more control.
Method 1: Using Eclipse's Built-in "Generate Diagram" Feature
Modern Eclipse IDEs (like those from the Eclipse IDE for Enterprise Java and Web Developers package) have a built-in feature that is very easy to use. This method creates a Class Diagram.
Step-by-Step Guide:
-
Open the Package Explorer: Navigate to the Java class for which you want to create a diagram.
-
Open the Class Diagram View:
- Go to the menu:
Window>Show View>Other... - In the dialog box, expand the
GeneralorModel Developmentcategory. - Select
Class Diagramand clickOpen.
- Go to the menu:
-
Generate the Diagram:
(图片来源网络,侵删)- In the Package Explorer, right-click on the package you want to diagram (e.g.,
com.mycompany.myapp). You can also right-click on a specific class. - From the context menu, select
New>Other.... - In the wizard, expand the
GeneralorModel Developmentcategory and chooseClass Diagram. - Give your diagram a name (e.g.,
MyAppDiagram) and clickFinish.
- In the Package Explorer, right-click on the package you want to diagram (e.g.,
-
Add Elements to the Diagram:
- A new diagram editor will open. On the left side, you'll see a "Palette" with tools.
- Drag and drop classes, interfaces, and enums from the Package Explorer onto the diagram canvas.
- To show relationships: Right-click on a class on the diagram and select
Show>Relationships. This will automatically detect and draw associations, inheritances, dependencies, etc.
Pros and Cons of the Built-in Method:
- Pros:
- Free: No installation required.
- Simple & Integrated: Works directly within the standard Eclipse interface.
- Good for Quick Overviews: Excellent for getting a visual representation of a package's structure.
- Cons:
- Limited Customization: You can't change line styles, colors, or fonts extensively.
- Less Powerful: Lacks advanced features like sequence diagrams or activity diagrams directly from code.
- Export Quality: The export options (like saving as an image) can be basic.
Method 2: Using a Dedicated Plugin (Papyrus)
For more professional and feature-rich UML diagrams, the Papyrus plugin is the standard for Eclipse. It's a full-fledged modeling tool that supports all UML 2.x diagrams.
Step 1: Install Papyrus
- Go to
Help>Eclipse Marketplace...in the menu. - In the search bar, type "Papyrus".
- Find "Papyrus (Incubation)" in the results and click Go.
- Click Install and follow the on-screen instructions to complete the installation. You may need to restart Eclipse.
Step 2: Generate a UML Diagram with Papyrus
Papyrus works by creating a Modeling Project and then reverse-engineering your Java code into it.
-
Create a New Modeling Project:
(图片来源网络,侵删)- Go to
File>New>Project.... - Expand the
Modelingcategory and selectPapyrus Model. - Name your project (e.g.,
MyAppModel) and clickFinish. - A new Papyrus editor will open with an empty model.
- Go to
-
Reverse-Engineer Java Code:
- In the "Model Explorer" view (which should open with the project), right-click on your model.
- Go to
Import>Java Code. - In the wizard, browse to your Java source folder (e.g.,
src) and select the packages or classes you want to import. - Click
Finish.
-
Create the Diagram:
- In the "Model Explorer," right-click on your diagram file (e.g.,
MyAppDiagram.di). - Select
Open Diagram. - A new diagram editor will open. You can now drag classes from the "Model Explorer" onto the canvas.
- To create relationships, use the tools in the Papyrus palette on the left. Select the tool (e.g., "Association") and click on the classes you want to connect.
- In the "Model Explorer," right-click on your diagram file (e.g.,
Pros and Cons of Papyrus:
- Pros:
- Extremely Powerful: Supports all UML 2.x diagrams: Class, Sequence, Use Case, Activity, State Machine, etc.
- Highly Customizable: Full control over diagram appearance, layouts, and properties.
- Industry Standard: A robust, professional-grade modeling tool.
- Code Generation: Can also generate Java code from your UML model (forward engineering).
- Cons:
- Learning Curve: More complex than the built-in tool.
- Resource-Intensive: Can be slower and heavier on your system.
Comparison and Recommendation
| Feature | Built-in Eclipse Diagram | Papyrus Plugin |
|---|---|---|
| Ease of Use | Very Easy | Moderate |
| Cost | Free | Free |
| Diagram Types | Primarily Class Diagrams | All UML 2.x Diagrams |
| Customization | Low | Very High |
| Best For | Quick overviews, documentation, simple reverse-engineering. | Complex modeling, detailed design, full software lifecycle. |
Which One Should You Choose?
-
Choose the Built-in Method if:
- You just need a quick visual of a few classes.
- You are documenting an existing part of your code.
- You want a simple, no-fuss solution.
-
Choose Papyrus if:
- You are doing full software architecture and design.
- You need to create sequence diagrams, use case diagrams, etc.
- You require high-quality, customizable diagrams for a presentation or documentation.
- You want to perform both reverse-engineering (Java to UML) and forward-engineering (UML to Java).
