1. /* ====================================================================
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowledgement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgement may appear in the software itself,
  24. * if and wherever such third-party acknowledgements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Software Foundation.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.commons.lang;
  55. /**
  56. * <p>Helpers for <code>java.lang.System</code>.</p>
  57. *
  58. * <p>If a system property cannot be read due to security restrictions,
  59. * the corresponding field in this class will be set to <code>null</code>
  60. * and a message will be written to <code>System.err</code>.</p>
  61. *
  62. * @author Based on code from Avalon Excalibur
  63. * @author Based on code from Lucene
  64. * @author Stephen Colebourne
  65. * @author <a href="mailto:sdowney@panix.com">Steve Downey</a>
  66. * @author Gary Gregory
  67. * @author Michael Becke
  68. * @author Tetsuya Kaneuchi
  69. * @since 1.0
  70. * @version $Id: SystemUtils.java,v 1.23 2003/08/22 16:34:06 ggregory Exp $
  71. */
  72. public class SystemUtils {
  73. // System property constants
  74. //-----------------------------------------------------------------------
  75. // These MUST be declared first. Other constants depend on this.
  76. /**
  77. * <p>The <code>file.encoding</code> System Property.</p>
  78. * <p>File encoding, such as <code>Cp1252</code>.</p>
  79. *
  80. * <p>Defaults to <code>null</code> if the runtime does not have
  81. * security access to read this property or the property does not exist.</p>
  82. *
  83. * @since 2.0
  84. * @since Java 1.2.
  85. */
  86. public static final String FILE_ENCODING = getSystemProperty("file.encoding");
  87. /**
  88. * <p>The <code>file.separator</code> System Property.
  89. * File separator (<code>"/"</code> on UNIX).</p>
  90. *
  91. * <p>Defaults to <code>null</code> if the runtime does not have
  92. * security access to read this property or the property does not exist.</p>
  93. *
  94. * @since Java 1.1.
  95. */
  96. public static final String FILE_SEPARATOR = getSystemProperty("file.separator");
  97. /**
  98. * <p>The <code>java.class.path</code> System Property. Java class path.</p>
  99. *
  100. * <p>Defaults to <code>null</code> if the runtime does not have
  101. * security access to read this property or the property does not exist.</p>
  102. *
  103. * @since Java 1.1.
  104. */
  105. public static final String JAVA_CLASS_PATH = getSystemProperty("java.class.path");
  106. /**
  107. * <p>The <code>java.class.version</code> System Property.
  108. * Java class format version number.</p>
  109. *
  110. * <p>Defaults to <code>null</code> if the runtime does not have
  111. * security access to read this property or the property does not exist.</p>
  112. *
  113. * @since Java 1.1.
  114. */
  115. public static final String JAVA_CLASS_VERSION = getSystemProperty("java.class.version");
  116. /**
  117. * <p>The <code>java.compiler</code> System Property. Name of JIT compiler to use.
  118. * First in JDK version 1.2. Not used in Sun JDKs after 1.2.</p>
  119. *
  120. * <p>Defaults to <code>null</code> if the runtime does not have
  121. * security access to read this property or the property does not exist.</p>
  122. *
  123. * @since Java 1.2. Not used in Sun versions after 1.2.
  124. */
  125. public static final String JAVA_COMPILER = getSystemProperty("java.compiler");
  126. /**
  127. * <p>The <code>java.ext.dirs</code> System Property. Path of extension directory
  128. * or directories.</p>
  129. *
  130. * <p>Defaults to <code>null</code> if the runtime does not have
  131. * security access to read this property or the property does not exist.</p>
  132. *
  133. * @since Java 1.3
  134. */
  135. public static final String JAVA_EXT_DIRS = getSystemProperty("java.ext.dirs");
  136. /**
  137. * <p>The <code>java.home</code> System Property. Java installation directory.</p>
  138. *
  139. * <p>Defaults to <code>null</code> if the runtime does not have
  140. * security access to read this property or the property does not exist.</p>
  141. *
  142. * @since Java 1.1
  143. */
  144. public static final String JAVA_HOME = getSystemProperty("java.home");
  145. /**
  146. * <p>The <code>java.io.tmpdir</code> System Property. Default temp file path.</p>
  147. *
  148. * <p>Defaults to <code>null</code> if the runtime does not have
  149. * security access to read this property or the property does not exist.</p>
  150. *
  151. * @since Java 1.2
  152. */
  153. public static final String JAVA_IO_TMPDIR = getSystemProperty("java.io.tmpdir");
  154. /**
  155. * <p>The <code>java.library.path</code> System Property. List of paths to search
  156. * when loading libraries.</p>
  157. *
  158. * <p>Defaults to <code>null</code> if the runtime does not have
  159. * security access to read this property or the property does not exist.</p>
  160. *
  161. * @since Java 1.2
  162. */
  163. public static final String JAVA_LIBRARY_PATH = getSystemProperty("java.library.path");
  164. /**
  165. * <p>The <code>java.runtime.name</code> System Property. Java Runtime Environment
  166. * name.</p>
  167. *
  168. * <p>Defaults to <code>null</code> if the runtime does not have
  169. * security access to read this property or the property does not exist.</p>
  170. *
  171. * @since 2.0
  172. * @since Java 1.3
  173. */
  174. public static final String JAVA_RUNTIME_NAME = getSystemProperty("java.runtime.name");
  175. /**
  176. * <p>The <code>java.runtime.version</code> System Property. Java Runtime Environment
  177. * version.</p>
  178. *
  179. * <p>Defaults to <code>null</code> if the runtime does not have
  180. * security access to read this property or the property does not exist.</p>
  181. *
  182. * @since 2.0
  183. * @since Java 1.3
  184. */
  185. public static final String JAVA_RUNTIME_VERSION = getSystemProperty("java.runtime.version");
  186. /**
  187. * <p>The <code>java.specification.name</code> System Property. Java Runtime Environment
  188. * specification name.</p>
  189. *
  190. * <p>Defaults to <code>null</code> if the runtime does not have
  191. * security access to read this property or the property does not exist.</p>
  192. *
  193. * @since Java 1.2
  194. */
  195. public static final String JAVA_SPECIFICATION_NAME = getSystemProperty("java.specification.name");
  196. /**
  197. * <p>The <code>java.specification.vendor</code> System Property. Java Runtime Environment
  198. * specification vendor.</p>
  199. *
  200. * <p>Defaults to <code>null</code> if the runtime does not have
  201. * security access to read this property or the property does not exist.</p>
  202. *
  203. * @since Java 1.2
  204. */
  205. public static final String JAVA_SPECIFICATION_VENDOR = getSystemProperty("java.specification.vendor");
  206. /**
  207. * <p>The <code>java.specification.version</code> System Property. Java Runtime Environment
  208. * specification version.</p>
  209. *
  210. * <p>Defaults to <code>null</code> if the runtime does not have
  211. * security access to read this property or the property does not exist.</p>
  212. *
  213. * @since Java 1.3
  214. */
  215. public static final String JAVA_SPECIFICATION_VERSION = getSystemProperty("java.specification.version");
  216. /**
  217. * <p>The <code>java.vendor</code> System Property. Java vendor-specific string.</p>
  218. *
  219. * <p>Defaults to <code>null</code> if the runtime does not have
  220. * security access to read this property or the property does not exist.</p>
  221. *
  222. * @since Java 1.1
  223. */
  224. public static final String JAVA_VENDOR = getSystemProperty("java.vendor");
  225. /**
  226. * <p>The <code>java.vendor.url</code> System Property. Java vendor URL.</p>
  227. *
  228. * <p>Defaults to <code>null</code> if the runtime does not have
  229. * security access to read this property or the property does not exist.</p>
  230. *
  231. * @since Java 1.1
  232. */
  233. public static final String JAVA_VENDOR_URL = getSystemProperty("java.vendor.url");
  234. /**
  235. * <p>The <code>java.version</code> System Property. Java version number.</p>
  236. *
  237. * <p>Defaults to <code>null</code> if the runtime does not have
  238. * security access to read this property or the property does not exist.</p>
  239. *
  240. * @since Java 1.1
  241. */
  242. public static final String JAVA_VERSION = getSystemProperty("java.version");
  243. /**
  244. * <p>The <code>java.vm.info</code> System Property. Java Virtual Machine implementation
  245. * info.</p>
  246. *
  247. * <p>Defaults to <code>null</code> if the runtime does not have
  248. * security access to read this property or the property does not exist.</p>
  249. *
  250. * @since 2.0
  251. * @since Java 1.2
  252. */
  253. public static final String JAVA_VM_INFO = getSystemProperty("java.vm.info");
  254. /**
  255. * <p>The <code>java.vm.name</code> System Property. Java Virtual Machine implementation
  256. * name.</p>
  257. *
  258. * <p>Defaults to <code>null</code> if the runtime does not have
  259. * security access to read this property or the property does not exist.</p>
  260. *
  261. * @since Java 1.2
  262. */
  263. public static final String JAVA_VM_NAME = getSystemProperty("java.vm.name");
  264. /**
  265. * <p>The <code>java.vm.specification.name</code> System Property. Java Virtual Machine
  266. * specification name.</p>
  267. *
  268. * <p>Defaults to <code>null</code> if the runtime does not have
  269. * security access to read this property or the property does not exist.</p>
  270. *
  271. * @since Java 1.2
  272. */
  273. public static final String JAVA_VM_SPECIFICATION_NAME = getSystemProperty("java.vm.specification.name");
  274. /**
  275. * <p>The <code>java.vm.specification.vendor</code> System Property. Java Virtual
  276. * Machine specification vendor.</p>
  277. *
  278. * <p>Defaults to <code>null</code> if the runtime does not have
  279. * security access to read this property or the property does not exist.</p>
  280. *
  281. * @since Java 1.2
  282. */
  283. public static final String JAVA_VM_SPECIFICATION_VENDOR = getSystemProperty("java.vm.specification.vendor");
  284. /**
  285. * <p>The <code>java.vm.specification.version</code> System Property. Java Virtual Machine
  286. * specification version.</p>
  287. *
  288. * <p>Defaults to <code>null</code> if the runtime does not have
  289. * security access to read this property or the property does not exist.</p>
  290. *
  291. * @since Java 1.2
  292. */
  293. public static final String JAVA_VM_SPECIFICATION_VERSION = getSystemProperty("java.vm.specification.version");
  294. /**
  295. * <p>The <code>java.vm.vendor</code> System Property. Java Virtual Machine implementation
  296. * vendor.</p>
  297. *
  298. * <p>Defaults to <code>null</code> if the runtime does not have
  299. * security access to read this property or the property does not exist.</p>
  300. *
  301. * @since Java 1.2
  302. */
  303. public static final String JAVA_VM_VENDOR = getSystemProperty("java.vm.vendor");
  304. /**
  305. * <p>The <code>java.vm.version</code> System Property. Java Virtual Machine
  306. * implementation version.</p>
  307. *
  308. * <p>Defaults to <code>null</code> if the runtime does not have
  309. * security access to read this property or the property does not exist.</p>
  310. *
  311. * @since Java 1.2
  312. */
  313. public static final String JAVA_VM_VERSION = getSystemProperty("java.vm.version");
  314. /**
  315. * <p>The <code>line.separator</code> System Property. Line separator
  316. * (<code>"\n<"</code> on UNIX).</p>
  317. *
  318. * <p>Defaults to <code>null</code> if the runtime does not have
  319. * security access to read this property or the property does not exist.</p>
  320. *
  321. * @since Java 1.1
  322. */
  323. public static final String LINE_SEPARATOR = getSystemProperty("line.separator");
  324. /**
  325. * <p>The <code>os.arch</code> System Property. Operating system architecture.</p>
  326. *
  327. * <p>Defaults to <code>null</code> if the runtime does not have
  328. * security access to read this property or the property does not exist.</p>
  329. *
  330. * @since Java 1.1
  331. */
  332. public static final String OS_ARCH = getSystemProperty("os.arch");
  333. /**
  334. * <p>The <code>os.name</code> System Property. Operating system name.</p>
  335. *
  336. * <p>Defaults to <code>null</code> if the runtime does not have
  337. * security access to read this property or the property does not exist.</p>
  338. *
  339. * @since Java 1.1
  340. */
  341. public static final String OS_NAME = getSystemProperty("os.name");
  342. /**
  343. * <p>The <code>os.version</code> System Property. Operating system version.</p>
  344. *
  345. * <p>Defaults to <code>null</code> if the runtime does not have
  346. * security access to read this property or the property does not exist.</p>
  347. *
  348. * @since Java 1.1
  349. */
  350. public static final String OS_VERSION = getSystemProperty("os.version");
  351. /**
  352. * <p>The <code>path.separator</code> System Property. Path separator
  353. * (<code>":"</code> on UNIX).</p>
  354. *
  355. * <p>Defaults to <code>null</code> if the runtime does not have
  356. * security access to read this property or the property does not exist.</p>
  357. *
  358. * @since Java 1.1
  359. */
  360. public static final String PATH_SEPARATOR = getSystemProperty("path.separator");
  361. /**
  362. * <p>The <code>user.country</code> or <code>user.region</code> System Property.
  363. * User's country code, such as <code>GB</code>. First in JDK version 1.2 as
  364. * <code>user.region</code>. Renamed to <code>user.country</code> in 1.4</p>
  365. *
  366. * <p>Defaults to <code>null</code> if the runtime does not have
  367. * security access to read this property or the property does not exist.</p>
  368. *
  369. * @since 2.0
  370. * @since Java 1.2
  371. */
  372. public static final String USER_COUNTRY =
  373. (getSystemProperty("user.country") == null ?
  374. getSystemProperty("user.region") : getSystemProperty("user.country"));
  375. /**
  376. * <p>The <code>user.dir</code> System Property. User's current working
  377. * directory.</p>
  378. *
  379. * <p>Defaults to <code>null</code> if the runtime does not have
  380. * security access to read this property or the property does not exist.</p>
  381. *
  382. * @since Java 1.1
  383. */
  384. public static final String USER_DIR = getSystemProperty("user.dir");
  385. /**
  386. * <p>The <code>user.home</code> System Property. User's home directory.</p>
  387. *
  388. * <p>Defaults to <code>null</code> if the runtime does not have
  389. * security access to read this property or the property does not exist.</p>
  390. *
  391. * @since Java 1.1
  392. */
  393. public static final String USER_HOME = getSystemProperty("user.home");
  394. /**
  395. * <p>The <code>user.language</code> System Property. User's language code,
  396. * such as 'en'.</p>
  397. *
  398. * <p>Defaults to <code>null</code> if the runtime does not have
  399. * security access to read this property or the property does not exist.</p>
  400. *
  401. * @since 2.0
  402. * @since Java 1.2
  403. */
  404. public static final String USER_LANGUAGE = getSystemProperty("user.language");
  405. /**
  406. * <p>The <code>user.name</code> System Property. User's account name.</p>
  407. *
  408. * <p>Defaults to <code>null</code> if the runtime does not have
  409. * security access to read this property or the property does not exist.</p>
  410. *
  411. * @since Java 1.1
  412. */
  413. public static final String USER_NAME = getSystemProperty("user.name");
  414. // Java version
  415. //-----------------------------------------------------------------------
  416. // These MUST be declared after those above as they depend on the
  417. // values being set up
  418. /**
  419. * <p>Gets the Java version as a <code>float</code>.</p>
  420. *
  421. * <p>Example return values:</p>
  422. * <ul>
  423. * <li><code>1.2f</code> for JDK 1.2
  424. * <li><code>1.31f</code> for JDK 1.3.1
  425. * </ul>
  426. *
  427. * <p>The field will return zero if {@link #JAVA_VERSION} is <code>null</code>.</p>
  428. *
  429. * @since 2.0
  430. */
  431. public static final float JAVA_VERSION_FLOAT = getJavaVersionAsFloat();
  432. /**
  433. * <p>Gets the Java version as an <code>int</code>.</p>
  434. *
  435. * <p>Example return values:</p>
  436. * <ul>
  437. * <li><code>120</code> for JDK 1.2
  438. * <li><code>131</code> for JDK 1.3.1
  439. * </ul>
  440. *
  441. * <p>The field will return zero if {@link #JAVA_VERSION} is <code>null</code>.</p>
  442. *
  443. * @since 2.0
  444. */
  445. public static final int JAVA_VERSION_INT = getJavaVersionAsInt();
  446. // Java version checks
  447. //-----------------------------------------------------------------------
  448. // These MUST be declared after those above as they depend on the
  449. // values being set up
  450. /**
  451. * <p>Is <code>true</code> if this is Java version 1.1 (also 1.1.x versions).</p>
  452. *
  453. * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
  454. * <code>null</code>.</p>
  455. */
  456. public static final boolean IS_JAVA_1_1 = getJavaVersionMatches("1.1");
  457. /**
  458. * <p>Is <code>true</code> if this is Java version 1.2 (also 1.2.x versions).</p>
  459. *
  460. * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
  461. * <code>null</code>.</p>
  462. */
  463. public static final boolean IS_JAVA_1_2 = getJavaVersionMatches("1.2");
  464. /**
  465. * <p>Is <code>true</code> if this is Java version 1.3 (also 1.3.x versions).</p>
  466. *
  467. * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
  468. * <code>null</code>.</p>
  469. */
  470. public static final boolean IS_JAVA_1_3 = getJavaVersionMatches("1.3");
  471. /**
  472. * <p>Is <code>true</code> if this is Java version 1.4 (also 1.4.x versions).</p>
  473. *
  474. * <p>The field will <code>false</code> false if {@link #JAVA_VERSION} is
  475. * <code>null</code>.</p>
  476. */
  477. public static final boolean IS_JAVA_1_4 = getJavaVersionMatches("1.4");
  478. /**
  479. * <p>Is <code>true</code> if this is Java version 1.5 (also 1.5.x versions).</p>
  480. *
  481. * <p>The field will return <code>false</code> if {@link #JAVA_VERSION} is
  482. * <code>null</code>.</p>
  483. */
  484. public static final boolean IS_JAVA_1_5 = getJavaVersionMatches("1.5");
  485. // Operating system checks
  486. //-----------------------------------------------------------------------
  487. // These MUST be declared after those above as they depend on the
  488. // values being set up
  489. // OS names from http://www.vamphq.com/os.html
  490. // Selected ones included - please advise commons-dev@jakarta.apache.org
  491. // if you want another added or a mistake corrected
  492. /**
  493. * <p>Is <code>true</code> if this is AIX.</p>
  494. *
  495. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  496. * <code>null</code>.</p>
  497. *
  498. * @since 2.0
  499. */
  500. public static final boolean IS_OS_AIX = getOSMatches("AIX");
  501. /**
  502. * <p>Is <code>true</code> if this is HP-UX.</p>
  503. *
  504. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  505. * <code>null</code>.</p>
  506. *
  507. * @since 2.0
  508. */
  509. public static final boolean IS_OS_HP_UX = getOSMatches("HP-UX");
  510. /**
  511. * <p>Is <code>true</code> if this is Irix.</p>
  512. *
  513. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  514. * <code>null</code>.</p>
  515. *
  516. * @since 2.0
  517. */
  518. public static final boolean IS_OS_IRIX = getOSMatches("Irix");
  519. /**
  520. * <p>Is <code>true</code> if this is Linux.</p>
  521. *
  522. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  523. * <code>null</code>.</p>
  524. *
  525. * @since 2.0
  526. */
  527. public static final boolean IS_OS_LINUX = getOSMatches("Linux") || getOSMatches("LINUX");
  528. /**
  529. * <p>Is <code>true</code> if this is Mac.</p>
  530. *
  531. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  532. * <code>null</code>.</p>
  533. *
  534. * @since 2.0
  535. */
  536. public static final boolean IS_OS_MAC = getOSMatches("Mac");
  537. /**
  538. * <p>Is <code>true</code> if this is Mac.</p>
  539. *
  540. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  541. * <code>null</code>.</p>
  542. *
  543. * @since 2.0
  544. */
  545. public static final boolean IS_OS_MAC_OSX = getOSMatches("Mac OS X");
  546. /**
  547. * <p>Is <code>true</code> if this is OS/2.</p>
  548. *
  549. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  550. * <code>null</code>.</p>
  551. *
  552. * @since 2.0
  553. */
  554. public static final boolean IS_OS_OS2 = getOSMatches("OS/2");
  555. /**
  556. * <p>Is <code>true</code> if this is Solaris.</p>
  557. *
  558. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  559. * <code>null</code>.</p>
  560. *
  561. * @since 2.0
  562. */
  563. public static final boolean IS_OS_SOLARIS = getOSMatches("Solaris");
  564. /**
  565. * <p>Is <code>true</code> if this is SunOS.</p>
  566. *
  567. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  568. * <code>null</code>.</p>
  569. *
  570. * @since 2.0
  571. */
  572. public static final boolean IS_OS_SUN_OS = getOSMatches("SunOS");
  573. /**
  574. * <p>Is <code>true</code> if this is Windows.</p>
  575. *
  576. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  577. * <code>null</code>.</p>
  578. *
  579. * @since 2.0
  580. */
  581. public static final boolean IS_OS_WINDOWS = getOSMatches("Windows");
  582. /**
  583. * <p>Is <code>true</code> if this is Windows 2000.</p>
  584. *
  585. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  586. * <code>null</code>.</p>
  587. *
  588. * @since 2.0
  589. */
  590. public static final boolean IS_OS_WINDOWS_2000 = getOSMatches("Windows", "5.0");
  591. /**
  592. * <p>Is <code>true</code> if this is Windows 95.</p>
  593. *
  594. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  595. * <code>null</code>.</p>
  596. *
  597. * @since 2.0
  598. */
  599. public static final boolean IS_OS_WINDOWS_95 = getOSMatches("Windows 9", "4.0");
  600. // JDK 1.2 running on Windows98 returns 'Windows 95', hence the above
  601. /**
  602. * <p>Is <code>true</code> if this is Windows 98.</p>
  603. *
  604. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  605. * <code>null</code>.</p>
  606. *
  607. * @since 2.0
  608. */
  609. public static final boolean IS_OS_WINDOWS_98 = getOSMatches("Windows 9", "4.1");
  610. // JDK 1.2 running on Windows98 returns 'Windows 95', hence the above
  611. /**
  612. * <p>Is <code>true</code> if this is Windows ME.</p>
  613. *
  614. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  615. * <code>null</code>.</p>
  616. *
  617. * @since 2.0
  618. */
  619. public static final boolean IS_OS_WINDOWS_ME = getOSMatches("Windows", "4.9");
  620. // JDK 1.2 running on WindowsME may return 'Windows 95', hence the above
  621. /**
  622. * <p>Is <code>true</code> if this is Windows NT.</p>
  623. *
  624. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  625. * <code>null</code>.</p>
  626. *
  627. * @since 2.0
  628. */
  629. public static final boolean IS_OS_WINDOWS_NT = getOSMatches("Windows NT");
  630. // Windows 2000 returns 'Windows 2000' but may suffer from same JDK1.2 problem
  631. /**
  632. * <p>Is <code>true</code> if this is Windows XP.</p>
  633. *
  634. * <p>The field will return <code>false</code> if <code>OS_NAME</code> is
  635. * <code>null</code>.</p>
  636. *
  637. * @since 2.0
  638. */
  639. public static final boolean IS_OS_WINDOWS_XP = getOSMatches("Windows", "5.1");
  640. // Windows XP returns 'Windows 2000' just for fun...
  641. //-----------------------------------------------------------------------
  642. /**
  643. * <p>SystemUtils instances should NOT be constructed in standard
  644. * programming. Instead, the class should be used as
  645. * <code>SystemUtils.FILE_SEPARATOR</code>.</p>
  646. *
  647. * <p>This constructor is public to permit tools that require a JavaBean
  648. * instance to operate.</p>
  649. */
  650. public SystemUtils() {
  651. }
  652. //-----------------------------------------------------------------------
  653. /**
  654. * <p>Gets the Java version number as a <code>float</code>.</p>
  655. *
  656. * <p>Example return values:</p>
  657. * <ul>
  658. * <li><code>1.2f</code> for JDK 1.2
  659. * <li><code>1.31f</code> for JDK 1.3.1
  660. * </ul>
  661. *
  662. * @return the version, for example 1.31f for JDK 1.3.1
  663. * @deprecated Use {@link #JAVA_VERSION_FLOAT} instead.
  664. * Method will be removed in Commons Lang 3.0.
  665. */
  666. public static float getJavaVersion() {
  667. return JAVA_VERSION_FLOAT;
  668. }
  669. /**
  670. * <p>Gets the Java version number as a <code>float</code>.</p>
  671. *
  672. * <p>Example return values:</p>
  673. * <ul>
  674. * <li><code>1.2f</code> for JDK 1.2
  675. * <li><code>1.31f</code> for JDK 1.3.1
  676. * </ul>
  677. *
  678. * <p>Patch releases are not reported.
  679. * Zero is returned if {@link #JAVA_VERSION} is <code>null</code>.</p>
  680. *
  681. * @return the version, for example 1.31f for JDK 1.3.1
  682. */
  683. private static float getJavaVersionAsFloat() {
  684. if (JAVA_VERSION == null) {
  685. return 0f;
  686. }
  687. String str = JAVA_VERSION.substring(0, 3);
  688. if (JAVA_VERSION.length() >= 5) {
  689. str = str + JAVA_VERSION.substring(4, 5);
  690. }
  691. return Float.parseFloat(str);
  692. }
  693. /**
  694. * <p>Gets the Java version number as an <code>int</code>.</p>
  695. *
  696. * <p>Example return values:</p>
  697. * <ul>
  698. * <li><code>120</code> for JDK 1.2
  699. * <li><code>131</code> for JDK 1.3.1
  700. * </ul>
  701. *
  702. * <p>Patch releases are not reported.
  703. * Zero is returned if {@link #JAVA_VERSION} is <code>null</code>.</p>
  704. *
  705. * @return the version, for example 131 for JDK 1.3.1
  706. */
  707. private static int getJavaVersionAsInt() {
  708. if (JAVA_VERSION == null) {
  709. return 0;
  710. }
  711. String str = JAVA_VERSION.substring(0, 1);
  712. str = str + JAVA_VERSION.substring(2, 3);
  713. if (JAVA_VERSION.length() >= 5) {
  714. str = str + JAVA_VERSION.substring(4, 5);
  715. } else {
  716. str = str + "0";
  717. }
  718. return Integer.parseInt(str);
  719. }
  720. /**
  721. * <p>Decides if the java version matches.</p>
  722. *
  723. * @param versionPrefix the prefix for the java version
  724. * @return true if matches, or false if not or can't determine
  725. */
  726. private static boolean getJavaVersionMatches(String versionPrefix) {
  727. if (JAVA_VERSION == null) {
  728. return false;
  729. }
  730. return JAVA_VERSION.startsWith(versionPrefix);
  731. }
  732. /**
  733. * <p>Decides if the operating system matches.</p>
  734. *
  735. * @param osNamePrefix the prefix for the os name
  736. * @return true if matches, or false if not or can't determine
  737. */
  738. private static boolean getOSMatches(String osNamePrefix) {
  739. if (OS_NAME == null) {
  740. return false;
  741. }
  742. return OS_NAME.startsWith(osNamePrefix);
  743. }
  744. /**
  745. * <p>Decides if the operating system matches.</p>
  746. *
  747. * @param osNamePrefix the prefix for the os name
  748. * @param osVersionPrefix the prefix for the version
  749. * @return true if matches, or false if not or can't determine
  750. */
  751. private static boolean getOSMatches(String osNamePrefix, String osVersionPrefix) {
  752. if (OS_NAME == null || OS_VERSION == null) {
  753. return false;
  754. }
  755. return OS_NAME.startsWith(osNamePrefix) && OS_VERSION.startsWith(osVersionPrefix);
  756. }
  757. //-----------------------------------------------------------------------
  758. /**
  759. * <p>Gets a System property, defaulting to <code>null</code> if the property
  760. * cannot be read.</p>
  761. *
  762. * <p>If a <code>SecurityException</code> is caught, the return
  763. * value is <code>null</code> and a message is written to <code>System.err</code>.</p>
  764. *
  765. * @param property the system property name
  766. * @return the system property value or <code>null</code> if a security problem occurs
  767. */
  768. private static String getSystemProperty(String property) {
  769. try {
  770. return System.getProperty(property);
  771. } catch (SecurityException ex) {
  772. // we are not allowed to look at this property
  773. System.err.println(
  774. "Caught a SecurityException reading the system property '" + property
  775. + "'; the SystemUtils property value will default to null."
  776. );
  777. return null;
  778. }
  779. }
  780. /**
  781. * <p>Is the Java version at least the requested version.</p>
  782. *
  783. * <p>Example input:</p>
  784. * <ul>
  785. * <li><code>1.2f</code> to test for JDK 1.2</li>
  786. * <li><code>1.31f</code> to test for JDK 1.3.1</li>
  787. * </ul>
  788. *
  789. * @param requiredVersion the required version, for example 1.31f
  790. * @return <code>true</code> if the actual version is equal or greater
  791. * than the required version
  792. */
  793. public static boolean isJavaVersionAtLeast(float requiredVersion) {
  794. return (JAVA_VERSION_FLOAT >= requiredVersion);
  795. }
  796. /**
  797. * <p>Is the Java version at least the requested version.</p>
  798. *
  799. * <p>Example input:</p>
  800. * <ul>
  801. * <li><code>120</code> to test for JDK 1.2 or greater</li>
  802. * <li><code>131</code> to test for JDK 1.3.1 or greater</li>
  803. * </ul>
  804. *
  805. * @param requiredVersion the required version, for example 131
  806. * @return <code>true</code> if the actual version is equal or greater
  807. * than the required version
  808. * @since 2.0
  809. */
  810. public static boolean isJavaVersionAtLeast(int requiredVersion) {
  811. return (JAVA_VERSION_INT >= requiredVersion);
  812. }
  813. }