MagicDraw or other modeling tool run in OSGI Framework. This implies that batch mode programs or any applications that start MagicDraw or other modeling tool have to run in OSGI Framework as well.

In order to launch the program, the launcher is started using a java command line which then starts the program inside the framework. The initial classpath have to include only jars needed to start the framework launcher. Here is the list of required jars on the classpath:

  • lib/com.nomagic.osgi.launcher-<latest version>.jar
  • lib/bundles/org.eclipse.osgi_<latest version>.jar
  • lib/bundles/com.nomagic.magicdraw.osgi.fragment_<latest version>.jar
  • lib/md_common_api.jar
  • lib/md_common.jar
  • lib/md_api.jar
  • lib/md.jar
  • lib/jna.jar

The rest of batch mode program is configured using system properties. MagicDraw or other modeling tool jars are specified using the system property md.class.path. The preferred way to specify this classpath is as follows:

file: <absolute or relative URL to MagicDraw or other modeling tool configuration properthere are specified as pathsies file> [?base=<path to MagicDraw or other modeling tool installation directory>]#<classpath property name>

In case the current directory is the MagicDraw or other modeling tool installation directory, the base parameter does not have to be specified. The classpath property name for MagicDraw or other modeling tool property file is CLASSPATH.

There are other mandatory properties that have to be specified as well:

  • -Dcom.nomagic.osgi.config.dir=configuration
  • -Desi.system.config=data/application.conf
  • -Dlogback.configurationFile=data/logback.xml

They here are specified as paths relative to <modeling tool installation directory> therefore you either have to make your current directory before launching your program or to specify absolute paths for above listed properties.

It is possible to simplify passing of mandatory properties (except for -Dcom.nomagic.osgi.config.dir):

  • by providing one property with path from witch values for those properties can be resolved (normally that would be relative or absolute path to installation directory):

-Dcom.nomagic.magicdraw_home="C:\Program Files\MagicDraw"

  • using system property MAGICDRAW_HOME

This will not override properties specified exclusively, allowing to mix and match how mandatory properties are specified.

If mandatory properties are not provided exclusively, via -Dcom.nomagic.magicdraw_home, or MAGICDRAW_HOME system property, then it is presumed that current directory is path from witch they can be resolved.

The main class to run is com.nomagic.osgi.launcher.ProductionFrameworkLauncher which is the OSGI framework launcher that starts the program in OSGI Framework.

The following system properties depends on the type of the batch mode program.

  1. Core related batch program:
    • md.additional.class.path  -  the jar(s) constituting batch mode program specific classpath is specified using this system property. The jar's are separated using the platform specific path separator in the same way as for a regular classpath.

    • com.nomagic.magicdraw.launcher - specifies the batch mode program (implementation of com.nomagic.magicdraw.commandline.CommandLine) you want to launch, for example, -Dcom.nomagic.magicdraw.launcher=com.nomagic.magicdraw.examples.imagegenerator.ExportDiagramImages

  2. Plugin related batch program:
    • -Dcom.nomagic.magicdraw.launcher=com.nomagic.magicdraw.commandline.CommandLineActionLauncher

    • com.nomagic.magicdraw.commandline.action - specifies the batch mode action (implementation of com.nomagic.magicdraw.commandline.CommandLineAction) you want to execute, for example, -Dcom.nomagic.magicdraw.commandline.action=com.nomagic.magicdraw.examples.commandlineplugin.CommandLineActionExample

 

The examples of complete command lines that launches the batch mode program:

