Java File Attribute findJavaExecutable(File vmInstallLocation)

Here you can find the source of findJavaExecutable(File vmInstallLocation)

Description

Starting in the specified VM install location, attempt to find the 'java' executable file.

License

Apache License

Declaration

public static File findJavaExecutable(File vmInstallLocation) 

Method Source Code


//package com.java2s;
/*-------------------------------------------------------------------------+
|                                                                          |
| Copyright 2005-2011 The ConQAT Project                                   |
|                                                                          |
| Licensed under the Apache License, Version 2.0 (the "License");          |
| you may not use this file except in compliance with the License.         |
| You may obtain a copy of the License at                                  |
|                                                                          |
|    http://www.apache.org/licenses/LICENSE-2.0                            |
|                                                                          |
| Unless required by applicable law or agreed to in writing, software      |
| distributed under the License is distributed on an "AS IS" BASIS,        |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and      |
| limitations under the License.                                           |
+-------------------------------------------------------------------------*/

import static java.io.File.separatorChar;
import java.io.File;

public class Main {
    /** List of candidate java executable names. */
    private static final String[] CANDIDATE_JAVA_EXECUTABLES = { "java", "java.exe", "javaw", "javaw.exe", "j9w",
            "j9w.exe", "j9", "j9.exe" };
    /**//from w ww. j  ava2s . c  om
     * The list of locations in which to look for the java executable in
     * candidate VM install locations, relative to the VM install location.
     */
    private static final String[] CANDIDATE_JAVA_LOCATIONS = { "bin" + separatorChar,
            "jre" + separatorChar + "bin" + separatorChar };

    /**
     * Starting in the specified VM install location, attempt to find the 'java'
     * executable file. If found, return the corresponding <code>File</code>
     * object, otherwise return <code>null</code>.
     * 
     * This is copied from
     * <code>org.eclipse.jdt.internal.launching.StandardVMType</code>.
     */
    public static File findJavaExecutable(File vmInstallLocation) {
        // Try each candidate in order. The first one found wins. Thus, the
        // order of CANDIDATE_JAVA_EXECUTABLES and CANDIDATE_JAVA_LOCATIONS is
        // significant.
        for (int i = 0; i < CANDIDATE_JAVA_EXECUTABLES.length; i++) {
            for (int j = 0; j < CANDIDATE_JAVA_LOCATIONS.length; j++) {
                File javaFile = new File(vmInstallLocation,
                        CANDIDATE_JAVA_LOCATIONS[j] + CANDIDATE_JAVA_EXECUTABLES[i]);
                if (javaFile.isFile()) {
                    return javaFile;
                }
            }
        }
        return null;
    }
}

Related

  1. ensureExecutable(IPath path)
  2. ensureFileIsExecutable(String filename)
  3. fileExecute(String path)
  4. findInPath(String executable, String path, String pathSeparator)
  5. findJavaCompilerExecutableInDir(File dir)
  6. findUnixExecutable(String unixCommandLineExecutables)
  7. javaExecutable(File jre)
  8. launchExecutable(String executable, List args, Preferences preferences)
  9. makeCurrentDirectoryThatOfExecutable(String executable)