DEV Community

Jan Mewes
Jan Mewes

Posted on

Create UML Class Diagrams for Java projects with IntelliJ IDEA and PlantUML


UML Class diagrams can be useful for understanding complex code or for designing major new features. However, hand-drawing those diagrams may require too much time and effort. To reduce those costs, the Ultimate edition of IntelliJ IDEA has the bundled "Diagrams" plugin which can generate UML Class diagrams for Java and Kotlin code.

For the examples used below, the source code of the Apache Kafka project is being used (see

Create diagram

Given that the project is already set up in IntelliJ, select the files to be included in the diagram in the Project window. Then right-click the files and select "Diagrams > Show diagram" from the context menu.


More classes can be added later on to the diagram by pressing "Space" from within the diagram and then typing in the class name. Or by dragging and dropping the file file into the diagram.

Configure diagram

To include the class relationships, activate the "Show Dependencies" options. Sometimes it may also be useful to activate the options next left to it.



The diagram can be exported as a PlantUML file with the help of the "Export Diagram" icon and the "Export to file > PlantUML" context menu.


Normalize generated code

PlantUML offers plenty of configuration options. The options that were used by the Diagrams plugin may be changed by running a custom Python script which is performing string-replacements in the generated file.

python3 example.puml
Enter fullscreen mode Exit fullscreen mode

Here is how the script may look like:

#!/usr/bin/env python3

# by janux_de is marked with CC0 1.0 

import argparse
import re

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('file', nargs=1)
    args = parser.parse_args()
    path = args.file[0]

    f = open(path, "r")
    original_lines = f.readlines()

    updated_lines = []
    for line in original_lines:
        # Replace theme configuration with "hiding empty members" flag
        line = line.replace("!theme plain", "hide empty members")

        # Remove cardinality indicators
        line = re.sub(r'"\d+"', "", line)

        # Remove variable names
        line = re.sub(r'-> ".*?"', "-> ", line)

        # Remove text on edges
        line = re.sub(r' : .*', "", line)

        # Simplify dashed lines
        line = re.sub(r'-\[.*?dashed\]-', "..", line)

        # Simplify plain lines
        line = re.sub(r'-\[.*?plain\]-', "--", line)

        # Replace composition symbol with directed association
        line = line.replace("*-->", "-->")

        # Use a more intuitive flavor for inheritance symbols
        line = line.replace("--^", "--|>")
        line = line.replace("..^", "..|>")


    f = open(path, "w")
Enter fullscreen mode Exit fullscreen mode

Further processing

The diagram may then be manually edited, e.g. with the help of the PlantUML plugin for VS Code.


Finally, the PlantUML file may be compiled to a PNG with the help of this Docker command:

cat example.puml | docker run --rm -i \
    dstockhammer/plantuml:1.2023.13 \
    -pipe -tpng > example.png
Enter fullscreen mode Exit fullscreen mode

(see for the source code of the Docker image from the snippet above)



Top comments (0)