001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.lang3;
018    
019    import java.io.File;
020    
021    /**
022     * <p>
023     * Helpers for {@code java.lang.System}.
024     * </p>
025     * <p>
026     * If a system property cannot be read due to security restrictions, the corresponding field in this class will be set
027     * to {@code null} and a message will be written to {@code System.err}.
028     * </p>
029     * <p>
030     * #ThreadSafe#
031     * </p>
032     *
033     * @since 1.0
034     * @version $Id: SystemUtils.java 1088899 2011-04-05 05:31:27Z bayard $
035     */
036    public class SystemUtils {
037    
038        /**
039         * The prefix String for all Windows OS.
040         */
041        private static final String OS_NAME_WINDOWS_PREFIX = "Windows";
042    
043        // System property constants
044        // -----------------------------------------------------------------------
045        // These MUST be declared first. Other constants depend on this.
046    
047        /**
048         * The System property key for the user home directory.
049         */
050        private static final String USER_HOME_KEY = "user.home";
051    
052        /**
053         * The System property key for the user directory.
054         */
055        private static final String USER_DIR_KEY = "user.dir";
056    
057        /**
058         * The System property key for the Java IO temporary directory.
059         */
060        private static final String JAVA_IO_TMPDIR_KEY = "java.io.tmpdir";
061    
062        /**
063         * The System property key for the Java home directory.
064         */
065        private static final String JAVA_HOME_KEY = "java.home";
066    
067        /**
068         * <p>
069         * The {@code awt.toolkit} System Property.
070         * </p>
071         * <p>
072         * Holds a class name, on Windows XP this is {@code sun.awt.windows.WToolkit}.
073         * </p>
074         * <p>
075         * <b>On platforms without a GUI, this value is {@code null}.</b>
076         * </p>
077         * <p>
078         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
079         * not exist.
080         * </p>
081         * <p>
082         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
083         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
084         * sync with that System property.
085         * </p>
086         *
087         * @since 2.1
088         */
089        public static final String AWT_TOOLKIT = getSystemProperty("awt.toolkit");
090    
091        /**
092         * <p>
093         * The {@code file.encoding} System Property.
094         * </p>
095         * <p>
096         * File encoding, such as {@code Cp1252}.
097         * </p>
098         * <p>
099         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
100         * not exist.
101         * </p>
102         * <p>
103         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
104         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
105         * sync with that System property.
106         * </p>
107         *
108         * @since 2.0
109         * @since Java 1.2
110         */
111        public static final String FILE_ENCODING = getSystemProperty("file.encoding");
112    
113        /**
114         * <p>
115         * The {@code file.separator} System Property. File separator (<code>&quot;/&quot;</code> on UNIX).
116         * </p>
117         * <p>
118         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
119         * not exist.
120         * </p>
121         * <p>
122         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
123         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
124         * sync with that System property.
125         * </p>
126         *
127         * @since Java 1.1
128         */
129        public static final String FILE_SEPARATOR = getSystemProperty("file.separator");
130    
131        /**
132         * <p>
133         * The {@code java.awt.fonts} System Property.
134         * </p>
135         * <p>
136         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
137         * not exist.
138         * </p>
139         * <p>
140         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
141         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
142         * sync with that System property.
143         * </p>
144         *
145         * @since 2.1
146         */
147        public static final String JAVA_AWT_FONTS = getSystemProperty("java.awt.fonts");
148    
149        /**
150         * <p>
151         * The {@code java.awt.graphicsenv} System Property.
152         * </p>
153         * <p>
154         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
155         * not exist.
156         * </p>
157         * <p>
158         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
159         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
160         * sync with that System property.
161         * </p>
162         *
163         * @since 2.1
164         */
165        public static final String JAVA_AWT_GRAPHICSENV = getSystemProperty("java.awt.graphicsenv");
166    
167        /**
168         * <p>
169         * The {@code java.awt.headless} System Property. The value of this property is the String {@code "true"} or
170         * {@code "false"}.
171         * </p>
172         * <p>
173         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
174         * not exist.
175         * </p>
176         * <p>
177         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
178         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
179         * sync with that System property.
180         * </p>
181         *
182         * @see #isJavaAwtHeadless()
183         * @since 2.1
184         * @since Java 1.4
185         */
186        public static final String JAVA_AWT_HEADLESS = getSystemProperty("java.awt.headless");
187    
188        /**
189         * <p>
190         * The {@code java.awt.printerjob} System Property.
191         * </p>
192         * <p>
193         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
194         * not exist.
195         * </p>
196         * <p>
197         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
198         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
199         * sync with that System property.
200         * </p>
201         *
202         * @since 2.1
203         */
204        public static final String JAVA_AWT_PRINTERJOB = getSystemProperty("java.awt.printerjob");
205    
206        /**
207         * <p>
208         * The {@code java.class.path} System Property. Java class path.
209         * </p>
210         * <p>
211         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
212         * not exist.
213         * </p>
214         * <p>
215         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
216         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
217         * sync with that System property.
218         * </p>
219         *
220         * @since Java 1.1
221         */
222        public static final String JAVA_CLASS_PATH = getSystemProperty("java.class.path");
223    
224        /**
225         * <p>
226         * The {@code java.class.version} System Property. Java class format version number.
227         * </p>
228         * <p>
229         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
230         * not exist.
231         * </p>
232         * <p>
233         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
234         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
235         * sync with that System property.
236         * </p>
237         *
238         * @since Java 1.1
239         */
240        public static final String JAVA_CLASS_VERSION = getSystemProperty("java.class.version");
241    
242        /**
243         * <p>
244         * The {@code java.compiler} System Property. Name of JIT compiler to use. First in JDK version 1.2. Not used in Sun
245         * JDKs after 1.2.
246         * </p>
247         * <p>
248         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
249         * not exist.
250         * </p>
251         * <p>
252         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
253         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
254         * sync with that System property.
255         * </p>
256         *
257         * @since Java 1.2. Not used in Sun versions after 1.2.
258         */
259        public static final String JAVA_COMPILER = getSystemProperty("java.compiler");
260    
261        /**
262         * <p>
263         * The {@code java.endorsed.dirs} System Property. Path of endorsed directory or directories.
264         * </p>
265         * <p>
266         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
267         * not exist.
268         * </p>
269         * <p>
270         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
271         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
272         * sync with that System property.
273         * </p>
274         *
275         * @since Java 1.4
276         */
277        public static final String JAVA_ENDORSED_DIRS = getSystemProperty("java.endorsed.dirs");
278    
279        /**
280         * <p>
281         * The {@code java.ext.dirs} System Property. Path of extension directory or directories.
282         * </p>
283         * <p>
284         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
285         * not exist.
286         * </p>
287         * <p>
288         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
289         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
290         * sync with that System property.
291         * </p>
292         *
293         * @since Java 1.3
294         */
295        public static final String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs");
296    
297        /**
298         * <p>
299         * The {@code java.home} System Property. Java installation directory.
300         * </p>
301         * <p>
302         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
303         * not exist.
304         * </p>
305         * <p>
306         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
307         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
308         * sync with that System property.
309         * </p>
310         *
311         * @since Java 1.1
312         */
313        public static final String JAVA_HOME = getSystemProperty(JAVA_HOME_KEY);
314    
315        /**
316         * <p>
317         * The {@code java.io.tmpdir} System Property. Default temp file path.
318         * </p>
319         * <p>
320         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
321         * not exist.
322         * </p>
323         * <p>
324         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
325         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
326         * sync with that System property.
327         * </p>
328         *
329         * @since Java 1.2
330         */
331        public static final String JAVA_IO_TMPDIR = getSystemProperty(JAVA_IO_TMPDIR_KEY);
332    
333        /**
334         * <p>
335         * The {@code java.library.path} System Property. List of paths to search when loading libraries.
336         * </p>
337         * <p>
338         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
339         * not exist.
340         * </p>
341         * <p>
342         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
343         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
344         * sync with that System property.
345         * </p>
346         *
347         * @since Java 1.2
348         */
349        public static final String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path");
350    
351        /**
352         * <p>
353         * The {@code java.runtime.name} System Property. Java Runtime Environment name.
354         * </p>
355         * <p>
356         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
357         * not exist.
358         * </p>
359         * <p>
360         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
361         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
362         * sync with that System property.
363         * </p>
364         *
365         * @since 2.0
366         * @since Java 1.3
367         */
368        public static final String JAVA_RUNTIME_NAME = getSystemProperty("java.runtime.name");
369    
370        /**
371         * <p>
372         * The {@code java.runtime.version} System Property. Java Runtime Environment version.
373         * </p>
374         * <p>
375         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
376         * not exist.
377         * </p>
378         * <p>
379         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
380         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
381         * sync with that System property.
382         * </p>
383         *
384         * @since 2.0
385         * @since Java 1.3
386         */
387        public static final String JAVA_RUNTIME_VERSION = getSystemProperty("java.runtime.version");
388    
389        /**
390         * <p>
391         * The {@code java.specification.name} System Property. Java Runtime Environment specification name.
392         * </p>
393         * <p>
394         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
395         * not exist.
396         * </p>
397         * <p>
398         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
399         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
400         * sync with that System property.
401         * </p>
402         *
403         * @since Java 1.2
404         */
405        public static final String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name");
406    
407        /**
408         * <p>
409         * The {@code java.specification.vendor} System Property. Java Runtime Environment specification vendor.
410         * </p>
411         * <p>
412         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
413         * not exist.
414         * </p>
415         * <p>
416         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
417         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
418         * sync with that System property.
419         * </p>
420         *
421         * @since Java 1.2
422         */
423        public static final String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor");
424    
425        /**
426         * <p>
427         * The {@code java.specification.version} System Property. Java Runtime Environment specification version.
428         * </p>
429         * <p>
430         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
431         * not exist.
432         * </p>
433         * <p>
434         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
435         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
436         * sync with that System property.
437         * </p>
438         *
439         * @since Java 1.3
440         */
441        public static final String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version");
442        private static final JavaVersion JAVA_SPECIFICATION_VERSION_AS_ENUM = JavaVersion.get(JAVA_SPECIFICATION_VERSION);
443    
444        /**
445         * <p>
446         * The {@code java.util.prefs.PreferencesFactory} System Property. A class name.
447         * </p>
448         * <p>
449         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
450         * not exist.
451         * </p>
452         * <p>
453         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
454         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
455         * sync with that System property.
456         * </p>
457         *
458         * @since 2.1
459         * @since Java 1.4
460         */
461        public static final String JAVA_UTIL_PREFS_PREFERENCES_FACTORY =
462            getSystemProperty("java.util.prefs.PreferencesFactory");
463    
464        /**
465         * <p>
466         * The {@code java.vendor} System Property. Java vendor-specific string.
467         * </p>
468         * <p>
469         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
470         * not exist.
471         * </p>
472         * <p>
473         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
474         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
475         * sync with that System property.
476         * </p>
477         *
478         * @since Java 1.1
479         */
480        public static final String JAVA_VENDOR = getSystemProperty("java.vendor");
481    
482        /**
483         * <p>
484         * The {@code java.vendor.url} System Property. Java vendor URL.
485         * </p>
486         * <p>
487         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
488         * not exist.
489         * </p>
490         * <p>
491         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
492         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
493         * sync with that System property.
494         * </p>
495         *
496         * @since Java 1.1
497         */
498        public static final String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url");
499    
500        /**
501         * <p>
502         * The {@code java.version} System Property. Java version number.
503         * </p>
504         * <p>
505         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
506         * not exist.
507         * </p>
508         * <p>
509         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
510         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
511         * sync with that System property.
512         * </p>
513         *
514         * @since Java 1.1
515         */
516        public static final String JAVA_VERSION = getSystemProperty("java.version");
517    
518        /**
519         * <p>
520         * The {@code java.vm.info} System Property. Java Virtual Machine implementation info.
521         * </p>
522         * <p>
523         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
524         * not exist.
525         * </p>
526         * <p>
527         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
528         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
529         * sync with that System property.
530         * </p>
531         *
532         * @since 2.0
533         * @since Java 1.2
534         */
535        public static final String JAVA_VM_INFO = getSystemProperty("java.vm.info");
536    
537        /**
538         * <p>
539         * The {@code java.vm.name} System Property. Java Virtual Machine implementation name.
540         * </p>
541         * <p>
542         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
543         * not exist.
544         * </p>
545         * <p>
546         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
547         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
548         * sync with that System property.
549         * </p>
550         *
551         * @since Java 1.2
552         */
553        public static final String JAVA_VM_NAME = getSystemProperty("java.vm.name");
554    
555        /**
556         * <p>
557         * The {@code java.vm.specification.name} System Property. Java Virtual Machine specification name.
558         * </p>
559         * <p>
560         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
561         * not exist.
562         * </p>
563         * <p>
564         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
565         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
566         * sync with that System property.
567         * </p>
568         *
569         * @since Java 1.2
570         */
571        public static final String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name");
572    
573        /**
574         * <p>
575         * The {@code java.vm.specification.vendor} System Property. Java Virtual Machine specification vendor.
576         * </p>
577         * <p>
578         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
579         * not exist.
580         * </p>
581         * <p>
582         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
583         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
584         * sync with that System property.
585         * </p>
586         *
587         * @since Java 1.2
588         */
589        public static final String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor");
590    
591        /**
592         * <p>
593         * The {@code java.vm.specification.version} System Property. Java Virtual Machine specification version.
594         * </p>
595         * <p>
596         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
597         * not exist.
598         * </p>
599         * <p>
600         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
601         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
602         * sync with that System property.
603         * </p>
604         *
605         * @since Java 1.2
606         */
607        public static final String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version");
608    
609        /**
610         * <p>
611         * The {@code java.vm.vendor} System Property. Java Virtual Machine implementation vendor.
612         * </p>
613         * <p>
614         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
615         * not exist.
616         * </p>
617         * <p>
618         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
619         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
620         * sync with that System property.
621         * </p>
622         *
623         * @since Java 1.2
624         */
625        public static final String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor");
626    
627        /**
628         * <p>
629         * The {@code java.vm.version} System Property. Java Virtual Machine implementation version.
630         * </p>
631         * <p>
632         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
633         * not exist.
634         * </p>
635         * <p>
636         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
637         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
638         * sync with that System property.
639         * </p>
640         *
641         * @since Java 1.2
642         */
643        public static final String JAVA_VM_VERSION = getSystemProperty("java.vm.version");
644    
645        /**
646         * <p>
647         * The {@code line.separator} System Property. Line separator (<code>&quot;\n&quot;</code> on UNIX).
648         * </p>
649         * <p>
650         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
651         * not exist.
652         * </p>
653         * <p>
654         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
655         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
656         * sync with that System property.
657         * </p>
658         *
659         * @since Java 1.1
660         */
661        public static final String LINE_SEPARATOR = getSystemProperty("line.separator");
662    
663        /**
664         * <p>
665         * The {@code os.arch} System Property. Operating system architecture.
666         * </p>
667         * <p>
668         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
669         * not exist.
670         * </p>
671         * <p>
672         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
673         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
674         * sync with that System property.
675         * </p>
676         *
677         * @since Java 1.1
678         */
679        public static final String OS_ARCH = getSystemProperty("os.arch");
680    
681        /**
682         * <p>
683         * The {@code os.name} System Property. Operating system name.
684         * </p>
685         * <p>
686         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
687         * not exist.
688         * </p>
689         * <p>
690         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
691         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
692         * sync with that System property.
693         * </p>
694         *
695         * @since Java 1.1
696         */
697        public static final String OS_NAME = getSystemProperty("os.name");
698    
699        /**
700         * <p>
701         * The {@code os.version} System Property. Operating system version.
702         * </p>
703         * <p>
704         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
705         * not exist.
706         * </p>
707         * <p>
708         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
709         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
710         * sync with that System property.
711         * </p>
712         *
713         * @since Java 1.1
714         */
715        public static final String OS_VERSION = getSystemProperty("os.version");
716    
717        /**
718         * <p>
719         * The {@code path.separator} System Property. Path separator (<code>&quot;:&quot;</code> on UNIX).
720         * </p>
721         * <p>
722         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
723         * not exist.
724         * </p>
725         * <p>
726         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
727         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
728         * sync with that System property.
729         * </p>
730         *
731         * @since Java 1.1
732         */
733        public static final String PATH_SEPARATOR = getSystemProperty("path.separator");
734    
735        /**
736         * <p>
737         * The {@code user.country} or {@code user.region} System Property. User's country code, such as {@code GB}. First
738         * in Java version 1.2 as {@code user.region}. Renamed to {@code user.country} in 1.4
739         * </p>
740         * <p>
741         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
742         * not exist.
743         * </p>
744         * <p>
745         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
746         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
747         * sync with that System property.
748         * </p>
749         *
750         * @since 2.0
751         * @since Java 1.2
752         */
753        public static final String USER_COUNTRY = getSystemProperty("user.country") == null ?
754                getSystemProperty("user.region") : getSystemProperty("user.country");
755    
756        /**
757         * <p>
758         * The {@code user.dir} System Property. User's current working directory.
759         * </p>
760         * <p>
761         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
762         * not exist.
763         * </p>
764         * <p>
765         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
766         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
767         * sync with that System property.
768         * </p>
769         *
770         * @since Java 1.1
771         */
772        public static final String USER_DIR = getSystemProperty(USER_DIR_KEY);
773    
774        /**
775         * <p>
776         * The {@code user.home} System Property. User's home directory.
777         * </p>
778         * <p>
779         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
780         * not exist.
781         * </p>
782         * <p>
783         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
784         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
785         * sync with that System property.
786         * </p>
787         *
788         * @since Java 1.1
789         */
790        public static final String USER_HOME = getSystemProperty(USER_HOME_KEY);
791    
792        /**
793         * <p>
794         * The {@code user.language} System Property. User's language code, such as {@code "en"}.
795         * </p>
796         * <p>
797         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
798         * not exist.
799         * </p>
800         * <p>
801         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
802         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
803         * sync with that System property.
804         * </p>
805         *
806         * @since 2.0
807         * @since Java 1.2
808         */
809        public static final String USER_LANGUAGE = getSystemProperty("user.language");
810    
811        /**
812         * <p>
813         * The {@code user.name} System Property. User's account name.
814         * </p>
815         * <p>
816         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
817         * not exist.
818         * </p>
819         * <p>
820         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
821         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
822         * sync with that System property.
823         * </p>
824         *
825         * @since Java 1.1
826         */
827        public static final String USER_NAME = getSystemProperty("user.name");
828    
829        /**
830         * <p>
831         * The {@code user.timezone} System Property. For example: {@code "America/Los_Angeles"}.
832         * </p>
833         * <p>
834         * Defaults to {@code null} if the runtime does not have security access to read this property or the property does
835         * not exist.
836         * </p>
837         * <p>
838         * This value is initialized when the class is loaded. If {@link System#setProperty(String,String)} or
839         * {@link System#setProperties(java.util.Properties)} is called after this class is loaded, the value will be out of
840         * sync with that System property.
841         * </p>
842         *
843         * @since 2.1
844         */
845        public static final String USER_TIMEZONE = getSystemProperty("user.timezone");
846    
847        // Java version checks
848        // -----------------------------------------------------------------------
849        // These MUST be declared after those above as they depend on the
850        // values being set up
851    
852        /**
853         * <p>
854         * Is {@code true} if this is Java version 1.1 (also 1.1.x versions).
855         * </p>
856         * <p>
857         * The field will return {@code false} if {@link #JAVA_VERSION} is {@code null}.
858         * </p>
859         */
860        public static final boolean IS_JAVA_1_1 = getJavaVersionMatches("1.1");
861    
862        /**
863         * <p>
864         * Is {@code true} if this is Java version 1.2 (also 1.2.x versions).
865         * </p>
866         * <p>
867         * The field will return {@code false} if {@link #JAVA_VERSION} is {@code null}.
868         * </p>
869         */
870        public static final boolean IS_JAVA_1_2 = getJavaVersionMatches("1.2");
871    
872        /**
873         * <p>
874         * Is {@code true} if this is Java version 1.3 (also 1.3.x versions).
875         * </p>
876         * <p>
877         * The field will return {@code false} if {@link #JAVA_VERSION} is {@code null}.
878         * </p>
879         */
880        public static final boolean IS_JAVA_1_3 = getJavaVersionMatches("1.3");
881    
882        /**
883         * <p>
884         * Is {@code true} if this is Java version 1.4 (also 1.4.x versions).
885         * </p>
886         * <p>
887         * The field will return {@code false} if {@link #JAVA_VERSION} is {@code null}.
888         * </p>
889         */
890        public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");
891    
892        /**
893         * <p>
894         * Is {@code true} if this is Java version 1.5 (also 1.5.x versions).
895         * </p>
896         * <p>
897         * The field will return {@code false} if {@link #JAVA_VERSION} is {@code null}.
898         * </p>
899         */
900        public static final boolean IS_JAVA_1_5 = getJavaVersionMatches("1.5");
901    
902        /**
903         * <p>
904         * Is {@code true} if this is Java version 1.6 (also 1.6.x versions).
905         * </p>
906         * <p>
907         * The field will return {@code false} if {@link #JAVA_VERSION} is {@code null}.
908         * </p>
909         */
910        public static final boolean IS_JAVA_1_6 = getJavaVersionMatches("1.6");
911    
912        /**
913         * <p>
914         * Is {@code true} if this is Java version 1.7 (also 1.7.x versions).
915         * </p>
916         * <p>
917         * The field will return {@code false} if {@link #JAVA_VERSION} is {@code null}.
918         * </p>
919         *
920         * @since 3.0
921         */
922        public static final boolean IS_JAVA_1_7 = getJavaVersionMatches("1.7");
923    
924        // Operating system checks
925        // -----------------------------------------------------------------------
926        // These MUST be declared after those above as they depend on the
927        // values being set up
928        // OS names from http://www.vamphq.com/os.html
929        // Selected ones included - please advise dev@commons.apache.org
930        // if you want another added or a mistake corrected
931    
932        /**
933         * <p>
934         * Is {@code true} if this is AIX.
935         * </p>
936         * <p>
937         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
938         * </p>
939         *
940         * @since 2.0
941         */
942        public static final boolean IS_OS_AIX = getOSMatchesName("AIX");
943    
944        /**
945         * <p>
946         * Is {@code true} if this is HP-UX.
947         * </p>
948         * <p>
949         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
950         * </p>
951         *
952         * @since 2.0
953         */
954        public static final boolean IS_OS_HP_UX = getOSMatchesName("HP-UX");
955    
956        /**
957         * <p>
958         * Is {@code true} if this is Irix.
959         * </p>
960         * <p>
961         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
962         * </p>
963         *
964         * @since 2.0
965         */
966        public static final boolean IS_OS_IRIX = getOSMatchesName("Irix");
967    
968        /**
969         * <p>
970         * Is {@code true} if this is Linux.
971         * </p>
972         * <p>
973         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
974         * </p>
975         *
976         * @since 2.0
977         */
978        public static final boolean IS_OS_LINUX = getOSMatchesName("Linux") || getOSMatchesName("LINUX");
979    
980        /**
981         * <p>
982         * Is {@code true} if this is Mac.
983         * </p>
984         * <p>
985         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
986         * </p>
987         *
988         * @since 2.0
989         */
990        public static final boolean IS_OS_MAC = getOSMatchesName("Mac");
991    
992        /**
993         * <p>
994         * Is {@code true} if this is Mac.
995         * </p>
996         * <p>
997         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
998         * </p>
999         *
1000         * @since 2.0
1001         */
1002        public static final boolean IS_OS_MAC_OSX = getOSMatchesName("Mac OS X");
1003    
1004        /**
1005         * <p>
1006         * Is {@code true} if this is OS/2.
1007         * </p>
1008         * <p>
1009         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1010         * </p>
1011         *
1012         * @since 2.0
1013         */
1014        public static final boolean IS_OS_OS2 = getOSMatchesName("OS/2");
1015    
1016        /**
1017         * <p>
1018         * Is {@code true} if this is Solaris.
1019         * </p>
1020         * <p>
1021         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1022         * </p>
1023         *
1024         * @since 2.0
1025         */
1026        public static final boolean IS_OS_SOLARIS = getOSMatchesName("Solaris");
1027    
1028        /**
1029         * <p>
1030         * Is {@code true} if this is SunOS.
1031         * </p>
1032         * <p>
1033         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1034         * </p>
1035         *
1036         * @since 2.0
1037         */
1038        public static final boolean IS_OS_SUN_OS = getOSMatchesName("SunOS");
1039    
1040        /**
1041         * <p>
1042         * Is {@code true} if this is a UNIX like system, as in any of AIX, HP-UX, Irix, Linux, MacOSX, Solaris or SUN OS.
1043         * </p>
1044         * <p>
1045         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1046         * </p>
1047         *
1048         * @since 2.1
1049         */
1050        public static final boolean IS_OS_UNIX = IS_OS_AIX || IS_OS_HP_UX || IS_OS_IRIX || IS_OS_LINUX || IS_OS_MAC_OSX
1051                || IS_OS_SOLARIS || IS_OS_SUN_OS;
1052    
1053        /**
1054         * <p>
1055         * Is {@code true} if this is Windows.
1056         * </p>
1057         * <p>
1058         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1059         * </p>
1060         *
1061         * @since 2.0
1062         */
1063        public static final boolean IS_OS_WINDOWS = getOSMatchesName(OS_NAME_WINDOWS_PREFIX);
1064    
1065        /**
1066         * <p>
1067         * Is {@code true} if this is Windows 2000.
1068         * </p>
1069         * <p>
1070         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1071         * </p>
1072         *
1073         * @since 2.0
1074         */
1075        public static final boolean IS_OS_WINDOWS_2000 = getOSMatches(OS_NAME_WINDOWS_PREFIX, "5.0");
1076    
1077        /**
1078         * <p>
1079         * Is {@code true} if this is Windows 95.
1080         * </p>
1081         * <p>
1082         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1083         * </p>
1084         *
1085         * @since 2.0
1086         */
1087        public static final boolean IS_OS_WINDOWS_95 = getOSMatches(OS_NAME_WINDOWS_PREFIX + " 9", "4.0");
1088        // Java 1.2 running on Windows98 returns 'Windows 95', hence the above
1089    
1090        /**
1091         * <p>
1092         * Is {@code true} if this is Windows 98.
1093         * </p>
1094         * <p>
1095         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1096         * </p>
1097         *
1098         * @since 2.0
1099         */
1100        public static final boolean IS_OS_WINDOWS_98 = getOSMatches(OS_NAME_WINDOWS_PREFIX + " 9", "4.1");
1101        // Java 1.2 running on Windows98 returns 'Windows 95', hence the above
1102    
1103        /**
1104         * <p>
1105         * Is {@code true} if this is Windows ME.
1106         * </p>
1107         * <p>
1108         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1109         * </p>
1110         *
1111         * @since 2.0
1112         */
1113        public static final boolean IS_OS_WINDOWS_ME = getOSMatches(OS_NAME_WINDOWS_PREFIX, "4.9");
1114        // Java 1.2 running on WindowsME may return 'Windows 95', hence the above
1115    
1116        /**
1117         * <p>
1118         * Is {@code true} if this is Windows NT.
1119         * </p>
1120         * <p>
1121         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1122         * </p>
1123         *
1124         * @since 2.0
1125         */
1126        public static final boolean IS_OS_WINDOWS_NT = getOSMatchesName(OS_NAME_WINDOWS_PREFIX + " NT");
1127        // Windows 2000 returns 'Windows 2000' but may suffer from same Java1.2 problem
1128    
1129        /**
1130         * <p>
1131         * Is {@code true} if this is Windows XP.
1132         * </p>
1133         * <p>
1134         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1135         * </p>
1136         *
1137         * @since 2.0
1138         */
1139        public static final boolean IS_OS_WINDOWS_XP = getOSMatches(OS_NAME_WINDOWS_PREFIX, "5.1");
1140    
1141        // -----------------------------------------------------------------------
1142        /**
1143         * <p>
1144         * Is {@code true} if this is Windows Vista.
1145         * </p>
1146         * <p>
1147         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1148         * </p>
1149         *
1150         * @since 2.4
1151         */
1152        public static final boolean IS_OS_WINDOWS_VISTA = getOSMatches(OS_NAME_WINDOWS_PREFIX, "6.0");
1153    
1154        /**
1155         * <p>
1156         * Is {@code true} if this is Windows 7.
1157         * </p>
1158         * <p>
1159         * The field will return {@code false} if {@code OS_NAME} is {@code null}.
1160         * </p>
1161         *
1162         * @since 3.0
1163         */
1164        public static final boolean IS_OS_WINDOWS_7 = getOSMatches(OS_NAME_WINDOWS_PREFIX, "6.1");
1165    
1166        /**
1167         * <p>
1168         * Gets the Java home directory as a {@code File}.
1169         * </p>
1170         *
1171         * @return a directory
1172         * @throws SecurityException if a security manager exists and its {@code checkPropertyAccess} method doesn't allow
1173         * access to the specified system property.
1174         * @see System#getProperty(String)
1175         * @since 2.1
1176         */
1177        public static File getJavaHome() {
1178            return new File(System.getProperty(JAVA_HOME_KEY));
1179        }
1180    
1181        /**
1182         * <p>
1183         * Gets the Java IO temporary directory as a {@code File}.
1184         * </p>
1185         *
1186         * @return a directory
1187         * @throws SecurityException if a security manager exists and its {@code checkPropertyAccess} method doesn't allow
1188         * access to the specified system property.
1189         * @see System#getProperty(String)
1190         * @since 2.1
1191         */
1192        public static File getJavaIoTmpDir() {
1193            return new File(System.getProperty(JAVA_IO_TMPDIR_KEY));
1194        }
1195    
1196        /**
1197         * <p>
1198         * Decides if the Java version matches.
1199         * </p>
1200         *
1201         * @param versionPrefix the prefix for the java version
1202         * @return true if matches, or false if not or can't determine
1203         */
1204        private static boolean getJavaVersionMatches(String versionPrefix) {
1205            return isJavaVersionMatch(JAVA_SPECIFICATION_VERSION, versionPrefix);
1206        }
1207    
1208        /**
1209         * Decides if the operating system matches.
1210         *
1211         * @param osNamePrefix the prefix for the os name
1212         * @param osVersionPrefix the prefix for the version
1213         * @return true if matches, or false if not or can't determine
1214         */
1215        private static boolean getOSMatches(String osNamePrefix, String osVersionPrefix) {
1216            return isOSMatch(OS_NAME, OS_VERSION, osNamePrefix, osVersionPrefix);
1217        }
1218    
1219        /**
1220         * Decides if the operating system matches.
1221         *
1222         * @param osNamePrefix the prefix for the os name
1223         * @return true if matches, or false if not or can't determine
1224         */
1225        private static boolean getOSMatchesName(String osNamePrefix) {
1226            return isOSNameMatch(OS_NAME, osNamePrefix);
1227        }
1228    
1229        // -----------------------------------------------------------------------
1230        /**
1231         * <p>
1232         * Gets a System property, defaulting to {@code null} if the property cannot be read.
1233         * </p>
1234         * <p>
1235         * If a {@code SecurityException} is caught, the return value is {@code null} and a message is written to
1236         * {@code System.err}.
1237         * </p>
1238         *
1239         * @param property the system property name
1240         * @return the system property value or {@code null} if a security problem occurs
1241         */
1242        private static String getSystemProperty(String property) {
1243            try {
1244                return System.getProperty(property);
1245            } catch (SecurityException ex) {
1246                // we are not allowed to look at this property
1247                System.err.println("Caught a SecurityException reading the system property '" + property
1248                        + "'; the SystemUtils property value will default to null.");
1249                return null;
1250            }
1251        }
1252    
1253        /**
1254         * <p>
1255         * Gets the user directory as a {@code File}.
1256         * </p>
1257         *
1258         * @return a directory
1259         * @throws SecurityException if a security manager exists and its {@code checkPropertyAccess} method doesn't allow
1260         * access to the specified system property.
1261         * @see System#getProperty(String)
1262         * @since 2.1
1263         */
1264        public static File getUserDir() {
1265            return new File(System.getProperty(USER_DIR_KEY));
1266        }
1267    
1268        /**
1269         * <p>
1270         * Gets the user home directory as a {@code File}.
1271         * </p>
1272         *
1273         * @return a directory
1274         * @throws SecurityException if a security manager exists and its {@code checkPropertyAccess} method doesn't allow
1275         * access to the specified system property.
1276         * @see System#getProperty(String)
1277         * @since 2.1
1278         */
1279        public static File getUserHome() {
1280            return new File(System.getProperty(USER_HOME_KEY));
1281        }
1282    
1283        /**
1284         * Returns whether the {@link #JAVA_AWT_HEADLESS} value is {@code true}.
1285         *
1286         * @return {@code true} if {@code JAVA_AWT_HEADLESS} is {@code "true"}, {@code false} otherwise.
1287         * @see #JAVA_AWT_HEADLESS
1288         * @since 2.1
1289         * @since Java 1.4
1290         */
1291        public static boolean isJavaAwtHeadless() {
1292            return JAVA_AWT_HEADLESS != null ? JAVA_AWT_HEADLESS.equals(Boolean.TRUE.toString()) : false;
1293        }
1294    
1295        /**
1296         * <p>
1297         * Is the Java version at least the requested version.
1298         * </p>
1299         * <p>
1300         * Example input:
1301         * </p>
1302         * <ul>
1303         * <li>{@code 1.2f} to test for Java 1.2</li>
1304         * <li>{@code 1.31f} to test for Java 1.3.1</li>
1305         * </ul>
1306         *
1307         * @param requiredVersion the required version, for example 1.31f
1308         * @return {@code true} if the actual version is equal or greater than the required version
1309         */
1310        public static boolean isJavaVersionAtLeast(JavaVersion requiredVersion) {
1311            return JAVA_SPECIFICATION_VERSION_AS_ENUM.atLeast(requiredVersion);
1312        }
1313    
1314        /**
1315         * <p>
1316         * Decides if the Java version matches.
1317         * </p>
1318         * <p>
1319         * This method is package private instead of private to support unit test invocation.
1320         * </p>
1321         *
1322         * @param version the actual Java version
1323         * @param versionPrefix the prefix for the expected Java version
1324         * @return true if matches, or false if not or can't determine
1325         */
1326        static boolean isJavaVersionMatch(String version, String versionPrefix) {
1327            if (version == null) {
1328                return false;
1329            }
1330            return version.startsWith(versionPrefix);
1331        }
1332    
1333        /**
1334         * Decides if the operating system matches.
1335         * <p>
1336         * This method is package private instead of private to support unit test invocation.
1337         * </p>
1338         *
1339         * @param osName the actual OS name
1340         * @param osVersion the actual OS version
1341         * @param osNamePrefix the prefix for the expected OS name
1342         * @param osVersionPrefix the prefix for the expected OS version
1343         * @return true if matches, or false if not or can't determine
1344         */
1345        static boolean isOSMatch(String osName, String osVersion, String osNamePrefix, String osVersionPrefix) {
1346            if (osName == null || osVersion == null) {
1347                return false;
1348            }
1349            return osName.startsWith(osNamePrefix) && osVersion.startsWith(osVersionPrefix);
1350        }
1351    
1352        /**
1353         * Decides if the operating system matches.
1354         * <p>
1355         * This method is package private instead of private to support unit test invocation.
1356         * </p>
1357         *
1358         * @param osName the actual OS name
1359         * @param osNamePrefix the prefix for the expected OS name
1360         * @return true if matches, or false if not or can't determine
1361         */
1362        static boolean isOSNameMatch(String osName, String osNamePrefix) {
1363            if (osName == null) {
1364                return false;
1365            }
1366            return osName.startsWith(osNamePrefix);
1367        }
1368    
1369        // -----------------------------------------------------------------------
1370        /**
1371         * <p>
1372         * SystemUtils instances should NOT be constructed in standard programming. Instead, the class should be used as
1373         * {@code SystemUtils.FILE_SEPARATOR}.
1374         * </p>
1375         * <p>
1376         * This constructor is public to permit tools that require a JavaBean instance to operate.
1377         * </p>
1378         */
1379        public SystemUtils() {
1380            super();
1381        }
1382    
1383    }