001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| 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+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.io; 018 019import static java.io.File.separatorChar; 020 021import java.io.File; 022 023/** 024 * This class provides utilities to access a Java runtime execution environment. 025 * 026 * @author juergens 027 */ 028public class JavaUtils { 029 030 /** JAVA_HOME environment variable. */ 031 private static final String JAVA_HOME = System.getProperty("java.home"); 032 033 /** List of candidate java executable names. */ 034 private static final String[] CANDIDATE_JAVA_EXECUTABLES = { "java", "java.exe", "javaw", "javaw.exe", "j9w", 035 "j9w.exe", "j9", "j9.exe" }; 036 037 /** 038 * The list of locations in which to look for the java executable in 039 * candidate VM install locations, relative to the VM install location. 040 */ 041 private static final String[] CANDIDATE_JAVA_LOCATIONS = { "bin" + separatorChar, 042 "jre" + separatorChar + "bin" + separatorChar }; 043 044 /** 045 * Starting in the specified VM install location, attempt to find the 'java' 046 * executable file. If found, return the corresponding <code>File</code> 047 * object, otherwise return <code>null</code>. 048 * 049 * This is copied from 050 * <code>org.eclipse.jdt.internal.launching.StandardVMType</code>. 051 */ 052 public static File findJavaExecutable(File vmInstallLocation) { 053 // Try each candidate in order. The first one found wins. Thus, the 054 // order of CANDIDATE_JAVA_EXECUTABLES and CANDIDATE_JAVA_LOCATIONS is 055 // significant. 056 for (int i = 0; i < CANDIDATE_JAVA_EXECUTABLES.length; i++) { 057 for (int j = 0; j < CANDIDATE_JAVA_LOCATIONS.length; j++) { 058 File javaFile = new File(vmInstallLocation, 059 CANDIDATE_JAVA_LOCATIONS[j] + CANDIDATE_JAVA_EXECUTABLES[i]); 060 if (javaFile.isFile()) { 061 return javaFile; 062 } 063 } 064 } 065 return null; 066 } 067 068 /** 069 * Use {@link #findJavaExecutable(File)} to search in the directory 070 * specified by environment variable <code>JAVA_HOME</code> for the Java 071 * executable. 072 */ 073 public static File obtainJavaExecutable() { 074 return findJavaExecutable(new File(JAVA_HOME)); 075 } 076 077 /** 078 * Use {@link #obtainJavaExecutable()} to determine the Java executable via 079 * environment variable <code>JAVA_HOME</code>. If this fails, a command 080 * that expects the Java executable to be on the path is returned. 081 */ 082 public static String obtainJavaExecutionCommand() { 083 File executable = obtainJavaExecutable(); 084 if (executable != null) { 085 return executable.getAbsolutePath(); 086 } 087 return CANDIDATE_JAVA_EXECUTABLES[0]; 088 } 089 090}