src/windows/classes/java/lang/ProcessEnvironment.java

Print this page

        

@@ -141,11 +141,11 @@
                     return new CheckedEntry(i.next());
                 }
                 public void remove() { i.remove();}
             };
         }
-        private static Map.Entry<String,String> checkedEntry (Object o) {
+        private static Map.Entry<String,String> checkedEntry(Object o) {
             Map.Entry<String,String> e = (Map.Entry<String,String>) o;
             nonNullString(e.getKey());
             nonNullString(e.getValue());
             return e;
         }

@@ -297,23 +297,53 @@
         // Sort Unicode-case-insensitively by name
         List<Map.Entry<String,String>> list = new ArrayList<>(entrySet());
         Collections.sort(list, entryComparator);
 
         StringBuilder sb = new StringBuilder(size()*30);
-        for (Map.Entry<String,String> e : list)
-            sb.append(e.getKey())
+        boolean foundSysRoot = false;
+        for (Map.Entry<String,String> e : list) {
+            String key = e.getKey();
+            String val = e.getValue();
+            if (!foundSysRoot) {
+                int cmp = key.compareToIgnoreCase("systemroot");
+                if (cmp == 0) {
+                    foundSysRoot = true;
+                } else if (cmp > 0) {
+                    // SystemRoot not set, so add it here
+                    // must use lowercase name, to preserve sort position
+                    addEnv(sb, "systemroot");
+                    foundSysRoot = true;
+                }
+            }
+            sb.append(key)
               .append('=')
-              .append(e.getValue())
+              .append(val)
               .append('\u0000');
+        }
+        if (!foundSysRoot) {
+            // must use lowercase name, to preserve sort position
+            addEnv(sb, "systemroot");
+        }
         // Ensure double NUL termination,
         // even if environment is empty.
         if (sb.length() == 0)
             sb.append('\u0000');
         sb.append('\u0000');
         return sb.toString();
     }
 
+    // add the environment variable to the child, if it exists in parent
+    private static void addEnv(StringBuilder sb, String envName) {
+        String s = getenv(envName);
+        if (s != null) {
+            sb.append(envName)
+              .append("=")
+              .append(s)
+              .append('\u0000');
+        }
+    }
+
     static String toEnvironmentBlock(Map<String,String> map) {
         return map == null ? null :
             ((ProcessEnvironment)map).toEnvironmentBlock();
     }
 }