General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion use Java API to check running XMetaL

  • liujason

    use Java API to check running XMetaL

    Participants 3
    Replies 4
    Last Activity 11 years, 11 months ago

    Hi,

    Is it possible to use Java API to check if there is a running XMetaL? So if there is a running XMetaL, get the Application object from the running XMetaL.

    Also, when XMetaL closes, is there a way the Application object will release itself?

    Thanks,

    Jason

    Reply

    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

    liujason

    Reply to: use Java API to check running XMetaL

    Thanks for the reply Derek.

    xmetal=new Application() actually launches an XMetaL Author instance in my test case.
    As soon as the following object initiated, constructor creates an xmetal instance, XMetaL Author is launched.

    [code]
    public class XMetaLWorker {
        Application xmetal=null;
        public XMetaLWorker(){
            try{
                xmetal=new Application();
                xmetal.PreventExit(“ Editor”, “XMetaL cannot be closed because the Editor is running. Close the Editor first, and then close XMetaL.”);
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
    [/code]

    I didn't really want to launch the application, I wanted it to check if an XMetaL instance is running. If there is one running, it will proceed working with the running XMetaL, otherwise, proceed with other business logic.

    Maybe there is another way of achieving this? (XMetaL(R) Author Enterprise 5.5)

    Also, there is another problem. Even I have closed my Java application, XMetaL still displays a notice saying it is running. That is very odd.

    Thanks,

    Jason

    Reply

    Derek Read

    Reply to: use Java API to check running XMetaL

    Interesting, so it sounds like your requirement is really to ask XMetaL if it is running when it is not running (which is not going to be possible). Do I understand correctly?

    I think you will need try to figure out a way to ask the operating system if it has a process called xmetal60.exe (or other version) running, or perhaps there is a 3rd party “process spying” tool or Java JAR that will let you do that. Such as solution would be the same as for any other software I think, so if you could get it working for something simple like notepad.exe then it should work for XMetaL as well.

    I think for your other issue (when you close your Java application) if you want XMetaL Author to be shut down at the same time you will need to call Application.Quit().

    Reply

    liujason

    Reply to: use Java API to check running XMetaL

    Hi Derek,

    Right, I might have to check if xmetal[0-9]{2}.exe is running. That is one way of doing it for sure.

    To my other question, no, I didn't want to close XMetaL. Users would want to have their order of closing applications.

    1. close the tool, keep working in XMetaL, close XMetaL – XMetaL should completely close (no hanging session).

    2. close XMetaL, Java Tool gets a call back (via any listener?), and do some clean up, and reset to initial state (or shut down).

    Are those two scenarios possible?

    Thanks!

    Jason

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support