Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch Application in a minimized state from Java

Tags:

java

windows

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."

like image 412
Morinar Avatar asked Mar 25 '09 21:03

Morinar


1 Answers

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 /?
like image 89
McDowell Avatar answered Oct 15 '22 23:10

McDowell