从Java在Mac中启动外部安装程序应用程序

时间:2011-12-21 18:04:14

标签: java windows macos runtime

我在Java中创建了一个小应用程序,需要在CD中使用Mac和Windows。

此应用程序的基本思想是只有一个主菜单(Mac和Windows不同),您可以在其中选择多个选项(安装应用程序,查看CD的内容,查看帮助手册等)带有公司徽标......等等。

要安装的应用程序在Windows和Mac中会有所不同。

我想要做的是启动外部安装程序,一旦安装,我想启动应用程序。

我遇到的主要问题是,一旦我在不同的进程中启动了安装程序,waitfor()将返回一个有效的退出值并继续。

我想等到这个应用程序完全安装后再尝试运行它。

for Windows

 Runtime.getRuntime().exec("    \"c:/.../ExternalAppforWin.exe\"");

for Mac

 File instFolder = new File(System.getProperty("user.dir") + "ExternalAppforMac.pkg")
 Process p = Runtime.getRuntime().exec(new String[] { "open", instFolder.toString() });
 int exitVal = p.waitFor();
 if (exitVal==0)

...

你能帮帮我吗?

感谢。

1 个答案:

答案 0 :(得分:0)

您似乎需要检查系统上是否存在安装窗口而不是可执行文件。据我所知,在Java中没有系统独立的方法可以做到这一点,但是使用像sun的JNA这样强大的库(Windows和Mac都支持,可以找到here)你可以做通过适当的OS API调用。

以下是您可能想要在Windows上执行的操作示例,mac调用应该类似:

    import com.sun.jna.platform.win32.User32;
    import com.sun.jna.platform.win32.WinDef;

        .
        .
        .

    //execute process
    Process p = Runtime.getRuntime().exec("    \"c:/.../ExternalAppforWin.exe\"");

    //wait for return value
    int res = p.waitFor();

    //if we have a valid return code begin waiting for window to be closed
    if(res == 0)
    {
        //define a window handle variable
        WinDef.HWND windowHandle = null;
        do
        {
            //sleep a little while before polling the value
            try{Thread.sleep(100);}catch(InterruptedException e){}

            //try to fetch the window by title
            windowHandle = User32.INSTANCE.FindWindow(null, "<Window Title>");

            //if the handle is not null, the window is still open so sleep and then try try again
        }while(windowHandle != null && windowHandle.getPointer() != Pointer.NULL);

        //continue on with your code
    }