Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java在运行的windows应用程序之间切换?_Java_Automation_Operating System - Fatal编程技术网

如何使用java在运行的windows应用程序之间切换?

如何使用java在运行的windows应用程序之间切换?,java,automation,operating-system,Java,Automation,Operating System,假设我在eclipse中运行我的程序,它将切换到mozilla窗口(它同时运行)。类似地,当我们单击任务栏中的图标时。我已经尝试了机器人类来刺激点击,但这是硬编码坐标到程序中,我不想这样做 任何关于我如何做的建议。谢谢 据我所知,仅使用核心Java无法按名称切换到另一个正在运行的窗口。您可以通过机器人发送alt-tab键来交换窗口,但这不会显示命名窗口。为此,我建议使用JNI、JNA或某些特定于操作系统的实用程序编程语言,如AutoIt(如果这是Windows问题) 例如,使用JNA,您可以执行

假设我在eclipse中运行我的程序,它将切换到mozilla窗口(它同时运行)。类似地,当我们单击任务栏中的图标时。我已经尝试了机器人类来刺激点击,但这是硬编码坐标到程序中,我不想这样做


任何关于我如何做的建议。谢谢

据我所知,仅使用核心Java无法按名称切换到另一个正在运行的窗口。您可以通过机器人发送alt-tab键来交换窗口,但这不会显示命名窗口。为此,我建议使用JNI、JNA或某些特定于操作系统的实用程序编程语言,如AutoIt(如果这是Windows问题)

例如,使用JNA,您可以执行以下操作:

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.win32.StdCallLibrary;

public class SetForgroundWindowUtil {
   public interface User32 extends StdCallLibrary {
      User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);

      interface WNDENUMPROC extends StdCallCallback {
         boolean callback(Pointer hWnd, Pointer arg);
      }

      boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);

      int GetWindowTextA(Pointer hWnd, byte[] lpString, int nMaxCount);

      int SetForegroundWindow(Pointer hWnd);

      Pointer GetForegroundWindow();
   }

   public static boolean setForegroundWindowByName(final String windowName,
         final boolean starting) {
      final User32 user32 = User32.INSTANCE;
      return user32.EnumWindows(new User32.WNDENUMPROC() {

         @Override
         public boolean callback(Pointer hWnd, Pointer arg) {
            byte[] windowText = new byte[512];
            user32.GetWindowTextA(hWnd, windowText, 512);
            String wText = Native.toString(windowText);
            // if (wText.contains(WINDOW_TEXT_TO_FIND)) {
            if (starting) {
               if (wText.startsWith(windowName)) {
                  user32.SetForegroundWindow(hWnd);
                  return false;
               }
            } else {
               if (wText.contains(windowName)) {
                  user32.SetForegroundWindow(hWnd);
                  return false;
               }
            }
            return true;
         }
      }, null);
   }

   public static void main(String[] args) {
      boolean result = setForegroundWindowByName("Untitled", true);
      System.out.println("result: " + result);
   }
}

我不知道任何操作系统不可知论的方法来解决这个问题。

我相信你的问题可能已经在这里得到了回答:

除此之外,JNA将是实现这一点的最佳选择,核心java或java一般不允许这样做 直接与操作系统交互,但JNA可以

我唯一能想到的另一种方法是调用应用程序,例如使用类似命令的参数(如果需要)调用chrome

编辑:

使用此方法使用参数调用它

Process process = new ProcessBuilder("Location\\to\\the\\program.exe",
          "param1","param2").start();
在linux上,尝试以下操作(这是kotlin代码):


适用于Elemental(Loki)

我已经用JNA或将我的Java程序链接到Windows实用程序编程语言(如AutoIt)完成了这项工作。谢谢,是的,你是对的,我不能使用Alt+Tab(因为Tab序列未知)。我想我必须调查一下JNA。
Process process = new ProcessBuilder("Location\\to\\the\\program.exe",
          "param1","param2").start();
val window = "The window name you want to show"
Runtime.getRuntime().exec(arrayOf("/bin/bash", "-c", "wmctrl -a \"$window\""))