8

I've noticed that the environment in Java on Windows (as obtained by a System.getenv() call) includes some variables that don't exist in the real environment. These begin with and equals-sign and include "=ExitCode" which maps to the exit code of the process that ran just before this java invokation; and the default directories of various drive letters, such as "=C:", "=D:". This seems to be the case with all of Sun's Java versions, running on all Windows versions. Is this documented anywhere, or is it purely for Sun's internal only?

Edit Here's a simple example application to show what I mean. Compile and run this on the command line:

import java.util.Map;
class ShowEnv {
    public static void main(String[] args) {
        for (Map.Entry v : System.getenv().entrySet())
            System.out.printf("%-23s= %.54s%n", v.getKey(), v.getValue());
    }
}

Then compare the variables with the SET command (from cmd.exe) or with similar command-line program written in C. You'll find the variables beginning with = don't exist in those:

=ExitCode              = 00000000
=::                    = ::\
=C:                    = C:\Temp

These variables are obviously added during execution of the JVM.

Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
  • https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getenv() – Garry May 07 '15 at 13:42
  • Although I'm unsure about the documentation - these variables are just the temporary variables that are set at the time that you execute your java command. You're likely unhindered by restrictions in using them however I would consider not as they are not promised to be available, being temporary. – Wayne May 07 '15 at 13:42
  • Share the code and output you refers here. I am working as Java Engineer i can help you to understand the behaviour. – Mohan Raj May 07 '15 at 13:44

2 Answers2

2

System variables that start in equal sign are real. What you observe is not Java adding more environment variables; it is SET command hiding some of the variables.

Windows prohibits the use of equal sign in names of environment variables that users can set, thus reserving variables with = in them for internal use. These variables can be retrieved through windows APIs, e.g. GetEnvironmentStringsW. Java library does not filter this list, so the special variables become available to your code. SET command of Windows, on the other hand, filters them out, creating a discrepancy.

According to this answer, these "magic" variables are there for backward compatibility with ms-dos directory handling, so you can safely ignore them.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    Other developers ran across this issue, too. Here is a [related issue in apache's ant bug repository](https://bz.apache.org/bugzilla/show_bug.cgi?id=49366). – Sergey Kalinichenko May 07 '15 at 14:23
  • Thanks @dasblinkenlight, this is exactly the correct answer! The SET command was misleading me, as well as the getenv() function in C. (P.S. I've just tried to downvote my own question because I drew the wrong conclusions, but it wouldn't let me.) – Klitos Kyriacou May 07 '15 at 15:24
1

Java's System.getenv() shows environment variables what Java sees. If it is different from "real" environment then there difference how you run your Java and "real" environment.

First of all what is "real" for you? Is it cmd window? Then starting cmd takes some steps (for example very obsolete but still active autoexec.bat) and then you have your variables. If "real" means for you Start->Computer->Properties->Advanced System Settings->Environment Variables then you still have to realize that there is some flow how this particular process starts and how it gets it's variables. At least you can see System Variables and User variables and understand that this is not trivial process. Personally, I prefer cmd way because here I cat use command SET and see real variables for current process at current moment. Current moment is another factor because variables can change in time based on some actions as well as they depend on when process started and they are preserved in this process till it dies.

Intent of this small lecture is to show that Java process is complicated and depends on many factors and so it will be different from whatever is "real" for you. Based on values what you provided I guess they could be some left artifacts from process how you run Java. For example your run your application in Eclipse and it has its own environmental settings plus each process has its own settings too. Some variables may come for other variables which are used in Java. _JAVA_OPTIONS would be good example.

Bottom line - if you have difference in environments - find them but don't blame Java in providing them. You are managing your environments, not Java.

Community
  • 1
  • 1
Alex
  • 4,457
  • 2
  • 20
  • 59