1

I try to retrieve the windows directory of my OS. To get the correct path I tried the following 2 commands:

System.getenv().get("WINDIR")
System.getenv().get("SystemRoot")

Both commands work, but the strange thing is, that the first command (WINDIR) returns the path only, if I run the program in debug mode. The latter command (SystemRoot) returns the path only if I run the program not in debug mode.

So this program

public static void main(String[] args) {
    System.out.println(System.getenv().get("WINDIR"));
    System.out.println(System.getenv().get("SystemRoot"));
}

evaluates to

// Debug mode
C:\Windows
null

// No Debug mode
null
C:\Windows

Is this a defined behavior?

(My application is Windows specific, and if i speak of debug mode I mean the default Eclipse "Debug as Java Applicaton" run configiguration)

Sebastian
  • 1,642
  • 13
  • 26
  • Possible duplicate of https://stackoverflow.com/questions/7048216/environment-variables-in-eclipse – jrmullen Jan 22 '18 at 14:30
  • Not quite. I run the program in both cases from eclipse. So the environment is the same wheter I run debug mode or not. – Sebastian Jan 22 '18 at 14:34

1 Answers1

1

System.getEnv() is an overloaded method, one implementation having no parameters and one having a String parameter.

  • static Map getenv​() Returns an unmodifiable string map view of the current system environment.
  • static String getenv​(String name) Gets the value of the specified environment variable.

You are calling the implementatation with no parameters, and then calling get() on the returned Map. From the Javadoc for System.getEnv():

  • For getEnv(): The returned map is typically case-sensitive on all platforms.
  • For getEnv(String): On UNIX systems the alphabetic case of name is typically significant, while on Microsoft Windows systems it is typically not.

So it is essential for your code to be providing the name of the environment variable in the correct case, specifying windir in all lower case, not upper case.

That said, I can't explain the differences you see when running in debug mode at all. If I run the program below - which is just an enhanced version of yours - I get identical results (as expected) regardless of whether it is run in debug mode or not:

System.getenv().get() windir=C:\WINDOWS
System.getenv().get() WINDIR=null
System.getenv().get() systemroot=null
System.getenv().get() SystemRoot=C:\WINDOWS
System.getenv()       windir=C:\WINDOWS
System.getenv()       WINDIR=C:\WINDOWS
System.getenv()       systemroot=C:\WINDOWS
System.getenv()       SystemRoot=C:\WINDOWS

Could you run the code below twice, once in debug mode and once in normal mode, and advise of the results? Also, advise of your environment: Windows version, Eclipse version and Java version.

[This is more of a request for further information than a final answer, but I couldn't fit it all into a comment.]

import java.lang.management.ManagementFactory;
import java.util.regex.Pattern;

public class App {

    private final static Pattern debugPattern = Pattern.compile("-Xdebug|jdwp");

    public static boolean isDebugging() {
        // https://stackoverflow.com/questions/7397584/how-to-know-my-code-is-running-in-debug-mode-in-ide
        // Taken from the code provided by SO user AlexR
        for (String arg : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
            if (debugPattern.matcher(arg).find()) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {

        System.out.println("Running in debug mode? " + App.isDebugging());
        System.out.println("System.getenv().get() windir=" + System.getenv().get("windir"));
        System.out.println("System.getenv().get() WINDIR=" + System.getenv().get("WINDIR"));
        System.out.println("System.getenv().get() systemroot=" + System.getenv().get("systemroot"));
        System.out.println("System.getenv().get() SystemRoot=" + System.getenv().get("SystemRoot"));
        System.out.println("System.getenv()       windir=" + System.getenv("windir"));
        System.out.println("System.getenv()       WINDIR=" + System.getenv("WINDIR"));
        System.out.println("System.getenv()       systemroot=" + System.getenv("systemroot"));
        System.out.println("System.getenv()       SystemRoot=" + System.getenv("SystemRoot"));
    }

}
skomisa
  • 16,436
  • 7
  • 61
  • 102
  • Thanks for your effort. I´ve run the programm and the output is both: more clear and more confusing. While the `System.getenv("...")` version has the same output in normal mode and debug mode, the `System.getenv().get("...")` version is now really confusing. In the normal run mode the uppercase `SystemRoot` and the lowercase `windir` returns the desired value. In debug mode, only the uppercase `WINDIR` returns the correct value. I´ll add the output to your answer. (I´m running Eclipse `Version: Mars.2 Release (4.5.2)` on `Windows 7 64Bit`) – Sebastian Jan 24 '18 at 06:51
  • Do you get the same output? Or is this a plattform specific behaviour? – Sebastian Jan 24 '18 at 06:53
  • @Schlangguru I have already included the output I get in my post above, and I get identical output regardless for the 8 calls to println() whether I am running in debug mode or not. Can you please clarify for which of the 8 tests your results are different to mine? – skomisa Jan 24 '18 at 07:11
  • Re "is this a plattform specific behaviour?"....perhaps. I am running Java 1.8 in Eclipse Oxygen 2 on Windows 10. – skomisa Jan 24 '18 at 07:14
  • I get the same output as you if i´m running in normal mode (no debugging). In the debug mode only the uppercase `System.getenv().get("WINDIR")` and all `System.getenv("...")` return the correct directory. The rest returns `null`. – Sebastian Jan 24 '18 at 07:59