Generate Java source from UML class diagram Eclipse

You can perform round-trip engineering in Eclipse, to keep Java source code and class model in sync. In this tutorial, we will see how to create a class model and eventually generating source code from model.

January 18, 2010
User Rating: / 30
Views: 48,236
PDF Link Add comments

  1. Create a Java project Phone Book in Eclipse.
    create java project
  2. Right click on the project node in Package Explorer and select Open Visual Paradigm from the popup menu.
  3. In Diagram Navigator, right click on Class Diagram and select New Class Diagram from the popup menu.
    new class diagram
  4. A new diagram is created. You are asked to enter a package header on top of the diagram. Enter myapp and press Enter.
    enter package header
  5. You are asked to provide the diagram name. Enter Domain Model and press Enter.
    enter diagram name
  6. Select Class from the diagram toolbar. Click on the diagram to create a class and name it as PhoneBook.
    create class
  7. We need to create a new class Category with association to PhoneBook. Move the mouse cursor of the class PhoneBook. Press on the resource icon Aggregation->Class.
    press on agg resource
  8. Drag to the right of the class PhoneBook and release the mouse button. Name the new class Category.
    created category class
  9. Follow the previous steps to create class Contact, associated by class Category.
    created contact class
  10. There are two kinds of contact: friend and relative. Create two subclasses from class Contact through the resource icon Generalization->Class, and name them as Friend and Relative.
    create subclasses from contact
    The diagram should look like this:
    subclasses created
  11. It is time to add attributes to classes. Right click on class PhoneBook and select Add > Attribute from the popup menu.
    create attribute
  12. Enter owner : String to name the attribute as owner, and set the type as String.
    created first attribute in phone book
  13. Press Enter to proceed to the next attribute. Enter createDate : java.util.Date as attribute name and type. Click on diagram to confirm editing.
    attributes created
    Note that the package of type Date, which is java.util, will not be shown by default. If you want to show fully qualified name you can right click on diagram background and select Presentation Options > Type > Fully Qualified from the popup menu.
  14. Let's continue with creating operation. Right click on the class PhoneBook and select Add > Operation from the popup menu.
  15. Enter createPhoneBook(owner : String) : PhoneBook to create a public operation createPhoneBook with parameter owner, and return an object in type PhoneBook.
    open created
  16. Follow the previous steps create attribute and operations in the remaining classes as listed below:
    Class Attributes Operations
    Category title : String
    desc : String
    createCategory(title : String, desc : String) : Category
    Contact name : String
    address : String
    age : int
    gender : char
    createContact(name : String) : Contact
    createContact(name : String, age : int) : Contact
    Friend firstMet : String  
    Relative relationship : String  

    The diagram should look like this:
    attributes operations added
  17. We almost complete modeling. Let's set the role names. Double click on the Category end of the connector between class PhoneBook and Category. Name the role cats. Set multiplicity as 0..*. These changes will affect the attribute of category that will be generated in the class PhoneBook.
    name connection end
  18. Similarly edit the connector between Category and Contact by setting the role names of the two ends to cat and contacts, and multiplicities to 1 and 0..* respectively. This is the completed diagram.
    completed class model
  19. Press Ctrl-S to save.
  20. Now comes the code generation. Click on the Update Code button on toolbar.
    update coed
  21. Check the Package Explorer. You should see a list of generated file. You can open them to fill in the code body.
    code generated
  22. This is the end of the tutorial. Instead of closing Eclipse now, you may try something more by editing the code like to add, rename or delete class, attributes and operations, and click the Update UML Model button, which is the one next to the Update Code button, and observe the changes that will make in the class model. Enjoy!

Rate this Article

Click on one of the stars below to rate this article from 1 (lowest) to 5 (highest).

You may be interested in

  1. Data Flow Diagram (DFD) Tutorial
  2. Writing Effective Use Case
  3. Draw Entity Relationship Diagram (ERD)
  4. Data Flow Diagram with Examples - Food Ordering System
  5. Perform UML Modeling in NetBeans
Tags of this article:

Comments (13)

written by Vitalij on March 3, 2010

I don't get the diagram menu in Eclipse and the code button is disabled, so I can't generate the code.

written by Jick on March 3, 2010

Did you start SDE-EC? Which edition are you using?

written by Ishtiaque on March 5, 2010

Hi, I have a java project. Is it possible to create the class diagram from it?


written by Jick on March 5, 2010

It is possible to reverse engineer class model from source code. Please visit the following page and watch the movie under "Java round-trip code engineering":

written by Kamal on March 12, 2010

This step is not working for me..

10.There are two kinds of contact: friend and relative. Create two subclasses from class Contact through the resource icon Generalization->Class, and name them as Friend and Relative

written by Angus Chan on March 12, 2010

What u mean not working?
Cannot name or cannot gen code?

written by Rujanart on March 13, 2010


I followed the tutorial video. However, when I tried to convert java source code to UML model, the button "update UML Model" is in gray so I cannot click it.

Any suggestions are appreciated.


written by Angus Chan on March 13, 2010

Which edition you are using?
The update to UML Model is only support in Professional Edition or above

written by Rujanart on March 13, 2010

I am using Community Edition so it doesn't work for this edition, right?

Thanks very much

written by Atul on April 10, 2011

Hi ,
Beautiful tutorial.Very easy to follow and execute.
I had one tiny issue though my friend and relative extending from Contact show up as 2 arrows as opposed to one as showed by you in Step 10 , how do i merge the 2 arrows into one.

written by Re: Mr on April 11, 2011

Hi Atul,

Thank you for your post. Please select the three classes - Contact, Friend, Relative. Then, right click on your selection and select "Layout > Hierarchical Style" from the popup menu. (Note: Do not select the other classes or else the other classes will be involved in layout)

Hope this help.

Best regards,
Jick Yeung

written by dhikra on September 2, 2013

I need help please
Can I generate the code java from the free edition community edition) ?

written by Rain Wong on September 3, 2013

Hi dhikra,

Code engineering features are supported in Standard Edition or above.

Best regards,
Rain Wong

Write comment



Enter the calculation result

security code