Home Forums General XMetaL Discussion use Java API to check running XMetaL Reply To: use Java API to check running XMetaL

Derek Read

Reply to: use Java API to check running XMetaL

I think the solution you are looking for is in the demo that ships with XMetaL Developer. The demo contains code to gracefully fail when XMetaL Author cannot be instantiated using COM.

The demo can be kicked off by selecting (from the Windows Start menu) Start > All Programs > XMetaL > Developer > Java API Toolkit Demo

With a standard installation of XMetaL Developer 6.0 on XP the file you need to have a look at is located here:

C:Program FilesXMetaL 6.0DeveloperJatDemoXMDemoFrame.java

The code that handles this is just a standard Java try...catch as follows:

[code]  /**Construct the frame*/
  public XMDemoFrame() {
    // try to initialize the XMetaL application object; abandon program if there
    // is a problem instantiating XMetaL
    try {
      xmetal = new Application();
  xmetal.PreventExit(“JavaAPI Demo”, “XMetaL cannot be closed because the Java API Demo is running. Close the Java API Demo first, and then close XMetaL.”);
    }
    catch (Exception ex) {
      System.err.println(“Execution terminated due to the following error:”);
      ex.printStackTrace();
      System.exit(0);
    }

    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }[/code]

That works for total failure. If you want to check specifically for an existing running copy then you should be able to just check to see if an Application object returns anything (in another try…catch). Calling Application.Documents should give you that. If it fails then you would assume there is no running instance. You might also use the PreventExit() API and force your users to always launch and shut down XMetaL Author through your UI. That would eliminate the need to check for an existing running instance from Java (as in the demo).

You could also try to do something else if it fails (offer to launch it, display a dialog with a nice error perhaps, or in the case of the former offer to take the user to your company's software storage location so they can install XMetaL Author, give the phone number for your IT dept, etc) but the basic logic would be similar.

Reply

Products
Downloads
Support