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

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

Introduction

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

Prototype

public static File getJavaHome() 

Source Link

Document

Gets the Java home directory as a File.

Usage

From source file:org.apache.cxf.maven_plugin.AbstractCodegenMoho.java

private File getJavaExecutable() throws IOException {
    if (javaExecutable != null) {
        getLog().debug("Plugin configuration set the 'javaExecutable' parameter to " + javaExecutable);
    } else {/*from w  w  w .j a  va  2s. c o m*/
        Toolchain tc = toolchainManager.getToolchainFromBuildContext("jdk", mavenSession);
        if (tc != null) {
            getLog().info("Using toolchain " + tc + " to find the java executable");
            javaExecutable = tc.findTool("java");
        } else {
            getLog().debug("The java executable is set to default value");
            javaExecutable = SystemUtils.getJavaHome() + File.separator + "bin" + File.separator + "java";
        }
    }
    String exe = SystemUtils.IS_OS_WINDOWS && !javaExecutable.endsWith(".exe") ? ".exe" : "";
    File javaExe = new File(javaExecutable + exe);
    if (!javaExe.isFile()) {
        throw new IOException("The java executable '" + javaExe + "' doesn't exist or is not a file."
                + "Verify the <javaExecutable/> parameter or toolchain configuration.");
    }
    getLog().info("The java executable is " + javaExe.getAbsolutePath());
    return javaExe;
}

From source file:org.apache.maven.plugin.jar.JarSignMojo.java

private static File getJDKCommandExe(String command) {
    String fullCommand = command + (SystemUtils.IS_OS_WINDOWS ? ".exe" : "");

    File exe;// ww  w  . ja v a  2 s  .  co m

    // For IBM's JDK 1.2
    if (SystemUtils.IS_OS_AIX) {
        exe = new File(SystemUtils.getJavaHome() + "/../sh", fullCommand);
    } else if (SystemUtils.IS_OS_MAC_OSX) {
        exe = new File(SystemUtils.getJavaHome() + "/bin", fullCommand);
    } else {
        exe = new File(SystemUtils.getJavaHome() + "/../bin", fullCommand);
    }

    return exe;
}

From source file:org.apache.maven.plugin.javadoc.AbstractJavadocMojo.java

/**
 * Get the path of the Javadoc tool executable depending the user entry or try to find it depending the OS
 * or the <code>java.home</code> system property or the <code>JAVA_HOME</code> environment variable.
 *
 * @return the path of the Javadoc tool//from  ww w.j av  a  2 s. c  o m
 * @throws IOException if not found
 */
private String getJavadocExecutable() throws IOException {
    Toolchain tc = getToolchain();

    if (tc != null) {
        getLog().info("Toolchain in javadoc-plugin: " + tc);
        if (javadocExecutable != null) {
            getLog().warn(
                    "Toolchains are ignored, 'javadocExecutable' parameter is set to " + javadocExecutable);
        } else {
            javadocExecutable = tc.findTool("javadoc");
        }
    }

    String javadocCommand = "javadoc" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : "");

    File javadocExe;

    // ----------------------------------------------------------------------
    // The javadoc executable is defined by the user
    // ----------------------------------------------------------------------
    if (StringUtils.isNotEmpty(javadocExecutable)) {
        javadocExe = new File(javadocExecutable);

        if (javadocExe.isDirectory()) {
            javadocExe = new File(javadocExe, javadocCommand);
        }

        if (SystemUtils.IS_OS_WINDOWS && javadocExe.getName().indexOf('.') < 0) {
            javadocExe = new File(javadocExe.getPath() + ".exe");
        }

        if (!javadocExe.isFile()) {
            throw new IOException("The javadoc executable '" + javadocExe
                    + "' doesn't exist or is not a file. Verify the <javadocExecutable/> parameter.");
        }

        return javadocExe.getAbsolutePath();
    }

    // ----------------------------------------------------------------------
    // Try to find javadocExe from System.getProperty( "java.home" )
    // By default, System.getProperty( "java.home" ) = JRE_HOME and JRE_HOME
    // should be in the JDK_HOME
    // ----------------------------------------------------------------------
    // For IBM's JDK 1.2
    if (SystemUtils.IS_OS_AIX) {
        javadocExe = new File(SystemUtils.getJavaHome() + File.separator + ".." + File.separator + "sh",
                javadocCommand);
    } else if (SystemUtils.IS_OS_MAC_OSX) {
        javadocExe = new File(SystemUtils.getJavaHome() + File.separator + "bin", javadocCommand);
    } else {
        javadocExe = new File(SystemUtils.getJavaHome() + File.separator + ".." + File.separator + "bin",
                javadocCommand);
    }

    // ----------------------------------------------------------------------
    // Try to find javadocExe from JAVA_HOME environment variable
    // ----------------------------------------------------------------------
    if (!javadocExe.exists() || !javadocExe.isFile()) {
        Properties env = CommandLineUtils.getSystemEnvVars();
        String javaHome = env.getProperty("JAVA_HOME");
        if (StringUtils.isEmpty(javaHome)) {
            throw new IOException("The environment variable JAVA_HOME is not correctly set.");
        }
        if ((!new File(javaHome).getCanonicalFile().exists())
                || (new File(javaHome).getCanonicalFile().isFile())) {
            throw new IOException("The environment variable JAVA_HOME=" + javaHome
                    + " doesn't exist or is not a valid directory.");
        }

        javadocExe = new File(javaHome + File.separator + "bin", javadocCommand);
    }

    if (!javadocExe.getCanonicalFile().exists() || !javadocExe.getCanonicalFile().isFile()) {
        throw new IOException("The javadoc executable '" + javadocExe
                + "' doesn't exist or is not a file. Verify the JAVA_HOME environment variable.");
    }

    return javadocExe.getAbsolutePath();
}

