tags:

views:

871

answers:

3

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

A: 

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?

Michael Todd
As I mentioned above, the application I am launching is a regular .exe not a Java app which I believe is the case in which this would work.
Morinar
Oops. Missed that part (and also goes to show how much I know about Java).
Michael Todd
From what I can tell looking at Runtime.getRunTime() there does not appear to be a way to start a process from Java and do anything other than pass params to it. If the exe has a parameter that means "minimize me", this would work. Otherwise, I'm out of ideas.
Michael Todd
A: 

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.

David Zaslavsky
+4  A: 

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 /?
McDowell
Nice. I've never thought to check start's options.
Michael Myers
This seems perfect, but when run from the Java app, it says "Cannot run program "cmd.exe /C START /MIN C:\path_to_app\app.exe": CreateProcess error=2, The system cannot find the file specified. If I copy that exact line and paste it into a cmd window it works fine.
Morinar
Hmm...maybe it's because I tried to do this with ProcessBuilder rather than Runtime...does that matter?
Morinar
Just tried again with Runtime and it worked fine.
Morinar