449

How can I generate UML diagrams (especially sequence diagrams) from existing Java code?

M. Prokhorov
  • 3,894
  • 25
  • 39
Jonathan
  • 7,349
  • 5
  • 29
  • 35
  • 4
    That is unlikely to happen. Real Java code is not structured to look pretty as UML diagrams. – Stephan Eggermont Feb 09 '13 at 01:24
  • 2
    So many answers that don't address sequence diagrams. Hmph – Bill K Jan 09 '18 at 20:32
  • @BillK my answer does address sequence diagrams. It is a Intellij Idea plugin - https://stackoverflow.com/a/45239269/529187. – Devs love ZenUML Jul 13 '19 at 05:01
  • 2
    This Question is more appropriate to the sister site [*Software Recommendations Stack Exchange*](https://softwarerecs.stackexchange.com/). – Basil Bourque Sep 15 '19 at 17:57
  • [Java Design Patterns Books in PDF format](https://stackoverflow.com/questions/2688910/learning-to-think-in-the-object-oriented-way/57994301#57994301) – Nasreen Ustad Sep 18 '19 at 13:52
  • There is a tool named **binarydoc** which can generate UML diagrams, including `UML sequence diagram` , `Control Flow Graph (CFG)` , from the Java `bytecode` (not source code). Here is an sample UML diagram page for the java method [java.net.AbstractPlainSocketImpl.getInputStream](https://openjdk.binarydoc.org/net.java/openjdk/13.0/method?classfilelocation=java.net.abstractplainsocketimpl&seq=19&methodname=getInputStream) – Happy Mar 07 '20 at 23:08
  • https://www.youtube.com/watch?v=cJwB5qxYDuU&t=0s Using EA explained Here. – Amruth A Apr 16 '20 at 04:30

16 Answers16

261

ObjectAid UML Explorer

Is what I used. It is easily installed from the repository:

Name:     ObjectAid UML Explorer
Location: http://www.objectaid.com/update/current

And produces quite nice UML diagrams:

Screenshot

Description from the website:

The ObjectAid UML Explorer is different from other UML tools. It uses the UML notation to show a graphical representation of existing code that is as accurate and up-to-date as your text editor, while being very easy to use. Several unique features make this possible:

  • Your source code and libraries are the model that is displayed, they are not reverse engineered into a different format.
  • If you update your code in Eclipse, your diagram is updated as well; there is no need to reverse engineer source code.
  • Refactoring updates your diagram as well as your source code. When you rename a field or move a class, your diagram simply reflects the changes without going out of sync.
  • All diagrams in your Eclipse workspace are updated with refactoring changes as appropriate. If necessary, they are checked out of your version control system.
  • Diagrams are fully integrated into the Eclipse IDE. You can drag Java classes from any other view onto the diagram, and diagram-related information is shown in other views wherever applicable.
pdem
  • 3,880
  • 1
  • 24
  • 38
Thomas Ahle
  • 30,774
  • 21
  • 92
  • 114
  • Easier than Papyrus, but with less options to create diagrams. – lcardosobr Jul 30 '12 at 14:13
  • In ObjectAid, Is there a way to auto arrange the class diagram? – Reusable Nov 21 '12 at 09:44
  • 4
    HowTo >>> Create account here: https://www.objectaid.com/login (activation email was send in 10minutes), Click Diagram Add-On, licence will be send to your email. Follow this http://www.objectaid.com/installation to install ObjectAid, then install GEF from http://download.eclipse.org/tools/gef/updates/releases/ Now copy-paste licence from email to Window->Preferences->ObjectAid How to create diagram? http://www.objectaid.com/class-diagram – rluks Apr 05 '13 at 22:01
  • Cannot install in eclipse juno. `An error occurred while installing the items session context was:(profile=epp.package.java, phase=org.eclipse.equinox.internal.p2.engine.phases.Install, operand=null --> [R]com.objectaid.uml 1.1.3, action=). Failed to prepare partial IU: [R]com.objectaid.uml 1.1.3.` – Gunaseelan Jul 23 '13 at 09:37
  • what about classes associations? can this plugin make it ? and if yes, How ? – Muhammed Refaat Jul 30 '13 at 20:52
  • The link is not for your browser, but for copying into eclipse – Thomas Ahle Sep 18 '13 at 19:21
  • Surprisingly, ObjectAID does not export models to xmi. Think about it if you wanna try jhipster-uml – Jerome_B Dec 22 '15 at 15:20
  • 1
    Commercial version of ObjectAid can do better when it comes to sequence diagram generation vs free can only be used to generate class diagrams !! – whoami - fakeFaceTrueSoul Oct 05 '18 at 16:38
  • Cannot get it to show associations between classes, nor are packages visualised, unless you manually drop packages onto the workspace and manually arrange their contents. The authors say "we chose not to display classes inside of their package because that just doesn't scale for many classes or classes with many features." IMO this is a missed opportunity - packages and associations are nice things to show in diagrams e.g. (my) gituml.com tool does such visualisations https://www.gituml.com/viewz/23 – abulka Sep 19 '19 at 00:07
64

EDIT: If you're a designer then Papyrus is your best choice it's very advanced and full of features, but if you just want to sketch out some UML diagrams and easy installation then ObjectAid is pretty cool and it doesn't require any plugins I just installed it over Eclipse-Java EE and works great !.


UPDATE Oct 11th, 2013

My original post was in June 2012 a lot of things have changed many tools has grown and others didn't. Since I'm going back to do some modeling and also getting some replies to the post I decided to install papyrus again and will investigate other possible UML modeling solutions again. UML generation (with synchronization feature) is really important not to software designer but to the average developer.

I wish papyrus had straightforward way to Reverse Engineer classes into UML class diagram and It would be super cool if that reverse engineering had a synchronization feature, but unfortunately papyrus project is full of features and I think developers there have already much at hand since also many actions you do over papyrus might not give you any response and just nothing happens but that's out of this question scope anyway.

The Answer (Oct 11th, 2013)

Tools

  1. Download Papyrus
  2. Go to Help -> Install New Software...
  3. In the Work with: drop-down, select --All Available Sites--
  4. In the filter, type in Papyrus
  5. After installation finishes restart Eclipse
  6. Repeat steps 1-3 and this time, install Modisco

Steps

  1. In your java project (assume it's called MyProject) create a folder e.g UML
  2. Right click over the project name -> Discovery -> Discoverer -> Discover Java and inventory model from java project, a file called MyProject_kdm.xmi will be generated. enter image description here
  3. Right click project name file --> new --> papyrus model -> and call it MyProject.
  4. Move the three generated files MyProject.di , MyProject.notation, MyProject.uml to the UML folder
  5. Right click on MyProject_kdm.xmi -> Discovery -> Discoverer -> Discover UML model from KDM code again you'll get a property dialog set the serialization prop to TRUE to generate a file named MyProject.uml enter image description here

  6. Move generated MyProject.uml which was generated at root, to UML folder, Eclipse will ask you If you wanted to replace it click yes. What we did in here was that we replaced an empty model with a generated one.

  7. ALT+W -> show view -> papyrus -> model explorer

  8. In that view, you'll find your classes like in the picture enter image description here

  9. In the view Right click root model -> New diagram enter image description here

  10. Then start grabbing classes to the diagram from the view

Some features

  • To show the class elements (variables, functions etc) Right click on any class -> Filters -> show/hide contents Voila !!

  • You can have default friendly color settings from Window -> pereferences -> papyrus -> class diagram

  • one very important setting is Arrange when you drop the classes they get a cramped right click on any empty space at a class diagram and click Arrange All

  • Arrows in the model explorer view can be grabbed to the diagram to show generalization, realization etc

  • After all of that your settings will show diagrams like enter image description here

  • Synchronization isn't available as far as I know you'll need to manually import any new classes.

That's all, And don't buy commercial products unless you really need it, papyrus is actually great and sophisticated instead donate or something.

Disclaimer: I've no relation to the papyrus people, in fact, I didn't like papyrus at first until I did lots of research and experienced it with some patience. And will get back to this post again when I try other free tools.

Yoon5oo
  • 496
  • 5
  • 11
Ismail Marmoush
  • 13,140
  • 25
  • 80
  • 114
  • I searched for Modisco in eclipse. it showed up with 0 intallable. Is this project alive or taken away ? Did it become paid tool. Will Papyrus not run without this ? – R-JANA Jul 24 '13 at 16:16
  • Used STS and unsuccessful . – R-JANA Jul 24 '13 at 16:31
  • @R-JANA I don't know what's the current status of Modisco, but I'm going back to this post ASAP cause I'm about to install papyrus soon and will have to update it. I advice you to try for yourself installing papyrus, with my current post in mind; I think much change happened since I posted this answer. – Ismail Marmoush Jul 27 '13 at 03:01
  • There is an optional plug-in for Papyrus, that adds Java reverse engineering functionality: http://wiki.eclipse.org/Java_reverse_engineering. With this, it is way easier to add diagram elements from source, you can even drag classes into a diagram. – Markus Ende Aug 05 '15 at 10:05
42

What is your codebase? Java or C++?

alt text

eUML2 for Java is a powerful UML modeler designed for Java developper in Eclipse. The free edition can be used for commercial use. It supports the following features:

  • CVS and Team Support
  • Designed for large project with multiple and customizable model views
  • Helios Compliant
  • Real-time code/model synchronization
  • UML2.1 compliant and support of OMG XMI
  • JDK 1.4 and 1.5 support
  • The commercial edition provides:

  • Advanced reversed engineering

  • Powerful true dependency analyze tools
  • UML Profile and MDD
  • Database tools
  • Customizable template support
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
prakash
  • 58,901
  • 25
  • 93
  • 115
  • 20
    This is a very difficult tool to install. I tried for about half an hour with no success. Any other suggestions? – Jonathan Sep 09 '08 at 15:07
  • 1
    Use Yoxos to manage dependencies in Eclipse , its really nice ! http://eclipsesource.com/en/yoxos/yoxos-ondemand/ – Khangharoth Nov 16 '09 at 13:41
  • 2
    Cant find eUML2 is my Update Managaer and I dont want to pack a new Eclipse setup just fr this feature. I'm using Eclipse "Helios" and go this way: Help - Install new Software - Helios http://download.eclipse.org/releases/helios - Modeling – OneWorld Sep 15 '10 at 09:55
  • 3
    I know this is an old post, but most problems with installing eUML2 seem to have been resolved. – Andy Apr 27 '11 at 10:55
  • 7
    eUML is real crap. It has changed my code format and added tags everywhere. My code is now so dirty that I prefer to undo the change. The problem is that there is no undo and your code is changed for ever. I would recommend to backup your project before using eUML crapper :-) – UML GURU May 04 '11 at 08:08
  • 33
    Did you ever hear about version-control? – devoured elysium May 17 '11 at 04:45
  • I installed something called "UML2 Extender SDK" through Help - Install new Software - Modeling. The install went fine, but I don't see the eUML2 Diagrams described in the [tutorial](http://www.soyatec.com/euml2/documentation/com.soyatec.euml2.doc/gettingstartedwithclassdiagram/firstDiagram/creating-1/index.html) – User1 Jun 03 '11 at 17:26
  • appalling software, installed everything ok but the tool itself is not good – NimChimpsky Jul 29 '11 at 18:43
  • 1
    The installer failed to resolve its dependency on Batik for me. Also the instructions on the homepage are sorely outdated. Pain in the butt to install, for sure. – Joe Coder Mar 26 '12 at 22:04
  • 1
    Juno version has not evaluation version, only licensed one. – logoff Mar 18 '13 at 15:58
  • I used [eUML2](http://www.soyatec.com/euml2/installation/) and it was really easy to download, install, and use. At least for Galileo and Ganymede, it works fine. – Max.Mirkia Nov 29 '10 at 03:53
  • eUML adds model tags directly in my code. It changes therefore my code structure without even asking me if I agree. If I refactor then I loose my diagrams which can not be opened anymore. The reverse is showing more and sometimes less methods than it really exist. etc.....this tool did have some potential but the bugs have never been fixed. It is still the same release since 2007. It is a no go now in my company and we have switched to other tools. – UML GURU Nov 15 '11 at 09:34
  • eUML2 refuses to install in Juno. – Chris Milburn Jun 27 '13 at 07:48
12

I am one of the authors, so the answer can be biased. It is open-source (Apache 2.0), but the plugin is not free. You don't have to pay (obviously) if you clone and build it locally.

On Intellij IDEA, ZenUML can generate sequence diagram from Java code. enter image description here

Check it out at https://plugins.jetbrains.com/plugin/12437-zenuml-support

Source code: https://github.com/ZenUml/jetbrains-zenuml

Devs love ZenUML
  • 11,344
  • 8
  • 53
  • 67
7

I developed a maven plugin that can both, be run from CLI as a plugin goal, or import as dependency and programmatically use the parser, @see Main#main() to get the idea on how.

It renders PlantUML src code of desired packages recursively that you can edit manually if needed (hopefully you won't). Then, by pasting the code in the plantUML page, or by downloading plant's jar you can render the UML diagram as a png image.

Check it out here https://github.com/juanmf/Java2PlantUML

Example output diagram: enter image description here

Any contribution is more than welcome. It has a set of filters that customize output but I didn't expose these yet in the plugin CLI params.

It's important to note that it's not limited to your *.java files, it can render UML diagrams src from you maven dependencies as well. This is very handy to understand libraries you depend on. It actually inspects compiled classes with reflection so no source needed

Be the 1st to star it at GitHub :P

Yoon5oo
  • 496
  • 5
  • 11
juanmf
  • 2,002
  • 2
  • 26
  • 28
7

How about PlantUML? It's not for reverse engineering!!! It's for engineering before you code.

Zamel
  • 692
  • 1
  • 10
  • 21
  • 5
    Doesn't answer the question, which clearly asks for a reverse engineering tool. – jmite Nov 29 '12 at 23:17
  • 1
    I'm not sure that's completely correct. PlantUML is also able to describe code with diagrams. Try opening the relevant view in a class filee – Erez A. Korn Dec 03 '12 at 08:14
  • 1
    Yup, I can verify that class diagrams are auto created from Java files in the Eclipse plugin. – HRJ Jul 03 '13 at 07:16
  • 1
    I tried this, but if your PlantUML code gets too complex, you spend all your time trying to figure out why the drawing won't render properly and has glitches. It's good only for small diagrams. – trusktr Oct 20 '13 at 09:09
4

I would recommend EclipseUML from Omondo for general usage, although I did experience some problems a few months back, with my web projects. They had a free edition at one point in time, but that is supposedly no longer promoted.

If you are really keen on reverse engineering sequence diagrams from source code, I would recommend jTracert.

As far as Eclipse projects themselves are concerned, the Eclipse UML2 Tools project might support reverse engineering, although I've have never seen its use in practice.

The MoDisco (Model Discovery) project Eclipse GMT project seems to be clearer in achieving your objective. The list of technology specific tools would be a good place to start with.

Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
  • Have not found that reverse engineering feature in UML2 Tools ;( – OneWorld Sep 15 '10 at 09:57
  • 2
    jTracert is now discontinued. A successor of jTracert, a new tool called jSonde is now licensed under GPL v3. http://www.jsonde.com/ – Alpine Feb 25 '11 at 15:02
  • Omondo is the best for java. Really clean integration, no mix between model and code. Superb but too expensive. We have been allowed to buy only one license but we are over 20 modelers using it because the security is based on the username login. It means that if 10 users have the same login then all of them can use it. We got a licence with Admin username :-) – UML GURU Nov 15 '11 at 09:35
  • The `jSonde` is not available in the URL shown above. You can find a [website](https://bedrin.github.io/jsonde/) and the source code in Github: https://github.com/bedrin/jsonde. – Jaime Nov 01 '17 at 15:46
3

Using IntelliJ IDEA. To generate class diagram select package and press Ctrl + Alt + U:enter image description here

By default, it displays only class names and not all dependencies. To change it: right click -> Show Categories... and Show dependencies: enter image description here

To genarate dependencies diagram (UML Deployment diagram) and you use maven go View -> Tool Windows -> Maven Projects and press Ctrl + Alt + U: enter image description here

The result: enter image description here

Also it is possible to generate more others diagrams. See documentation.

Ihor Rybak
  • 3,091
  • 2
  • 25
  • 32
  • SequenceDiagram Plugin for IntelliJ Idea (tested on community edition) does the job really well. thanks to @dean-jain from [this question](https://stackoverflow.com/questions/17311797/how-to-generate-a-sequence-diagram-from-java-source-code) – codester Feb 18 '21 at 14:19
3

I found Green plugin very simple to use and to generate class diagram from source code. Give it a try :). Just copy the plugin to your plugin dir.

pcmind
  • 990
  • 10
  • 12
  • 2
    Green seems like it has potential. However at my work we have a project that has ~700 classes and it chugs for a little bit then dies. – Appak Feb 02 '12 at 22:27
3

You could also give the netbeans UML modeller a try. I have used it to generate javacode that I used in my eclipse projects. You can even import eclipse projects in netbeans and keep the eclipse settings synced with the netbeans project settings.

I tried several UML modellers for eclipse and wasn't satisfied with them. They were either unstable, complicated or just plain ugly. ;-)

Patrick Cornelissen
  • 7,968
  • 6
  • 48
  • 70
  • Just wanted to mention that I've also used Netbeans UML modeller and didn't bump into problems in my relatively simple cases. The UI is not too intuitive, but it's totally ok. The modeller also has some advanced features such as creating JPA entity code out of model if I remember correctly. – Tuukka Mustonen Jun 21 '10 at 12:10
  • 1
    Unfortunately is seems like the UML plugin is no longer supported. A [rewrite of the plugin](http://wiki.netbeans.org/EidosUML) is apparently underway but that does not help anyone since they dropped the old plugin before the rewrite was completed. – Appak Feb 02 '12 at 23:01
  • That's a bummer :-( Let's hope that the rewrite doesn't take too long :-) – Patrick Cornelissen Feb 08 '12 at 14:46
  • 1
    The UML for NetBeans was being made by VP. They won't do it any longer, only their VP UML is available now, with a weak free version. – Gangnus Sep 15 '15 at 06:59
2

You can use the 30 days evaluation build of EclipseUML for Eclipse 3.5 : http://www.uml2.org/eclipse-java-galileo-SR2-win32_eclipseUML2.2_package_may2010.zip This is not the latest 3.6 build, but is pretty good and don't require you purchase it for testing and reverse engineering.

Reverse engineering : http://www.forum-omondo.com/documentation_eclipseuml_2008/reverse/reverse/reverse_engineering_example.html

Live flash demo: http://www.ejb3.org/reverse.swf

EclipseUML Omondo is the best tool in the world for Java. Only eUML seems to compete with it on this live java synchronization market, but eUML adds model tags in the code which is really very very bad and a definitive no go for me.

magi182
  • 3,427
  • 1
  • 15
  • 23
UML GURU
  • 1,452
  • 1
  • 9
  • 7
1

By far the best tool I have used for reverse engineering, and round tripping java -> UML is Borland's Together. It is based on Eclipse (not just a single plugin) and really works well.

kaiya
  • 271
  • 1
  • 3
  • 16
serg10
  • 31,923
  • 16
  • 73
  • 94
  • Together is now [Micro Focus Together](https://www.microfocus.com/products/requirements-management/together/) – Jaime Nov 01 '17 at 15:47
1

I've noticed SequenceDiagram plugin for Intellij is also a good option.

kaiya
  • 271
  • 1
  • 3
  • 16
whoami - fakeFaceTrueSoul
  • 17,086
  • 6
  • 32
  • 46
  • 1
    Check this for generation :- https://www.quora.com/What-is-the-best-tool-to-generate-UML-diagrams , https://stackoverflow.com/questions/4449291/need-uml-reverse-engineering-tool-for-java-project?noredirect=1&lq=1 (reverse engineering) && for Sketching :- https://www.guru99.com/best-uml-tools.html (Remember not all free tools does generate sequence diagrams, may only support class diagrams, I've used SequenceDiagram for generation & draw.io for manual creation of UML diagrams) – whoami - fakeFaceTrueSoul Jun 29 '19 at 20:49
1

Another modelling tool for Java is (my) website GitUML. Generate UML diagrams from Java or Python code stored in GitHub repositories.

One key idea with GitUML is to address one of the problems with "documentation": that diagrams are always out of date. With GitUML, diagrams automatically update when you push code using git.

Browse through community UML diagrams, there are some Java design patterns there. Surf through popular GitHub repositories and visualise the architectures and patterns in them.

diagram browser

Create diagrams using point and click. There is no drag drop editor, just click on the classes in the repository tree that you want to visualise:

select the java classes you want to visualise

The underlying technology is PlantUML based, which means you can refine your diagrams with additional PlantUML markup.

abulka
  • 1,316
  • 13
  • 18
1

There is a Free tool named binarydoc which can generate UML Sequence Diagram, or Control Flow Graph (CFG) from the bytecode (instead of source code) of a Java method.

Here is an sample diagram binarydoc generated for the java method java.net.AbstractPlainSocketImpl.getInputStream:

  • Control Flow Graph of method java.net.AbstractPlainSocketImpl.getInputStream:

Control Flow Graph

  • UML Sequence Diagram of method java.net.AbstractPlainSocketImpl.getInputStream:

UML Sequence Diagram

Happy
  • 757
  • 9
  • 18
0

I suggest PlantUML. this tools is very usefull and easy to use. PlantUML have a plugin for Netbeans that you can create UML diagram from your java code.

you can install PlantUML plugin in the netbeans by this method:

Netbeans Menu -> Tools -> Plugin

Now select Available Plugins and then find PlantUML and install it.

For more information go to website: www.plantuml.com

Masood
  • 189
  • 5