Example usage for org.apache.commons.lang3 SystemUtils getJavaHome

List of usage examples for org.apache.commons.lang3 SystemUtils getJavaHome

Introduction

In this page you can find the example usage for org.apache.commons.lang3 SystemUtils getJavaHome.

Prototype

public static File getJavaHome() 

Source Link

Document

Gets the Java home directory as a File .

Usage

From source file:com.chaschev.install.InstallMojo.java

private static File javaExePath() {
    return new File(SystemUtils.getJavaHome(), "bin/" + (IS_OS_UNIX ? "java" : "java.exe"));
}

From source file:com.chaschev.install.InstallMojo.java

private String findPath() throws MojoFailureException {
    String path = Optional.fromNullable(System.getenv("path")).or(System.getenv("PATH"));

    ArrayList<String> pathEntries = newArrayList(path == null ? new String[0] : path.split(File.pathSeparator));

    String javaHomeAbsPath = SystemUtils.getJavaHome().getParentFile().getAbsolutePath();

    String mavenHomeAbsPath = getMavenHomeByClass(DefaultMaven.class).getAbsolutePath();

    List<MatchingPath> matchingPaths = new ArrayList<MatchingPath>();

    final LinkedHashSet<File> knownBinFolders = Sets.newLinkedHashSet(
            Lists.transform(Arrays.asList("/usr/local/bin", "/usr/local/sbin"), PATH_TO_FILE));

    for (String pathEntry : pathEntries) {
        File entryFile = new File(pathEntry);
        String absPath = entryFile.getAbsolutePath();

        boolean writable = isWritable(entryFile);

        getLog().debug(/*w w  w.j ava  2  s . co  m*/
                "testing " + entryFile.getAbsolutePath() + ": " + (writable ? "writable" : "not writable"));

        if (absPath.startsWith(javaHomeAbsPath)) {
            addMatching(matchingPaths, absPath, writable, 1);
        } else if (absPath.startsWith(mavenHomeAbsPath)) {
            addMatching(matchingPaths, absPath, writable, 2);
        }
    }

    if (IS_OS_UNIX && matchingPaths.isEmpty()) {
        getLog().warn("didn't find maven/jdk writable roots available on path, trying common unix paths: "
                + knownBinFolders);

        final LinkedHashSet<File> pathEntriesSet = Sets
                .newLinkedHashSet(Lists.transform(pathEntries, PATH_TO_FILE));

        for (File knownBinFolder : knownBinFolders) {
            if (pathEntriesSet.contains(knownBinFolder)) {
                addMatching(matchingPaths, knownBinFolder.getAbsolutePath(), isWritable(knownBinFolder), 3);
            }
        }
    }

    Collections.sort(matchingPaths);

    if (matchingPaths.isEmpty()) {
        throw new MojoFailureException("Could not find a bin folder to write to. Tried: \n"
                + Joiner.on("\n").join(mavenHomeAbsPath, javaHomeAbsPath) + "\n"
                + (IS_OS_UNIX ? knownBinFolders + "\n" : "")
                + " but they don't appear on the path or are not writable. You may try running as administrator or specifying -DinstallTo=your-bin-dir-path parameter");
    }

    return matchingPaths.get(0).path;
}

From source file:org.exist.launcher.LauncherWrapper.java

protected String getJavaCmd() {
    final File javaHome = SystemUtils.getJavaHome();
    if (SystemUtils.IS_OS_WINDOWS) {
        Path javaBin = Paths.get(javaHome.getAbsolutePath(), "bin", "javaw.exe");
        if (Files.isExecutable(javaBin)) {
            return '"' + javaBin.toString() + '"';
        }//from  www  . ja v a 2s .c  o m
        javaBin = Paths.get(javaHome.getAbsolutePath(), "bin", "java.exe");
        if (Files.isExecutable(javaBin)) {
            return '"' + javaBin.toString() + '"';
        }
    } else {
        Path javaBin = Paths.get(javaHome.getAbsolutePath(), "bin", "java");
        if (Files.isExecutable(javaBin)) {
            return javaBin.toString();
        }
    }
    return "java";
}