33

我想在控制台中显示消息或弹出消息,所以如果未指定参数,我想知道应该显示哪个

就像是:

if( !file.exists() ) {
    if( fromCommandLine()){
        System.out.println("File doesn't exists");
    }else if ( fromDoubleClickOnJar() ) {
        JOptionPane.showMessage(null, "File doesn't exists");
    }
 }
4

7 回答 7

18

直截了当的答案是您无法判断 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虽然不是防水的,但强烈暗示 Console 对象(如果存在)写入控制台并且无法重定向。

感谢@Stephen Denne 的!GraphicsEnvironment.isHeadless()提示。

于 2010-05-20T00:47:27.227 回答
4

我不清楚这个问题,但我会解释它,因为你想区分以下 2

java -jar fred.jar

java package.Main

这是程序的大纲

import sun.jvmstat.monitor.*;
...
HostIdentifier hostId = new HostIdentifier("localhost");
MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(hostId);
Set jvms = monitoredHost.activeVms();
for (Object i: jvms) {
   VmIdentifier id = new VmIdentifier("//" + i + "?mode=r");
   MonitoredVm vm = monitoredHost.getMonitoredVm(id, 0);   
   System.out.println(i);
   System.out.println("\t main class: " + MonitoredVmUtil.mainClass(vm, false));
   System.out.println("\t main args: " + MonitoredVmUtil.mainArgs(vm));
   System.out.println("\t jvmArgs: " + MonitoredVmUtil.jvmArgs(vm));
   monitoredHost.detach(vm);
}

该调用MonitoredVmUtil.mainClass(vm, false)将返回“ jar”或您的主类的名称,例如Main

你必须使用它$JAVA_HOME/lib/tools.jar来编译和运行。

于 2010-05-20T01:51:01.630 回答
3

这个 System.console()技巧似乎奏效了。

这是另一种选择:Class getProtectionDomain()类中有一个方法,可用于了解代码的来源和位置。

有趣的是,这个方法从 1.2 开始可用

我知道我以前用过这个,这是埃里克森 原始答案

这是概念证明:

public class FromJar {
    public static void main( String [] args ) {
        if ( FromJar.class
                 .getProtectionDomain()
                 .getCodeSource()
                 .getLocation()
                 .getFile()
                 .endsWith(".jar") ) {

            javax.swing.JOptionPane.showMessageDialog( null, "Launched from Jar" );

       } else {
            System.out.println("Launched NOT from Jar :P ");
       }
    }
}

这是一个简短的(1m aprox)视频,可以看到这段代码正在运行(并且是用cat :-o 编写的)

Youtube 视频

于 2010-05-20T03:54:44.747 回答
2

您可以尝试:

if (System.console() != null) {
    // Console attached to the JVM: command prompt output
    System.out.println("...");
} else {
    // No console: use Swing
}
于 2010-05-20T00:55:17.873 回答
0

来自http://java.itags.org/java-essentials/15972/

try {
    GraphicsEnvironment.getLocalGraphicsEnvironment();
} catch(Throwable ex) {
    System.out.println("No graphical environment is available.");
}
于 2010-05-20T00:54:13.260 回答
0

确实无法判断 JVM 是如何被调用的。但是......有一种方法可以绕过这个。您假设当用户双击 JAR 时,GUI 正在运行……好吧。所以让我们扩展这个假设。检查..从调用类的位置,目录。检查那个目录..假设它是正常使用,当有一个 *.jar 文件时,那么用户必须从 jar 启动应用程序.. 但一个缺陷是用户也可以单击主类文件。哈哈哈哈

于 2010-05-20T01:33:36.397 回答
0

您可以使用 RuntimeMBean.getInputArguments() 获取所有输入参数,这可用于检测何时启用调试。

编辑:但是, -jar 参数不是其中之一。:(

于 2010-05-20T20:50:41.510 回答