windows 从 Java 以最小化状态启动应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/683427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Launch Application in a minimized state from Java
提问by Morinar
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.
无论如何要以 Java 代码(Windows 中的 .exe,而不是 Java 应用程序)启动应用程序并使其最小化?或者也许在开始后立即将其最小化?这将解决另一个问题的焦点问题,并且已经运行的问题或多或少会自行解决。
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.
再次澄清问题:Java 客户端和 .exe 在 Windows 中运行,我真的没有能力编写任何包装器或使用 JNI mojo 或类似的东西。我或多或少需要一个纯 Java 解决方案。
Again, thanks for the help and I am more than willing to accept an answer that is simply: "This is just not possible."
再次感谢您的帮助,我非常愿意接受一个简单的答案:“这是不可能的。”
回答by McDowell
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 /?
回答by Michael Todd
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 作为参数(图标化将是最小化状态)。
回答by David Z
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.
如果这些应用程序具有命令行开关来使它们开始最小化,那么您可以轻松使用它们。否则,我不能 100% 确定,但我非常怀疑这是可能的。您必须有某种方式与 Windows 窗口管理器交互,它本质上是特定于平台的,因此 Java 不太可能包含它。总有可能有人编写了第三方库来处理该任务,但对我来说似乎不太可能。