如何从Java独立应用程序在前台打开Windows应用程序

时间:2019-01-07 09:00:26

标签: java windows jna

我想从Java代码运行基于Windows的应用程序,例如:

Runtime rt = Runtime.getRuntime();

Process pr = rt.exec("C:\MyWindowsApplication.exe");

此应用程序已正确启动,但是要知道如何确保MyWindowsApplication始终从Java应用程序在前台打开

我尝试使用jna库获取进程的PID编号,然后使用给定的PID编号运行VBScript以将Windows应用程序设置为前台。

预期结果是:如果我从Java中打开任何基于Windows的应用程序,则该应用程序应始终在前台打开。

1 个答案:

答案 0 :(得分:0)

Since you are using JNA already, you probably want to use the SetForegroundWindow function in the User32 class.

You will need a handle to the Window to use that function. You can use JNA's WindowUtils.getAllWindows() to get a list, iterate and match the title or filePath, and then call getHWND() on the matching window.