Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 views
in Technique[技术] by (71.8m points)

eclipse - Can I find out if the java program was launched using java or javaw

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

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));

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

  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?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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");
        }
    }
}

However, when running from within Eclipse, System.console() will still return null, even when started with 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...