简体   繁体   English

从Java启动应用程序处于最小化状态

[英]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 开始-程序-如果-不已经磨合的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? 无论如何用Java代码(Windows中的.exe,而不是Java应用程序)启动应用程序,并让它开始最小化? 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. 再次澄清问题:Java客户端和.exe在Windows中运行,我真的没有能力编写任何包装器或使用JNI mojo或类似的东西。 I more or less need a pure Java solution. 我或多或少需要一个纯Java解决方案。

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: 仅适用于Windows:

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). 我对Java的细节并不熟悉,但根据我刚看过的网站,如果你使用的是java.awt.Frame(包括来自Swing的JFrame),你应该使用该框架的函数调用setState,它接受Frame.ICONIFIED和Frame.NORMAL作为参数(图标化将是最小化状态)。

How do I minimize a Java application window? 如何最小化Java应用程序窗口?

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. 否则,我不能100%肯定,但我非常怀疑这是可能的。 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. 您必须有一些方法来与Windows窗口管理器进行交互,Windows窗口管理器本质上是特定于平台的,因此Java不太可能包含它。 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. 总有可能有人编写了第三方库来处理任务,但这对我来说似乎不太可能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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