Core batch program command line for Windows
C:\Program Files\MagicDraw>java -Xmx1200M -Xss1024K ^
-cp lib/com.nomagic.osgi.launcher-20.0.0-SNAPSHOT.jar;^
lib/bundles/org.eclipse.osgi_3.12.50.v20170928-1321.jar;^
lib/bundles/com.nomagic.magicdraw.osgi.fragment_20.0.0.202010140732.jar;^
lib/md_api.jar;lib/md_common_api.jar;lib/md.jar;lib/md_common.jar;lib/jna.jar ^
-Dcom.nomagic.magicdraw_home="C:\Program Files\MagicDraw" ^
-Dcom.nomagic.osgi.config.dir=configuration ^
-Dmd.additional.class.path=openapi/examples/imagegenerator/imagegenerator.jar ^
-Dcom.nomagic.magicdraw.launcher=com.nomagic.magicdraw.examples.imagegenerator.ExportDiagramImages ^
com.nomagic.osgi.launcher.ProductionFrameworkLauncher project=project.mdzip destination_dir=out
Plugin batch program command line for Windows
C:\Program Files\MagicDraw>java -Xmx1200M -Xss1024K ^
-cp lib/com.nomagic.osgi.launcher-20.0.0-SNAPSHOT.jar;^
lib/bundles/org.eclipse.osgi_3.12.50.v20170928-1321.jar;^
lib/bundles/com.nomagic.magicdraw.osgi.fragment_20.0.0.202010140732.jar;^
lib/md_api.jar;lib/md_common_api.jar;lib/md.jar;lib/md_common.jar;lib/jna.jar ^
-Dcom.nomagic.magicdraw_home="C:\Program Files\MagicDraw" ^
-Dcom.nomagic.osgi.config.dir=configuration ^
-Dcom.nomagic.magicdraw.launcher=com.nomagic.magicdraw.commandline.CommandLineActionLauncher ^
-Dcom.nomagic.magicdraw.commandline.action=com.nomagic.magicdraw.examples.commandlineplugin.CommandLineActionExample ^
com.nomagic.osgi.launcher.ProductionFrameworkLauncher argument1 argument2
Core batch program command line for Mac OS-X and Linux
> cd MagicDraw_2021x_no_install
> java -Xmx1200M -Xss1024K \
       -Dcom.nomagic.magicdraw_home="/home/user/MagicDraw" \
       -Dcom.nomagic.osgi.config.dir=configuration \
       -Dcom.nomagic.magicdraw.launcher=com.nomagic.magicdraw.examples.imagegenerator.ExportDiagramImages \
       -cp lib/com.nomagic.osgi.launcher-20.0.0-SNAPSHOT.jar:\
`         `lib/bundles/org.eclipse.osgi_3.12.50.v20170928-1321.jar:\
`         `lib/bundles/com.nomagic.magicdraw.osgi.fragment_20.0.0.202010140732.jar:\
`         `lib/md_api.jar:lib/md_common_api.jar:lib/md.jar:lib/md_common.jar:lib/jna.jar \
       -Dmd.additional.class.path=openapi/examples/imagegenerator/imagegenerator.jar \
       com.nomagic.osgi.launcher.ProductionFrameworkLauncher project=project.mdzip destination_dir=out
Plugin batch program command line for Mac OS-X and Linux
> cd MagicDraw_2021x_no_install
> java -Xmx1200M -Xss1024K \
       -Dcom.nomagic.magicdraw_home="/home/user/MagicDraw" \
       -Dcom.nomagic.osgi.config.dir=configuration \
       -Dcom.nomagic.magicdraw.launcher=com.nomagic.magicdraw.commandline.CommandLineActionLauncher \
       -Dcom.nomagic.magicdraw.commandline.action=com.nomagic.magicdraw.examples.commandlineplugin.CommandLineActionExample \
       -cp lib/com.nomagic.osgi.launcher-20.0.0-SNAPSHOT.jar:\
`         `lib/bundles/org.eclipse.osgi_3.12.50.v20170928-1321.jar:\
`         `lib/bundles/com.nomagic.magicdraw.osgi.fragment_20.0.0.202010140732.jar:\
`         `lib/md_api.jar:lib/md_common_api.jar:lib/md.jar:lib/md_common.jar:lib/jna.jar \
       com.nomagic.osgi.launcher.ProductionFrameworkLauncher argument1 argument2

In case the command line program is not started from MagicDraw, the command line becomes a little bit more complex.  The exemplary scripts that can be used to start the command line program are shown in the following script examples. Additionally, the scripts can be used with multiple MagicDraw versions since they construct a startup class path dynamically. In order to use them, the MagicDraw installation location has to be specified by setting the MAGICDRAW_HOME environment variable.

The shell script that starts a command line program on Mac OS-X, Linux, and Windows cygwin/msys:

Command line program shell script that is able to launch program from any directory
#!/bin/bash

if [ -z "$MAGICDRAW_HOME" ]; then
    echo "MAGICDRAW_HOME environment variable not set, please set it to the MagicDraw installation folder"
    exit 1
fi

if [ "$OS" = Windows_NT ]; then
    md_home_url_leader=$(echo "$MAGICDRAW_HOME" | sed -e 's/^/\//' -e 's/ /%20/g' -e 's/\\/\//g')
    md_home_url_base=$(echo "$MAGICDRAW_HOME" | sed -e 's/:/%3A/g' -e 's/ /%20/g' \
                                                    -e 's/\//%2F/g' -e 's/\\/%5C/g')
	cp_delim=";"
else
	md_home_url_leader=$(echo "$MAGICDRAW_HOME" | sed -e 's/ /%20/g')
	md_home_url_base=$(echo "$MAGICDRAW_HOME" | sed -e 's/ /%20/g')
	cp_delim=":"
fi

md_cp_url=file:$md_home_url_leader/bin/magicdraw.properties?base=$md_home_url_base#CLASSPATH

OSGI_LAUNCHER=$(echo "$MAGICDRAW_HOME"/lib/com.nomagic.osgi.launcher-*.jar)
OSGI_FRAMEWORK=$(echo "$MAGICDRAW_HOME"/lib/bundles/org.eclipse.osgi_*.jar)
MD_OSGI_FRAGMENT=$(echo "$MAGICDRAW_HOME"/lib/bundles/com.nomagic.magicdraw.osgi.fragment_*.jar)

CP="${OSGI_LAUNCHER}${cp_delim}${OSGI_FRAMEWORK}${cp_delim}${MD_OSGI_FRAGMENT}${cp_delim}\
`  `$MAGICDRAW_HOME/lib/md_api.jar${cp_delim}$MAGICDRAW_HOME/lib/md_common_api.jar${cp_delim}\
`  `$MAGICDRAW_HOME/lib/md.jar${cp_delim}$MAGICDRAW_HOME/lib/md_common.jar${cp_delim}\
`  `$MAGICDRAW_HOME/lib/jna.jar"

java -Xmx1200M -Xss1024K \
     -Dmd.class.path=$md_cp_url \
     -Dcom.nomagic.osgi.config.dir="$MAGICDRAW_HOME/configuration" \
     -Desi.system.config="$MAGICDRAW_HOME/data/application.conf" \
     -Dlogback.configurationFile="$MAGICDRAW_HOME/data/logback.xml" \
     -Dcom.nomagic.magicdraw.launcher=com.nomagic.magicdraw.examples.imagegenerator.ExportDiagramImages \
     -cp "$CP" \
     -Dmd.additional.class.path="$MAGICDRAW_HOME/openapi/examples/imagegenerator/imagegenerator.jar" \
     com.nomagic.osgi.launcher.ProductionFrameworkLauncher "$@"

A Windows batch file that starts a command line program on Windows:

Command line program Windows batch file that is able to launch program from any directory
@echo off
setlocal EnableExtensions

if "%MAGICDRAW_HOME%" == "" (
    echo MAGICDRAW_HOME environment variable not set, please set it to the MagicDraw installation folder
    exit 1
)

setlocal enableDelayedExpansion
set md_home_url_leader=!MAGICDRAW_HOME: =%%20!
set md_home_url_leader=!md_home_url_leader:\=/!
set md_home_url_leader=/!md_home_url_leader!
set md_home_url_base=!MAGICDRAW_HOME::=%%3A!
set md_home_url_base=!md_home_url_base: =%%20!
set md_home_url_base=!md_home_url_base:/=%%2F!
set md_home_url_base=!md_home_url_base:\=%%5C!
setlocal disableDelayedExpansion

set md_cp_url=file:%md_home_url_leader%/bin/magicdraw.properties?base=%md_home_url_base%#CLASSPATH

for /F %%a in ('dir /b "%MAGICDRAW_HOME%\lib\com.nomagic.osgi.launcher-*.jar"') do ^
set OSGI_LAUNCHER=%MAGICDRAW_HOME%\lib\%%a
for /F %%a in ('dir /b "%MAGICDRAW_HOME%\lib\bundles\org.eclipse.osgi_*.jar"') do ^
set OSGI_FRAMEWORK=%MAGICDRAW_HOME%\lib\bundles\%%a
for /F %%a in ('dir /b "%MAGICDRAW_HOME%\lib\bundles\com.nomagic.magicdraw.osgi.fragment_*.jar"') do ^
set MD_OSGI_FRAGMENT=%MAGICDRAW_HOME%\lib\bundles\%%a

set CP=%OSGI_LAUNCHER%;%OSGI_FRAMEWORK%;%MD_OSGI_FRAGMENT%;^
%MAGICDRAW_HOME%\lib\md_api.jar;%MAGICDRAW_HOME%\lib\md_common_api.jar;^
%MAGICDRAW_HOME%\lib\md.jar;%MAGICDRAW_HOME%\lib\md_common.jar;^
%MAGICDRAW_HOME%\lib\jna.jar

java -Xmx1200M -Xss1024K ^
     -Dmd.class.path=%md_cp_url% ^
     -Dcom.nomagic.osgi.config.dir="%MAGICDRAW_HOME%\configuration" ^
     -Desi.system.config="%MAGICDRAW_HOME%\data\application.conf" ^
     -Dlogback.configurationFile="%MAGICDRAW_HOME%\data\logback.xml" ^
     -Dcom.nomagic.magicdraw.launcher=com.nomagic.magicdraw.examples.imagegenerator.ExportDiagramImages ^
     -cp "%CP%" ^
     -Dmd.additional.class.path="%MAGICDRAW_HOME%\openapi\examples\imagegenerator\imagegenerator.jar" ^
     com.nomagic.osgi.launcher.ProductionFrameworkLauncher %*