简体   繁体   中英

How to switch between runnng windows applications using java?

Suppose i run my program in eclipse and it'll switch to mozilla window(it is running simultaneously). Similarly when we click a icon in task bar. I have tried Robot class to stimulate click but that's hard-coding coordinates into the program and i don't want to do that.

Any suggestion how i can do this. Thanks.

As far as I understand things, you cannot switch to another running window by name using just core Java. You can swap windows by sending alt-tab keystrokes via a Robot, but this won't bring up a named window. To do this, I recommend using JNI, JNA or some OS-specific utility programming language, such as AutoIt if this were a Windows issue.

For example, using JNA, you could do something like this:

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);
   }
}

I don't know any OS-agnostic way of solving this problem.

I Believe you're question may have been answered here: Active other process's window in Java .

Other than that, JNA would be your best bet for doing this, core java, or java in general doesn't allow interaction with the operating system directly but JNA does.

The only other way i can think of is call the application, for example chrome with command like arguments(if it takes any) with

try{
    Desktop.getDesktop().open(new File("Location\\to\\the\\program.exe"));
} catch(IOException ex){
    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

edit:

use this method to call it with parameters

Process process = new ProcessBuilder("Location\\to\\the\\program.exe",
          "param1","param2").start();

On linux, try this (this is kotlin code):

val window = "The window name you want to show"
Runtime.getRuntime().exec(arrayOf("/bin/bash", "-c", "wmctrl -a \"$window\""))

Works on Elementary (Loki)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM