简体   繁体   中英

Launch Application in a minimized state from Java

This is a followup question to one I previously asked:

start-program-if-not-already-running-in-java

I didn't get a great solution there (as there doesn't appear to be one), but I have a related question:

Is there anyway to launch an application in Java code (an .exe in Windows, not a Java app) and have it start minimized? Or perhaps to minimize it right after start? That would solve the focus issue from the other question and the already running problem would more or less deal with itself.

Clarification issues again: the Java client and the .exe are running in Windows and I really don't have the ability to write any wrappers or make use of JNI mojo or anything like that. I more or less need a pure Java solution.

Again, thanks for the help and I am more than willing to accept an answer that is simply: "This is just not possible."

Windows only:

public class StartWindowMinimized {

  public static void main(String[] args) throws IOException {
    if (args.length != 1) {
      System.err
          .println("Expected: one argument; the command to launch minimized");
    }
    String cmd = "cmd.exe /C START /MIN ";
    Runtime.getRuntime().exec(cmd + args[0]);
  }

}

Sample usage:

java -cp . StartWindowMinimized notepad.exe
java -cp . StartWindowMinimized cmd.exe

To understand the arguments involved:

cmd /?
START /?

I'm not that familiar with the specifics of Java, but according to a web site I just looked at, if you're using java.awt.Frame (which includes JFrame from Swing), you should use the function off of that frame called setState, which accepts Frame.ICONIFIED and Frame.NORMAL as a parameter (iconified would be the minimized state).

How do I minimize a Java application window?

If these apps have command-line switches to make them start minimized, then you can easily use those. Otherwise, I can't be 100% sure, but I highly doubt this is possible. You would have to have some way to interface with the Windows window manager, which is inherently very platform-specific and Java is therefore unlikely to include it. It's always possible that someone has written a third-party library to handle the task but it just doesn't seem likely to me.

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