From source file:org.apache.maven.plugin.javadoc.JavadocUtil.java

/**
 * @param log a logger could be null//from w  w w .  j  av  a2 s .  c  o  m
 * @return the <code>JAVA_HOME</code> from System.getProperty( "java.home" )
 * By default, <code>System.getProperty( "java.home" ) = JRE_HOME</code> and <code>JRE_HOME</code>
 * should be in the <code>JDK_HOME</code>
 * @since 2.6
 */
private static File getJavaHome(Log log) {
    File javaHome;
    if (SystemUtils.IS_OS_MAC_OSX) {
        javaHome = SystemUtils.getJavaHome();
    } else {
        javaHome = new File(SystemUtils.getJavaHome(), "..");
    }

    if (javaHome == null || !javaHome.exists()) {
        try {
            javaHome = new File(CommandLineUtils.getSystemEnvVars().getProperty("JAVA_HOME"));
        } catch (IOException e) {
            if (log != null && log.isDebugEnabled()) {
                log.debug("IOException: " + e.getMessage());
            }
        }
    }

    if (javaHome == null || !javaHome.exists()) {
        if (log != null && log.isErrorEnabled()) {
            log.error(
                    "Cannot find Java application directory. Either specify \'java.home\' system property, or "
                            + "JAVA_HOME environment variable.");
        }
    }

    return javaHome;
}

From source file:org.apache.maven.plugin.jdeps.AbstractJDepsMojo.java

