Connecting to a Specific Rhapsody Instance in an External Java Application
Overview
Plugin Helpers
When a Java based helper is added to Rhapsody as a plugin (in a HEP file) then the plugin init method is passed the Rhapsody instance that called it:

Note: This is only true if your code actually uses that argument. If you find your plugin connecting to ther wrong Rhapsody instance, double check all of your other plugin methods and make sure they are not inadvertently calling getActiveRhapsodyApplication !
External Helpers
This is not true of an external Java application called thorugh a HEP file. This means that the external Java application connects to Rhapsody through getActiveRhapsodyApplication and could therefore connect to the 'wrong' Rhapsody instance if there are several currently running:

Connecting to a Specific Instance
The (undocumented) Rhapsody variable $RHPCONNECTIONID may be passed as an argment to the Java application in the HEP file, for example:
name4=Invoke Helper (External With ID)
command4=$APPLICATION_ROOT/jdk/bin/javaw
isSynced4=0
isVisible4=1
applicableToNonStrict4=ModelElement
arguments4="-Djava.library.path="$OMROOT/JavaAPI" -Djava.class.path="$OMROOT/JavaAPI/rhapsody.jar;C:/Workspaces/Eclipse/SimpleHelper/bin" com.ibm.rhapsody.samples.basic.SimpleHelper_Launcher $RHPCONNECTIONID"
initialDir4=$JavaLocation/bin
isMacro4=0
applicableToProfile4=
helperTriggers4=
Rhapsody will fill that variable with the current Rhapsody ID and will expand to values such as:
- "RHAPSODY2.9.0.1.0:5720:x64" for an instance of v9.0.1 64 bit
- "RHAPSODY2.9.0.2.0:1632" for an instance of v9.0.2 32 bit
- "RHAPSODY2.10.0.0.0:5848:x64" for an instance of v10.0.0
This value is then accessible through the main arguments and may be used to connect to the correct Rhapsody instance using getActiveRhapsodyApplicationByID:
public static void main( String[] args )
{
connectToRhapsody( args );
}
private static void connectToRhapsody( String[] args ){
ArrayList<String> lst = new ArrayList<>( Arrays.asList( args ) );if ( lst.isEmpty() ) {connectToActiveRhapsody();
}else{for ( String s : lst ){if ( s.startsWith( "RHAPSODY" ) ){rpAppID = s;rpApp = RhapsodyAppServer.getActiveRhapsodyApplicationByID( rpAppID );
if (rpApp != null) {report( "Connected to Rhapsody by ID: " + rpAppID );return;
}}}}connectToActiveRhapsody();}