我正在使用 Java 创建一个小型应用程序,需要通过 CD 在 Mac 和 Windows 中使用该应用程序。
这个应用程序的基本思想只是有一个主菜单(Mac 和 Windows 有所不同),您可以在其中选择多个选项(安装应用程序、查看 CD 内容、查看帮助手册...等)带有公司 Logo ...等。
要安装的应用程序在 Windows 和 Mac 中会有所不同。
我想做的是启动外部安装程序,安装完成后,我想启动该应用程序。
我遇到的主要问题是,一旦我在不同的进程中启动安装程序,waitfor()就会返回有效的退出值并继续。
我想等到这个应用程序完全安装后再尝试运行它。
适用于 Windows
Runtime.getRuntime().exec(" \"c:/.../ExternalAppforWin.exe\"");
对于 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)
...
你能帮我吗?
谢谢。
最佳答案
看来您需要检查系统上是否存在安装窗口而不是可执行文件。据我所知,在Java中没有独立于系统的方法可以做到这一点,但是通过使用强大的库,如sun的JNA(在windows和mac上都支持,可以找到here),您可以通过以下方式做到这一点适当的操作系统 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
}
关于java - 从 Java 在 Mac 中启动外部安装程序应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8594294/