private String getJDepsExecutable() throws IOException {
    Toolchain tc = getToolchain();//from   w  ww. j a  va 2 s.  co m

    String jdepsExecutable = null;
    if (tc != null) {
        jdepsExecutable = tc.findTool("jdeps");
    }

    String jdepsCommand = "jdeps" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : "");

    File jdepsExe;

    if (StringUtils.isNotEmpty(jdepsExecutable)) {
        jdepsExe = new File(jdepsExecutable);

        if (jdepsExe.isDirectory()) {
            jdepsExe = new File(jdepsExe, jdepsCommand);
        }

        if (SystemUtils.IS_OS_WINDOWS && jdepsExe.getName().indexOf('.') < 0) {
            jdepsExe = new File(jdepsExe.getPath() + ".exe");
        }

        if (!jdepsExe.isFile()) {
            throw new IOException("The jdeps executable '" + jdepsExe + "' doesn't exist or is not a file.");
        }
        return jdepsExe.getAbsolutePath();
    }

    // ----------------------------------------------------------------------
    // Try to find jdepsExe from System.getProperty( "java.home" )
    // By default, System.getProperty( "java.home" ) = JRE_HOME and JRE_HOME
    // should be in the JDK_HOME
    // ----------------------------------------------------------------------
    // For IBM's JDK 1.2
    if (SystemUtils.IS_OS_AIX) {
        jdepsExe = new File(SystemUtils.getJavaHome() + File.separator + ".." + File.separator + "sh",
                jdepsCommand);
    }
    // For Apple's JDK 1.6.x (and older?) on Mac OSX
    // CHECKSTYLE_OFF: MagicNumber
    else if (SystemUtils.IS_OS_MAC_OSX && SystemUtils.JAVA_VERSION_FLOAT < 1.7f)
    // CHECKSTYLE_ON: MagicNumber
    {
        jdepsExe = new File(SystemUtils.getJavaHome() + File.separator + "bin", jdepsCommand);
    } else {
        jdepsExe = new File(SystemUtils.getJavaHome() + File.separator + ".." + File.separator + "bin",
                jdepsCommand);
    }

    // ----------------------------------------------------------------------
    // Try to find jdepsExe from JAVA_HOME environment variable
    // ----------------------------------------------------------------------
    if (!jdepsExe.exists() || !jdepsExe.isFile()) {
        Properties env = CommandLineUtils.getSystemEnvVars();
        String javaHome = env.getProperty("JAVA_HOME");
        if (StringUtils.isEmpty(javaHome)) {
            throw new IOException("The environment variable JAVA_HOME is not correctly set.");
        }
        if ((!new File(javaHome).getCanonicalFile().exists())
                || (new File(javaHome).getCanonicalFile().isFile())) {
            throw new IOException("The environment variable JAVA_HOME=" + javaHome
                    + " doesn't exist or is not a valid directory.");
        }

        jdepsExe = new File(javaHome + File.separator + "bin", jdepsCommand);
    }

    if (!jdepsExe.getCanonicalFile().exists() || !jdepsExe.getCanonicalFile().isFile()) {
        throw new IOException("The jdeps executable '" + jdepsExe
                + "' doesn't exist or is not a file. Verify the JAVA_HOME environment variable.");
    }

    return jdepsExe.getAbsolutePath();
}

From source file:org.apache.maven.plugin.jdiff.JavadocBean.java

private String getJavadocPath() {
    final String javadocCommand = "javadoc" + (SystemUtils.IS_OS_WINDOWS ? ".exe" : "");
    // For IBM's JDK 1.2
    final File javadocExe = (SystemUtils.IS_OS_AIX
            ? new File(SystemUtils.getJavaHome() + "/../sh", javadocCommand)
            : new File(SystemUtils.getJavaHome() + "/../bin", javadocCommand));

    //return javadocExe.getAbsolutePath();

    return "javadoc";
}

From source file:org.apache.maven.plugins.linkcheck.SiteInvoker.java

/**
 * @return the <code>JAVA_HOME</code> from System.getProperty( "java.home" )
 * By default, <code>System.getProperty( "java.home" ) = JRE_HOME</code> and <code>JRE_HOME</code>
 * should be in the <code>JDK_HOME</code> or null if not setted.
 * @see #invoke(Invoker, InvocationRequest, File, List, Properties, String)
 *//* w w w  . j av  a  2  s .  c  om*/
private File getJavaHome() {
    File javaHome;
    if (SystemUtils.IS_OS_MAC_OSX) {
        javaHome = SystemUtils.getJavaHome();
    } else {
        javaHome = new File(SystemUtils.getJavaHome(), "..");
    }

    if (javaHome == null || !javaHome.exists()) {
        try {
            javaHome = new File(CommandLineUtils.getSystemEnvVars().getProperty("JAVA_HOME"));
        } catch (IOException e) {
            getLog().error("IOException: " + e.getMessage());
            getLog().debug(e);
        }
    }

    if (javaHome == null || !javaHome.exists()) {
        getLog().error("Cannot find Java application directory. Either specify \'java.home\' "
                + "system property, or JAVA_HOME environment variable.");
    }

    return javaHome;
}

From source file:org.codehaus.mojo.cobertura.integration.shell.PlainJvmCommandLine.java

/**
 * Default implementation of the CommandLine assigns the classpath as an
 * environment variable (i.e. {@code CLASSPATH}) to reduce the CommandLine
 * length, thereby catering for Windows shells and their problems with
 * long command lines .../* w ww.j  a v  a2 s  .  c om*/
 * <p/>
 * {@inheritDoc}
 */
