001 /*--------------------------------------------------------------------------+
002 $Id: JavaUtils.java 26268 2010-02-18 10:44:30Z juergens $
003 | |
004 | Copyright 2005-2010 Technische Universitaet Muenchen |
005 | |
006 | Licensed under the Apache License, Version 2.0 (the "License"); |
007 | you may not use this file except in compliance with the License. |
008 | You may obtain a copy of the License at |
009 | |
010 | http://www.apache.org/licenses/LICENSE-2.0 |
011 | |
012 | Unless required by applicable law or agreed to in writing, software |
013 | distributed under the License is distributed on an "AS IS" BASIS, |
014 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015 | See the License for the specific language governing permissions and |
016 | limitations under the License. |
017 +--------------------------------------------------------------------------*/
018 package edu.tum.cs.commons.io;
019
020 import static java.io.File.separatorChar;
021
022 import java.io.File;
023
024 /**
025 * This class provides utilities to access a Java runtime execution environment.
026 *
027 * @author juergens
028 * @author $Author: juergens $
029 * @version $Rev: 26268 $
030 * @levd.rating GREEN Hash: 27257F331AC5DF44F35CBC78C15A1E96
031 */
032 public class JavaUtils {
033
034 /** JAVA_HOME environment variable. */
035 private static final String JAVA_HOME = System.getProperty("java.home");
036
037 /** List of candidate java executable names. */
038 private static final String[] CANDIDATE_JAVA_EXECUTABLES = { "java",
039 "java.exe", "javaw", "javaw.exe", "j9w", "j9w.exe", "j9", "j9.exe" };
040
041 /**
042 * The list of locations in which to look for the java executable in
043 * candidate VM install locations, relative to the VM install location.
044 */
045 private static final String[] CANDIDATE_JAVA_LOCATIONS = {
046 "bin" + separatorChar,
047 "jre" + separatorChar + "bin" + separatorChar };
048
049 /**
050 * Starting in the specified VM install location, attempt to find the 'java'
051 * executable file. If found, return the corresponding <code>File</code>
052 * object, otherwise return <code>null</code>.
053 *
054 * This is copied from
055 * <code>org.eclipse.jdt.internal.launching.StandardVMType</code>.
056 */
057 public static File findJavaExecutable(File vmInstallLocation) {
058 // Try each candidate in order. The first one found wins. Thus, the
059 // order of CANDIDATE_JAVA_EXECUTABLES and CANDIDATE_JAVA_LOCATIONS is
060 // significant.
061 for (int i = 0; i < CANDIDATE_JAVA_EXECUTABLES.length; i++) {
062 for (int j = 0; j < CANDIDATE_JAVA_LOCATIONS.length; j++) {
063 File javaFile = new File(vmInstallLocation,
064 CANDIDATE_JAVA_LOCATIONS[j]
065 + CANDIDATE_JAVA_EXECUTABLES[i]);
066 if (javaFile.isFile()) {
067 return javaFile;
068 }
069 }
070 }
071 return null;
072 }
073
074 /**
075 * Use {@link #findJavaExecutable(File)} to search in the directory
076 * specified by environment variable <code>JAVA_HOME</code> for the Java
077 * executable.
078 */
079 public static File obtainJavaExecutable() {
080 return findJavaExecutable(new File(JAVA_HOME));
081 }
082
083 /**
084 * Use {@link #obtainJavaExecutable()} to determine the Java executable via
085 * environment variable <code>JAVA_HOME</code>. If this fails, a command
086 * that expects the Java executable to be on the path is returned.
087 */
088 public static String obtainJavaExecutionCommand() {
089 File executable = obtainJavaExecutable();
090 if (executable != null) {
091 return executable.getAbsolutePath();
092 }
093 return CANDIDATE_JAVA_EXECUTABLES[0];
094 }
095
096 }