Can I find out if the java program was launched using java or javaw
Asked Answered
R

3

9

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?
Resentment answered 18/3, 2010 at 10:34 Comment(0)
T
7

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.

Tidwell answered 18/3, 2010 at 10:46 Comment(3)
will it? You can still write to the standard output streams, it will just be silently directed by the operating system to either /dev/null or whatever other streams you've attached to them using the launching command. Same with input streams, you can launch something with javaw and pipe input to it from say a file or other stream.Oleary
@jwenting, yes, you can still write to System.out even though System.console() returns null.Tidwell
Apparently (at least in java 7), if you run in command line redirecting the output to a file, System.console() is also null.Gaygaya
M
4

Checking for System.console() didn't work for me, because of:

  1. It requires JDK 6 or later
  2. Console object is also missing, if application was run through the Runtime.exec(String) method. This was critical for me, because we using a lot of automated script.

So I'm using following solution:

private static boolean isJavaw() {
  try {
    System.in.available();
    return false;
  } catch (IOException e) {
    // invalid handle in case of javaw
    return true;
  }
}

Works fine with JDKs 5, 6 and 7.

Madeleinemadelena answered 10/12, 2012 at 15:41 Comment(0)
A
1

Is there any way to find it out whether it was launched using java or javaw?

According to the documentation: The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.

Why does eclipse use javaw to launch programs?

  1. To change from javaw to java:

    1. Open your launch configuration by choosing 'Run'->'Run...' from the menu bar.
    2. Select your application's launch configuration.
    3. Switch to the 'JRE' page.
    4. Deselect 'Use default Java executable'
    5. Type in 'java' in place of 'javaw'.
  2. I think because eclipse developers develops GUI applications and they don't need console window.

Applesauce answered 18/3, 2010 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.