public Commandline validateStateAndRetrieveCommandLine() throws IllegalStateException {

    // Check sanity
    if (mainClass == null || mainClass.trim().equals("")) {
        throw new IllegalStateException(
                "Invalid null or empty MainClass. " + "(Must be set before retrieving a Commandline).");
    }

    // Use Plexus Utils to generate a CommandLine which can be executed as a separate Java process.
    final Commandline cl = new Commandline();
    final File java = new File(SystemUtils.getJavaHome(), "bin/java");
    cl.setExecutable(java.getAbsolutePath());
    cl.addEnvironment("CLASSPATH", synthesizeClasspath());

    // Add all options to the CommandLine
    for (String current : options) {
        createArgWithValue(cl, current);
    }

    createArgWithValue(cl, mainClass);

    // Add all other arguments
    if (useCommandFile) {
        try {

            final File commandsFile = createCommandsFile();

            // Add the commandsfile argument, which is on the form
            //
            //     --commandsfile [path to file]
            //
            createArgWithValue(cl, "--commandsfile");
            createArgWithValue(cl, commandsFile.getAbsolutePath());

        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {

        // Simply add the arguments in order
        for (String arg : arguments) {
            createArgWithValue(cl, arg);
        }
    }

    // All done.
    return cl;
}

From source file:org.codehaus.mojo.cobertura.tasks.AbstractTask.java

protected int executeJava() throws MojoExecutionException {
    Commandline cl = new Commandline();
    File java = new File(SystemUtils.getJavaHome(), "bin/java");
    cl.setExecutable(java.getAbsolutePath());
    cl.createArg().setValue("-cp");
    cl.createArg().setValue(createClasspath());

    String log4jConfig = getLog4jConfigFile();
    if (log4jConfig != null) {
        cl.createArg().setValue("-Dlog4j.configuration=" + log4jConfig);
    }/*from   w w w. ja va  2  s .  c o  m*/

    cl.createArg().setValue("-Xmx" + maxmem);

    cl.createArg().setValue(taskClass);

    if (cmdLineArgs.useCommandsFile()) {
        cl.createArg().setValue("--commandsfile");
        try {
            String commandsFile = cmdLineArgs.getCommandsFile();
            cl.createArg().setValue(commandsFile);
            FileUtils.copyFile(new File(commandsFile), new File(commandsFile + ".bak"));
        } catch (IOException e) {
            throw new MojoExecutionException("Unable to obtain CommandsFile location.", e);
        }
    } else {
        Iterator it = cmdLineArgs.iterator();
        while (it.hasNext()) {
            cl.createArg().setValue(it.next().toString());
        }
    }

    CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();

    CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();

    if (quiet) {
        CommandLineUtils.StringStreamConsumer nullConsumer = new CommandLineUtils.StringStreamConsumer() {
            public void consumeLine(String line) {
                // swallow
            }
        };
        stdout = nullConsumer;
        stderr = nullConsumer;
    }

    getLog().debug("Working Directory: " + cl.getWorkingDirectory());
    getLog().debug("Executing command line:");
    getLog().debug(cl.toString());

    int exitCode;
    try {
        exitCode = CommandLineUtils.executeCommandLine(cl, stdout, stderr);
    } catch (CommandLineException e) {
        throw new MojoExecutionException("Unable to execute Cobertura.", e);
    }

    getLog().debug("exit code: " + exitCode);

    String output = stdout.getOutput();

    if (output.trim().length() > 0) {
        getLog().debug("--------------------");
        getLog().debug(" Standard output from the Cobertura task:");
        getLog().debug("--------------------");
        getLog().info(output);
        getLog().debug("--------------------");
    }

    String stream = stderr.getOutput();

    if (stream.trim().length() > 0) {
        getLog().debug("--------------------");
        getLog().debug(" Standard error from the Cobertura task:");
        getLog().debug("--------------------");
        getLog().error(stderr.getOutput());
        getLog().debug("--------------------");
    }

    return exitCode;
}

From source file:org.codehaus.mojo.keytool.GenkeyMojo.java

private static File getJDKCommandExe(String command) {
    String fullCommand = command + (SystemUtils.IS_OS_WINDOWS ? ".exe" : "");

    File exe;//ww w .j  a  v  a  2 s . c om

    // For IBM's JDK 1.2
    if (SystemUtils.IS_OS_AIX) {
        exe = new File(SystemUtils.getJavaHome() + "/../sh", fullCommand);
    } else if (SystemUtils.IS_OS_MAC_OSX) // what about IS_OS_MAC_OS ??
    {
        exe = new File(SystemUtils.getJavaHome() + "/bin", fullCommand);
    } else {
        exe = new File(SystemUtils.getJavaHome() + "/../bin", fullCommand);
    }

    return exe;
}