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 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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.File;
  20. import java.io.IOException;
  21. import java.io.PrintWriter;
  22. import java.io.FileWriter;
  23. import java.io.BufferedWriter;
  24. import java.util.Vector;
  25. import org.apache.tools.ant.taskdefs.condition.Os;
  26. /**
  27. * A set of helper methods related to locating executables or checking
  28. * conditons of a given Java installation.
  29. *
  30. * @since Ant 1.5
  31. */
  32. public final class JavaEnvUtils {
  33. private JavaEnvUtils() {
  34. }
  35. /** Are we on a DOS-based system */
  36. private static final boolean IS_DOS = Os.isFamily("dos");
  37. /** Are we on Novell NetWare */
  38. private static final boolean IS_NETWARE = Os.isName("netware");
  39. /** Are we on AIX */
  40. private static final boolean IS_AIX = Os.isName("aix");
  41. /** shortcut for System.getProperty("java.home") */
  42. private static final String JAVA_HOME = System.getProperty("java.home");
  43. /** FileUtils instance for path normalization */
  44. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  45. /** Version of currently running VM. */
  46. private static String javaVersion;
  47. /** floating version of the JVM */
  48. private static int javaVersionNumber;
  49. /** Version constant for Java 1.0 */
  50. public static final String JAVA_1_0 = "1.0";
  51. /** Version constant for Java 1.1 */
  52. public static final String JAVA_1_1 = "1.1";
  53. /** Version constant for Java 1.2 */
  54. public static final String JAVA_1_2 = "1.2";
  55. /** Version constant for Java 1.3 */
  56. public static final String JAVA_1_3 = "1.3";
  57. /** Version constant for Java 1.4 */
  58. public static final String JAVA_1_4 = "1.4";
  59. /** Version constant for Java 1.5 */
  60. public static final String JAVA_1_5 = "1.5";
  61. /** Version constant for Java 1.6 */
  62. public static final String JAVA_1_6 = "1.6";
  63. /** Whether this is the Kaffe VM */
  64. private static boolean kaffeDetected;
  65. /** array of packages in the runtime */
  66. private static Vector jrePackages;
  67. static {
  68. // Determine the Java version by looking at available classes
  69. // java.net.Proxy was introduced in JDK 1.5
  70. // java.lang.CharSequence was introduced in JDK 1.4
  71. // java.lang.StrictMath was introduced in JDK 1.3
  72. // java.lang.ThreadLocal was introduced in JDK 1.2
  73. // java.lang.Void was introduced in JDK 1.1
  74. // Count up version until a NoClassDefFoundError ends the try
  75. try {
  76. javaVersion = JAVA_1_0;
  77. javaVersionNumber = 10;
  78. Class.forName("java.lang.Void");
  79. javaVersion = JAVA_1_1;
  80. javaVersionNumber++;
  81. Class.forName("java.lang.ThreadLocal");
  82. javaVersion = JAVA_1_2;
  83. javaVersionNumber++;
  84. Class.forName("java.lang.StrictMath");
  85. javaVersion = JAVA_1_3;
  86. javaVersionNumber++;
  87. Class.forName("java.lang.CharSequence");
  88. javaVersion = JAVA_1_4;
  89. javaVersionNumber++;
  90. Class.forName("java.net.Proxy");
  91. javaVersion = JAVA_1_5;
  92. javaVersionNumber++;
  93. Class.forName("java.util.Service");
  94. javaVersion = JAVA_1_6;
  95. javaVersionNumber++;
  96. } catch (Throwable t) {
  97. // swallow as we've hit the max class version that
  98. // we have
  99. }
  100. kaffeDetected = false;
  101. try {
  102. Class.forName("kaffe.util.NotImplemented");
  103. kaffeDetected = true;
  104. } catch (Throwable t) {
  105. // swallow as this simply doesn't seem to be Kaffe
  106. }
  107. }
  108. /**
  109. * Returns the version of Java this class is running under.
  110. * @return the version of Java as a String, e.g. "1.1"
  111. */
  112. public static String getJavaVersion() {
  113. return javaVersion;
  114. }
  115. /**
  116. * Returns the version of Java this class is running under.
  117. * This number can be used for comparisions; it will always be
  118. * @return the version of Java as a number 10x the major/minor,
  119. * e.g Java1.5 has a value of 15
  120. */
  121. public static int getJavaVersionNumber() {
  122. return javaVersionNumber;
  123. }
  124. /**
  125. * Compares the current Java version to the passed in String -
  126. * assumes the argument is one of the constants defined in this
  127. * class.
  128. * Note that Ant now requires JDK 1.2+ so {@link #JAVA_1_0} and
  129. * {@link #JAVA_1_1} need no longer be tested for.
  130. * @param version the version to check against the current version.
  131. * @return true if the version of Java is the same as the given version.
  132. * @since Ant 1.5
  133. */
  134. public static boolean isJavaVersion(String version) {
  135. return javaVersion.equals(version);
  136. }
  137. /**
  138. * Compares the current Java version to the passed in String -
  139. * assumes the argument is one of the constants defined in this
  140. * class.
  141. * Note that Ant now requires JDK 1.2+ so {@link #JAVA_1_0} and
  142. * {@link #JAVA_1_1} need no longer be tested for.
  143. * @param version the version to check against the current version.
  144. * @return true if the version of Java is the same or higher than the
  145. * given version.
  146. * @since Ant 1.7
  147. */
  148. public static boolean isAtLeastJavaVersion(String version) {
  149. return javaVersion.compareTo(version) >= 0;
  150. }
  151. /**
  152. * Checks whether the current Java VM is Kaffe.
  153. * @return true if the current Java VM is Kaffe.
  154. * @since Ant 1.6.3
  155. * @see <a href="http://www.kaffe.org/">http://www.kaffe.org/</a>
  156. */
  157. public static boolean isKaffe() {
  158. return kaffeDetected;
  159. }
  160. /**
  161. * Finds an executable that is part of a JRE installation based on
  162. * the java.home system property.
  163. *
  164. * <p><code>java</code>, <code>keytool</code>,
  165. * <code>policytool</code>, <code>orbd</code>, <code>rmid</code>,
  166. * <code>rmiregistry</code>, <code>servertool</code> and
  167. * <code>tnameserv</code> are JRE executables on Sun based
  168. * JRE's.</p>
  169. *
  170. * <p>You typically find them in <code>JAVA_HOME/jre/bin</code> if
  171. * <code>JAVA_HOME</code> points to your JDK installation. JDK
  172. * &lt; 1.2 has them in the same directory as the JDK
  173. * executables.</p>
  174. * @param command the java executable to find.
  175. * @return the path to the command.
  176. * @since Ant 1.5
  177. */
  178. public static String getJreExecutable(String command) {
  179. if (IS_NETWARE) {
  180. // Extrapolating from:
  181. // "NetWare may have a "java" in that directory, but 99% of
  182. // the time, you don't want to execute it" -- Jeff Tulley
  183. // <JTULLEY@novell.com>
  184. return command;
  185. }
  186. File jExecutable = null;
  187. if (IS_AIX) {
  188. // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
  189. // Sun's layout.
  190. jExecutable = findInDir(JAVA_HOME + "/sh", command);
  191. }
  192. if (jExecutable == null) {
  193. jExecutable = findInDir(JAVA_HOME + "/bin", command);
  194. }
  195. if (jExecutable != null) {
  196. return jExecutable.getAbsolutePath();
  197. } else {
  198. // Unfortunately on Windows java.home doesn't always refer
  199. // to the correct location, so we need to fall back to
  200. // assuming java is somewhere on the PATH.
  201. return addExtension(command);
  202. }
  203. }
  204. /**
  205. * Finds an executable that is part of a JDK installation based on
  206. * the java.home system property.
  207. *
  208. * <p>You typically find them in <code>JAVA_HOME/bin</code> if
  209. * <code>JAVA_HOME</code> points to your JDK installation.</p>
  210. * @param command the java executable to find.
  211. * @return the path to the command.
  212. * @since Ant 1.5
  213. */
  214. public static String getJdkExecutable(String command) {
  215. if (IS_NETWARE) {
  216. // Extrapolating from:
  217. // "NetWare may have a "java" in that directory, but 99% of
  218. // the time, you don't want to execute it" -- Jeff Tulley
  219. // <JTULLEY@novell.com>
  220. return command;
  221. }
  222. File jExecutable = null;
  223. if (IS_AIX) {
  224. // On IBM's JDK 1.2 the directory layout is different, 1.3 follows
  225. // Sun's layout.
  226. jExecutable = findInDir(JAVA_HOME + "/../sh", command);
  227. }
  228. if (jExecutable == null) {
  229. jExecutable = findInDir(JAVA_HOME + "/../bin", command);
  230. }
  231. if (jExecutable != null) {
  232. return jExecutable.getAbsolutePath();
  233. } else {
  234. // fall back to JRE bin directory, also catches JDK 1.0 and 1.1
  235. // where java.home points to the root of the JDK and Mac OS X where
  236. // the whole directory layout is different from Sun's
  237. return getJreExecutable(command);
  238. }
  239. }
  240. /**
  241. * Adds a system specific extension to the name of an executable.
  242. *
  243. * @since Ant 1.5
  244. */
  245. private static String addExtension(String command) {
  246. // This is the most common extension case - exe for windows and OS/2,
  247. // nothing for *nix.
  248. return command + (IS_DOS ? ".exe" : "");
  249. }
  250. /**
  251. * Look for an executable in a given directory.
  252. *
  253. * @return null if the executable cannot be found.
  254. */
  255. private static File findInDir(String dirName, String commandName) {
  256. File dir = FILE_UTILS.normalize(dirName);
  257. File executable = null;
  258. if (dir.exists()) {
  259. executable = new File(dir, addExtension(commandName));
  260. if (!executable.exists()) {
  261. executable = null;
  262. }
  263. }
  264. return executable;
  265. }
  266. /**
  267. * demand creation of the package list.
  268. * When you add a new package, add a new test below.
  269. */
  270. private static void buildJrePackages() {
  271. jrePackages = new Vector();
  272. switch(javaVersionNumber) {
  273. case 16:
  274. case 15:
  275. //In Java1.5, the apache stuff moved.
  276. jrePackages.addElement("com.sun.org.apache");
  277. //fall through.
  278. case 14:
  279. if (javaVersionNumber == 14) {
  280. jrePackages.addElement("org.apache.crimson");
  281. jrePackages.addElement("org.apache.xalan");
  282. jrePackages.addElement("org.apache.xml");
  283. jrePackages.addElement("org.apache.xpath");
  284. }
  285. jrePackages.addElement("org.ietf.jgss");
  286. jrePackages.addElement("org.w3c.dom");
  287. jrePackages.addElement("org.xml.sax");
  288. // fall through
  289. case 13:
  290. jrePackages.addElement("org.omg");
  291. jrePackages.addElement("com.sun.corba");
  292. jrePackages.addElement("com.sun.jndi");
  293. jrePackages.addElement("com.sun.media");
  294. jrePackages.addElement("com.sun.naming");
  295. jrePackages.addElement("com.sun.org.omg");
  296. jrePackages.addElement("com.sun.rmi");
  297. jrePackages.addElement("sunw.io");
  298. jrePackages.addElement("sunw.util");
  299. // fall through
  300. case 12:
  301. jrePackages.addElement("com.sun.java");
  302. jrePackages.addElement("com.sun.image");
  303. // are there any here that we forgot?
  304. // fall through
  305. case 11:
  306. default:
  307. //things like sun.reflection, sun.misc, sun.net
  308. jrePackages.addElement("sun");
  309. jrePackages.addElement("java");
  310. jrePackages.addElement("javax");
  311. break;
  312. }
  313. }
  314. /**
  315. * Testing helper method; kept here for unification of changes.
  316. * @return a list of test classes depending on the java version.
  317. */
  318. public static Vector getJrePackageTestCases() {
  319. Vector tests = new Vector();
  320. tests.addElement("java.lang.Object");
  321. switch(javaVersionNumber) {
  322. case 16:
  323. case 15:
  324. tests.addElement(
  325. "com.sun.org.apache.xerces.internal.jaxp.datatype.DatatypeFactoryImpl ");
  326. // Fall tru
  327. case 14:
  328. tests.addElement("sun.audio.AudioPlayer");
  329. if (javaVersionNumber == 14) {
  330. tests.addElement("org.apache.crimson.parser.ContentModel");
  331. tests.addElement("org.apache.xalan.processor.ProcessorImport");
  332. tests.addElement("org.apache.xml.utils.URI");
  333. tests.addElement("org.apache.xpath.XPathFactory");
  334. }
  335. tests.addElement("org.ietf.jgss.Oid");
  336. tests.addElement("org.w3c.dom.Attr");
  337. tests.addElement("org.xml.sax.XMLReader");
  338. // fall through
  339. case 13:
  340. tests.addElement("org.omg.CORBA.Any");
  341. tests.addElement("com.sun.corba.se.internal.corba.AnyImpl");
  342. tests.addElement("com.sun.jndi.ldap.LdapURL");
  343. tests.addElement("com.sun.media.sound.Printer");
  344. tests.addElement("com.sun.naming.internal.VersionHelper");
  345. tests.addElement("com.sun.org.omg.CORBA.Initializer");
  346. tests.addElement("sunw.io.Serializable");
  347. tests.addElement("sunw.util.EventListener");
  348. // fall through
  349. case 12:
  350. tests.addElement("javax.accessibility.Accessible");
  351. tests.addElement("sun.misc.BASE64Encoder");
  352. tests.addElement("com.sun.image.codec.jpeg.JPEGCodec");
  353. // fall through
  354. case 11:
  355. default:
  356. //things like sun.reflection, sun.misc, sun.net
  357. tests.addElement("sun.reflect.SerializationConstructorAccessorImpl");
  358. tests.addElement("sun.net.www.http.HttpClient");
  359. tests.addElement("sun.audio.AudioPlayer");
  360. break;
  361. }
  362. return tests;
  363. }
  364. /**
  365. * get a vector of strings of packages built into
  366. * that platforms runtime jar(s)
  367. * @return list of packages.
  368. */
  369. public static Vector getJrePackages() {
  370. if (jrePackages == null) {
  371. buildJrePackages();
  372. }
  373. return jrePackages;
  374. }
  375. /**
  376. *
  377. * Writes the command into a temporary DCL script and returns the
  378. * corresponding File object.
  379. * It is the job of the caller to delete the file on exit.
  380. * @param cmd the command.
  381. * @return the file containing the command.
  382. * @throws IOException if there is an error writing to the file.
  383. */
  384. public static File createVmsJavaOptionFile(String[] cmd)
  385. throws IOException {
  386. File script = FILE_UTILS.createTempFile("ANT", ".JAVA_OPTS", null);
  387. PrintWriter out = null;
  388. try {
  389. out = new PrintWriter(new BufferedWriter(new FileWriter(script)));
  390. for (int i = 0; i < cmd.length; i++) {
  391. out.println(cmd[i]);
  392. }
  393. } finally {
  394. FileUtils.close(out);
  395. }
  396. return script;
  397. }
  398. /**
  399. * Return the value of ${java.home}
  400. * @return the java home value.
  401. */
  402. public static String getJavaHome() {
  403. return JAVA_HOME;
  404. }
  405. }