我可以找出Java程序是使用java还是javaw启动的 [英] Can I find out if the java program was launched using java or javaw

查看:121
本文介绍了我可以找出Java程序是使用java还是javaw启动的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是与其他用户之前的一个问题相关的,要求如何检测该代码是否在eclipse IDE中运行

This is related to an earlier question by a different user, asking How to detect that code is running inside eclipse IDE.

我注意到Eclipse始终使用 javaw 而不是 java 。 (这并不意味着使用 javaw 从Eclipse启动的程序)。

I noticed that Eclipse always launches programs with javaw rather than java. (This does not imply a program launched with javaw was launched from Eclipse).

我可以找到传递的参数使用

I can find the arguments passed using

RuntimeMXBean RuntimemxBean = ManagementFactory.getRuntimeMXBean();
List<String> lst = RuntimemxBean.getInputArguments();
for (int i = 0; i < lst.size(); i++)
    System.out.println(lst.get(i));

但这并不告诉我是否使用 java javaw

But this does not tell me whether it was launched using java or javaw.


  1. 有没有办法找出是否使用 java javaw

  2. 为什么Eclipse使用 javaw 启动程序? li>
  1. Is there any way to find it out whether it was launched using java or javaw?
  2. Why does Eclipse use javaw to launch programs?


推荐答案

System.console()将返回 null ,因为唯一的区别在于使用 java javaw javaw 没有关联的控制台窗口。

System.console() will return null, since the only difference between using java and javaw is that for javaw, there is no associated console window.

这是一个小型测试程序,您可以用来演示:

Here's a small test program you can use to demonstrate that:

import javax.swing.JOptionPane;
public class ConsoleTest {
    public static void main(String[] args) {
        if (System.console() == null) {
            JOptionPane.showMessageDialog(null, "System.console() is null");
        } else {
           JOptionPane.showMessageDialog(null, "System.console() is not null");
        }
    }
}

但是,从内部运行Eclipse, System.console()仍然返回 null ,即使以 java

However, when running from within Eclipse, System.console() will still return null, even when started with java.

在Eclipse的启动配置JRE选项卡中,如果将运行时JRE更改为替代JRE ,则可以将Java可执行文件从 javaw 更改为 java

In Eclipse's launch configuration, JRE tab, if you change the Runtime JRE to Alternate JRE, you can then change the Java executable from javaw to java.

这篇关于我可以找出Java程序是使用java还是javaw启动的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