直接的答案是,你不能告诉JVM是如何启动的。
但是对于你的问题中的示例用例,你真的不需要知道JVM是如何启动的。你真正需要知道的是用户是否会看到写入控制台的消息。而这样做的方式是这样的:
if (!file.exists()) {
Console console = System.console();
if (console != null) {
console.format("File doesn't exists%n");
} else if (!GraphicsEnvironment.isHeadless()) {
JOptionPane.showMessage(null, "File doesn't exists");
} else {
// Put it in the log
}
}
对于Console的javadoc,虽然不是水密,强烈地暗示控制台对象(如果存在)写入控制台,不能重定向。
感谢@Stephen Denne为!GraphicsEnvironment.isHeadless()提示。