You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

JavaEnvUtils.java 21 kB

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.util;
  19. import java.io.BufferedWriter;
  20. import java.io.File;
  21. import java.io.FileWriter;
  22. import java.io.IOException;
  23. import java.util.Vector;
  24. import org.apache.tools.ant.taskdefs.condition.Os;
  25. /**
  26. * A set of helper methods related to locating executables or checking
  27. * conditions of a given Java installation.
  28. *
  29. * @since Ant 1.5
  30. */
  31. public final class JavaEnvUtils {
  32. private JavaEnvUtils() {
  33. }
  34. /** Are we on a DOS-based system */
  35. private static final boolean IS_DOS = Os.isFamily("dos");
  36. /** Are we on Novell NetWare */
  37. private static final boolean IS_NETWARE = Os.isName("netware");
  38. /** Are we on AIX */
  39. private static final boolean IS_AIX = Os.isName("aix");
  40. /** shortcut for System.getProperty("java.home") */
  41. private static final String JAVA_HOME = System.getProperty("java.home");
  42. /** FileUtils instance for path normalization */
  43. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  44. /** Version of currently running VM. */
  45. private static String javaVersion;
  46. /** floating version of the JVM */
  47. private static int javaVersionNumber;
  48. /** Version constant for Java 1.0 */
  49. public static final String JAVA_1_0 = "1.0";
  50. /** Number Version constant for Java 1.0 */
  51. public static final int VERSION_1_0 = 10;
  52. /** Version constant for Java 1.1 */
  53. public static final String JAVA_1_1 = "1.1";
  54. /** Number Version constant for Java 1.1 */
  55. public static final int VERSION_1_1 = 11;
  56. /** Version constant for Java 1.2 */
  57. public static final String JAVA_1_2 = "1.2";
  58. /** Number Version constant for Java 1.2 */
  59. public static final int VERSION_1_2 = 12;
  60. /** Version constant for Java 1.3 */
  61. public static final String JAVA_1_3 = "1.3";
  62. /** Number Version constant for Java 1.3 */
  63. public static final int VERSION_1_3 = 13;
  64. /** Version constant for Java 1.4 */
  65. public static final String JAVA_1_4 = "1.4";
  66. /** Number Version constant for Java 1.4 */
  67. public static final int VERSION_1_4 = 14;
  68. /** Version constant for Java 1.5 */
  69. public static final String JAVA_1_5 = "1.5";
  70. /** Number Version constant for Java 1.5 */
  71. public static final int VERSION_1_5 = 15;
  72. /** Version constant for Java 1.6 */
  73. public static final String JAVA_1_6 = "1.6";
  74. /** Number Version constant for Java 1.6 */
  75. public static final int VERSION_1_6 = 16;
  76. /** Version constant for Java 1.7 */
  77. public static final String JAVA_1_7 = "1.7";
  78. /** Number Version constant for Java 1.7 */
  79. public static final int VERSION_1_7 = 17;
  80. /** Version constant for Java 1.8 */
  81. public static final String JAVA_1_8 = "1.8";
  82. /** Number Version constant for Java 1.8 */
  83. public static final int VERSION_1_8 = 18;
  84. /** Version constant for Java 1.9 */
  85. public static final String JAVA_1_9 = "1.9";
  86. /** Number Version constant for Java 1.9 */
  87. public static final int VERSION_1_9 = 19;
  88. /** Whether this is the Kaffe VM */
  89. private static boolean kaffeDetected;
  90. /** Wheter this is a GNU Classpath based VM */
  91. private static boolean classpathDetected;
  92. /** Whether this is the GNU VM (gcj/gij) */
  93. private static boolean gijDetected;
  94. /** Whether this is Apache Harmony */
  95. private static boolean harmonyDetected;
  96. /** array of packages in the runtime */
  97. private static Vector<String> jrePackages;
  98. static {
  99. // Determine the Java version by looking at available classes
  100. // java.net.Proxy was introduced in JDK 1.5
  101. // java.lang.CharSequence was introduced in JDK 1.4
  102. // java.lang.StrictMath was introduced in JDK 1.3
  103. // java.lang.ThreadLocal was introduced in JDK 1.2
  104. // java.lang.Void was introduced in JDK 1.1
  105. // Count up version until a NoClassDefFoundError ends the try
  106. try {
  107. javaVersion = JAVA_1_0;
  108. javaVersionNumber = VERSION_1_0;
  109. Class.forName("java.lang.Void");
  110. javaVersion = JAVA_1_1;
  111. javaVersionNumber++;
  112. Class.forName("java.lang.ThreadLocal");
  113. javaVersion = JAVA_1_2;
  114. javaVersionNumber++;
  115. Class.forName("java.lang.StrictMath");
  116. javaVersion = JAVA_1_3;
  117. javaVersionNumber++;
  118. Class.forName("java.lang.CharSequence");
  119. javaVersion = JAVA_1_4;
  120. javaVersionNumber++;
  121. Class.forName("java.net.Proxy");
  122. javaVersion = JAVA_1_5;
  123. javaVersionNumber++;
  124. Class.forName("java.net.CookieStore");
  125. javaVersion = JAVA_1_6;
  126. javaVersionNumber++;
  127. Class.forName("java.nio.file.FileSystem");
  128. javaVersion = JAVA_1_7;
  129. javaVersionNumber++;
  130. Class.forName("java.lang.reflect.Executable");
  131. javaVersion = JAVA_1_8;
  132. javaVersionNumber++;
  133. checkForJava9();
  134. javaVersion = JAVA_1_9;
  135. javaVersionNumber++;
  136. } catch (Throwable t) {
  137. // swallow as we've hit the max class version that
  138. // we have
  139. }
  140. kaffeDetected = false;
  141. try {
  142. Class.forName("kaffe.util.NotImplemented");
  143. kaffeDetected = true;
  144. } catch (Throwable t) {
  145. // swallow as this simply doesn't seem to be Kaffe
  146. }
  147. classpathDetected = false;
  148. try {
  149. Class.forName("gnu.classpath.Configuration");
  150. classpathDetected = true;
  151. } catch (Throwable t) {
  152. // swallow as this simply doesn't seem to be GNU classpath based.
  153. }
  154. gijDetected = false;
  155. try {
  156. Class.forName("gnu.gcj.Core");
  157. gijDetected = true;
  158. } catch (Throwable t) {
  159. // swallow as this simply doesn't seem to be gcj/gij
  160. }
  161. harmonyDetected = false;
  162. try {
  163. Class.forName("org.apache.harmony.luni.util.Base64");
  164. harmonyDetected = true;
  165. } catch (Throwable t) {
  166. // swallow as this simply doesn't seem to be Apache Harmony
  167. }
  168. }
  169. /**
  170. * Returns the version of Java this class is running under.
  171. * @return the version of Java as a String, e.g. "1.6"
  172. */
  173. public static String getJavaVersion() {
  174. return javaVersion;
  175. }
  176. /**
  177. * Checks for a give Java 9 runtime.
  178. * At the time of writing the actual version of the JDK was 1.9.0_b06.
  179. * Searching for new classes gave no hits, so we need another aproach.
  180. * Searching for changes (grep -r -i -n "@since 1.9" .) in the sources gave
  181. * only one hit: a new constant in the class SourceVersion.
  182. * So we have to check that ...
  183. *
  184. * @throws Exception if we can't load the class or don't find the new constant.
  185. * This is the behavior when searching for new features on older versions.
  186. * @since Ant 1.9.4
  187. */
  188. private static void checkForJava9() throws Exception {
  189. Class<?> clazz = Class.forName("javax.lang.model.SourceVersion");
  190. clazz.getDeclaredField("RELEASE_9");
  191. }
  192. /**
  193. * Returns the version of Java this class is running under.
  194. * This number can be used for comparisons; it will always be
  195. * @return the version of Java as a number 10x the major/minor,
  196. * e.g Java1.5 has a value of 15
  197. */
  198. public static int getJavaVersionNumber() {
  199. return javaVersionNumber;
  200. }
  201. /**
  202. * Compares the current Java version to the passed in String -
  203. * assumes the argument is one of the constants defined in this
  204. * class.
  205. * Note that Ant now requires JDK 1.4+ so {@link #JAVA_1_0} through
  206. * {@link #JAVA_1_3} need no longer be tested for.
  207. * @param version the version to check against the current version.
  208. * @return true if the version of Java is the same as the given version.
  209. * @since Ant 1.5
  210. */
  211. public static boolean isJavaVersion(String version) {
  212. return javaVersion.equals(version);
  213. }
  214. /**
  215. * Compares the current Java version to the passed in String -
  216. * assumes the argument is one of the constants defined in this
  217. * class.
  218. * Note that Ant now requires JDK 1.4+ so {@link #JAVA_1_0} through
  219. * {@link #JAVA_1_3} need no longer be tested for.
  220. * @param version the version to check against the current version.
  221. * @return true if the version of Java is the same or higher than the
  222. * given version.
  223. * @since Ant 1.7
  224. */
  225. public static boolean isAtLeastJavaVersion(String version) {
  226. return javaVersion.compareTo(version) >= 0;
  227. }
  228. /**
  229. * Checks whether the current Java VM is Kaffe.
  230. * @return true if the current Java VM is Kaffe.
  231. * @since Ant 1.6.3
  232. * @see <a href="http://www.kaffe.org/">http://www.kaffe.org/</a>
  233. */
  234. public static boolean isKaffe() {
  235. return kaffeDetected;
  236. }
  237. /**
  238. * Checks whether the current Java VM is GNU Classpath
  239. * @since Ant 1.9.1
  240. * @return true if the version of Java is GNU Classpath
  241. */
  242. public static boolean isClasspathBased() {
  243. return classpathDetected;
  244. }
  245. /**
  246. * Checks whether the current Java VM is the GNU interpreter gij
  247. * or we are running in a gcj precompiled binary.
  248. * @since Ant 1.8.2
  249. * @return true if the current Java VM is gcj/gij.
  250. */
  251. public static boolean isGij() {
  252. return gijDetected;
  253. }
  254. /**
  255. * Checks whether the current VM is Apache Harmony.
  256. * @since Ant 1.8.2
  257. * @return true if the current VM is Apache Harmony.
  258. */
  259. public static boolean isApacheHarmony() {
  260. return harmonyDetected;
  261. }
  262. /**
  263. * Finds an executable that is part of a JRE installation based on
  264. * the java.home system property.
  265. *
  266. * <p><code>java</code>, <code>keytool</code>,
  267. * <code>policytool</code>, <code>orbd</code>, <code>rmid</code>,
  268. * <code>rmiregistry</code>, <code>servertool</code> and
  269. * <code>tnameserv</code> are JRE executables on Sun based
  270. * JRE's.</p>
  271. *
  272. * <p>You typically find them in <code>JAVA_HOME/jre/bin</code> if
  273. * <code>JAVA_HOME</code> points to your JDK installation. JDK
  274. * &lt; 1.2 has them in the same directory as the JDK
  275. * executables.</p>
  276. * @param command the java executable to find.
  277. * @return the path to the command.
  278. * @since Ant 1.5
  279. */
  280. public static String getJreExecutable(String command) {
  281. if (IS_NETWARE) {
  282. // Extrapolating from:
  283. // "NetWare may have a "java" in that directory, but 99% of
  284. // the time, you don't want to execute it" -- Jeff Tulley
  285. // <JTULLEY@novell.com>
  286. return command;
  287. }
  288. File jExecutable = null;
  289. if (IS_AIX) {
  290. // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
  291. // Sun's layout.
  292. jExecutable = findInDir(JAVA_HOME + "/sh", command);
  293. }
  294. if (jExecutable == null) {
  295. jExecutable = findInDir(JAVA_HOME + "/bin", command);
  296. }
  297. if (jExecutable != null) {
  298. return jExecutable.getAbsolutePath();
  299. } else {
  300. // Unfortunately on Windows java.home doesn't always refer
  301. // to the correct location, so we need to fall back to
  302. // assuming java is somewhere on the PATH.
  303. return addExtension(command);
  304. }
  305. }
  306. /**
  307. * Finds an executable that is part of a JDK installation based on
  308. * the java.home system property.
  309. *
  310. * <p>You typically find them in <code>JAVA_HOME/bin</code> if
  311. * <code>JAVA_HOME</code> points to your JDK installation.</p>
  312. * @param command the java executable to find.
  313. * @return the path to the command.
  314. * @since Ant 1.5
  315. */
  316. public static String getJdkExecutable(String command) {
  317. if (IS_NETWARE) {
  318. // Extrapolating from:
  319. // "NetWare may have a "java" in that directory, but 99% of
  320. // the time, you don't want to execute it" -- Jeff Tulley
  321. // <JTULLEY@novell.com>
  322. return command;
  323. }
  324. File jExecutable = null;
  325. if (IS_AIX) {
  326. // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
  327. // Sun's layout.
  328. jExecutable = findInDir(JAVA_HOME + "/../sh", command);
  329. }
  330. if (jExecutable == null) {
  331. jExecutable = findInDir(JAVA_HOME + "/../bin", command);
  332. }
  333. if (jExecutable != null) {
  334. return jExecutable.getAbsolutePath();
  335. } else {
  336. // fall back to JRE bin directory, also catches JDK 1.0 and 1.1
  337. // where java.home points to the root of the JDK and Mac OS X where
  338. // the whole directory layout is different from Sun's
  339. return getJreExecutable(command);
  340. }
  341. }
  342. /**
  343. * Adds a system specific extension to the name of an executable.
  344. *
  345. * @since Ant 1.5
  346. */
  347. private static String addExtension(String command) {
  348. // This is the most common extension case - exe for windows and OS/2,
  349. // nothing for *nix.
  350. return command + (IS_DOS ? ".exe" : "");
  351. }
  352. /**
  353. * Look for an executable in a given directory.
  354. *
  355. * @return null if the executable cannot be found.
  356. */
  357. private static File findInDir(String dirName, String commandName) {
  358. File dir = FILE_UTILS.normalize(dirName);
  359. File executable = null;
  360. if (dir.exists()) {
  361. executable = new File(dir, addExtension(commandName));
  362. if (!executable.exists()) {
  363. executable = null;
  364. }
  365. }
  366. return executable;
  367. }
  368. /**
  369. * demand creation of the package list.
  370. * When you add a new package, add a new test below.
  371. */
  372. private static void buildJrePackages() {
  373. jrePackages = new Vector<String>();
  374. switch(javaVersionNumber) {
  375. case VERSION_1_9:
  376. case VERSION_1_8:
  377. case VERSION_1_7:
  378. case VERSION_1_6:
  379. case VERSION_1_5:
  380. //In Java1.5, the apache stuff moved.
  381. jrePackages.addElement("com.sun.org.apache");
  382. //fall through.
  383. case VERSION_1_4:
  384. if (javaVersionNumber == VERSION_1_4) {
  385. jrePackages.addElement("org.apache.crimson");
  386. jrePackages.addElement("org.apache.xalan");
  387. jrePackages.addElement("org.apache.xml");
  388. jrePackages.addElement("org.apache.xpath");
  389. }
  390. jrePackages.addElement("org.ietf.jgss");
  391. jrePackages.addElement("org.w3c.dom");
  392. jrePackages.addElement("org.xml.sax");
  393. // fall through
  394. case VERSION_1_3:
  395. jrePackages.addElement("org.omg");
  396. jrePackages.addElement("com.sun.corba");
  397. jrePackages.addElement("com.sun.jndi");
  398. jrePackages.addElement("com.sun.media");
  399. jrePackages.addElement("com.sun.naming");
  400. jrePackages.addElement("com.sun.org.omg");
  401. jrePackages.addElement("com.sun.rmi");
  402. jrePackages.addElement("sunw.io");
  403. jrePackages.addElement("sunw.util");
  404. // fall through
  405. case VERSION_1_2:
  406. jrePackages.addElement("com.sun.java");
  407. jrePackages.addElement("com.sun.image");
  408. // are there any here that we forgot?
  409. // fall through
  410. case VERSION_1_1:
  411. default:
  412. //things like sun.reflection, sun.misc, sun.net
  413. jrePackages.addElement("sun");
  414. jrePackages.addElement("java");
  415. jrePackages.addElement("javax");
  416. break;
  417. }
  418. }
  419. /**
  420. * Testing helper method; kept here for unification of changes.
  421. * @return a list of test classes depending on the java version.
  422. */
  423. public static Vector<String> getJrePackageTestCases() {
  424. Vector<String> tests = new Vector<String>();
  425. tests.addElement("java.lang.Object");
  426. switch(javaVersionNumber) {
  427. case VERSION_1_9:
  428. case VERSION_1_8:
  429. case VERSION_1_7:
  430. case VERSION_1_6:
  431. case VERSION_1_5:
  432. tests.addElement(
  433. "com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl ");
  434. // Fall through
  435. case VERSION_1_4:
  436. tests.addElement("sun.audio.AudioPlayer");
  437. if (javaVersionNumber == VERSION_1_4) {
  438. // only for 1.4, not for higher versions which fall through
  439. tests.addElement("org.apache.crimson.parser.ContentModel");
  440. tests.addElement("org.apache.xalan.processor.ProcessorImport");
  441. tests.addElement("org.apache.xml.utils.URI");
  442. tests.addElement("org.apache.xpath.XPathFactory");
  443. }
  444. tests.addElement("org.ietf.jgss.Oid");
  445. tests.addElement("org.w3c.dom.Attr");
  446. tests.addElement("org.xml.sax.XMLReader");
  447. // fall through
  448. case VERSION_1_3:
  449. tests.addElement("org.omg.CORBA.Any");
  450. tests.addElement("com.sun.corba.se.internal.corba.AnyImpl");
  451. tests.addElement("com.sun.jndi.ldap.LdapURL");
  452. tests.addElement("com.sun.media.sound.Printer");
  453. tests.addElement("com.sun.naming.internal.VersionHelper");
  454. tests.addElement("com.sun.org.omg.CORBA.Initializer");
  455. tests.addElement("sunw.io.Serializable");
  456. tests.addElement("sunw.util.EventListener");
  457. // fall through
  458. case VERSION_1_2:
  459. tests.addElement("javax.accessibility.Accessible");
  460. tests.addElement("sun.misc.BASE64Encoder");
  461. tests.addElement("com.sun.image.codec.jpeg.JPEGCodec");
  462. // fall through
  463. case VERSION_1_1:
  464. default:
  465. //things like sun.reflection, sun.misc, sun.net
  466. tests.addElement("sun.reflect.SerializationConstructorAccessorImpl");
  467. tests.addElement("sun.net.www.http.HttpClient");
  468. tests.addElement("sun.audio.AudioPlayer");
  469. break;
  470. }
  471. return tests;
  472. }
  473. /**
  474. * get a vector of strings of packages built into
  475. * that platforms runtime jar(s)
  476. * @return list of packages.
  477. */
  478. public static Vector<String> getJrePackages() {
  479. if (jrePackages == null) {
  480. buildJrePackages();
  481. }
  482. return jrePackages;
  483. }
  484. /**
  485. *
  486. * Writes the command into a temporary DCL script and returns the
  487. * corresponding File object.
  488. * It is the job of the caller to delete the file on exit.
  489. * @param cmd the command.
  490. * @return the file containing the command.
  491. * @throws IOException if there is an error writing to the file.
  492. */
  493. public static File createVmsJavaOptionFile(String[] cmd)
  494. throws IOException {
  495. File script = FILE_UTILS.createTempFile("ANT", ".JAVA_OPTS", null, false, true);
  496. BufferedWriter out = null;
  497. try {
  498. out = new BufferedWriter(new FileWriter(script));
  499. for (int i = 0; i < cmd.length; i++) {
  500. out.write(cmd[i]);
  501. out.newLine();
  502. }
  503. } finally {
  504. FileUtils.close(out);
  505. }
  506. return script;
  507. }
  508. /**
  509. * Return the value of ${java.home}
  510. * @return the java home value.
  511. */
  512. public static String getJavaHome() {
  513. return JAVA_HOME;
  514. }
  515. }