Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve how the "java" executable is found #241

Closed
cescoffier opened this issue Dec 10, 2018 · 10 comments
Closed

Improve how the "java" executable is found #241

cescoffier opened this issue Dec 10, 2018 · 10 comments
Assignees
Labels
kind/enhancement New feature or request
Milestone

Comments

@cescoffier
Copy link
Member

Right now it expects java to be in the system path.
there are two issues:

  1. windows users (java.exe)
  2. it may not be the case on all systems

Something like:

private File findJava() {
        String javaHome = System.getProperty("java.home");
        File found;
        if (javaHome == null) {
            found = findExecutableInSystemPath("java");
        } else {
            File bin = new File(javaHome, "bin");
            found = find("java", bin);
        }

        if (found == null || ! found.isFile()) {
            throw new IllegalStateException("Unable to find the java executable in JAVA_HOME or in the system path");
        }
        return found;
    }

public static File findExecutableInSystemPath(String executable) {
        String systemPath = System.getenv("PATH");

        // Fast failure if we don't have the PATH defined.
        if (systemPath == null) {
            return null;
        }

        String[] pathDirs = systemPath.split(File.pathSeparator);

        for (String pathDir : pathDirs) {
            File dir = new File(pathDir);
            if (dir.isDirectory()) {
                File file = findExecutableInDirectory(executable, dir);
                if (file != null) {
                    return file;
                }
            }
        }
        // :-(
        return null;
    }

public static File find(String executable, File... dirs) {
        // First try using the hints.
        if (dirs != null) {
            for (File hint : dirs) {
                File file = findExecutableInDirectory(executable, hint);
                if (file != null) {
                    return file;
                }
            }
        }

        // Not found, try to use the system path.
        return findExecutableInSystemPath(executable);
    }

private static File findExecutableInDirectory(String executable, File directory) {
        if (directory == null || !directory.isDirectory()) {
            // if the directory is null or not a directory => returning null.
            return null;
        }
        for (String extension : EXECUTABLE_EXTENSIONS) {
            File file = new File(directory, executable + extension);
            if (file.isFile() && file.canExecute()) {
                return file;
            }
        }
        // Not found.
        return null;
    }

would be more robust. I will work on a PR.

@cescoffier cescoffier self-assigned this Dec 10, 2018
@cescoffier cescoffier added the kind/enhancement New feature or request label Dec 10, 2018
@dmlloyd
Copy link
Member

dmlloyd commented Dec 11, 2018

On Linux we can be very accurate using Files.readSymbolicLink(Paths.get("/proc/self/exe")) (but only if the sun.java.launcher property is equal to SUN_STANDARD, otherwise we might have been launched via JNI).

But don't do stuff like looking for java.exe on Linux or java on Windows; just get the exact expected executable.

@cescoffier
Copy link
Member Author

@dmlloyd the idea is to traverse the system path to find the right executable (and before that check $JAVA_HOME/bin/ if exist).

@lburgazzoli
Copy link
Contributor

maybe you can use maven toolchains too ?

@cescoffier
Copy link
Member Author

you mean the ability to extract java home from maven?

@lburgazzoli
Copy link
Contributor

lburgazzoli commented Dec 11, 2018

AFAIR the maven compiler plugin uses toolchains to determine the java executable but I do not know if it is the same problem here :)

@cescoffier
Copy link
Member Author

It's a good idea, I didn't look at it for years! A great opportunity to refresh my memory!

@dmlloyd
Copy link
Member

dmlloyd commented Dec 11, 2018

@dmlloyd the idea is to traverse the system path to find the right executable (and before that check $JAVA_HOME/bin/ if exist).

Well that is an idea. :) My point was just that there is only an executable extension on Windows, so we should look specifically for java.exe there and java elsewhere. And, that if you're running Java from Java, at least on Linux you can definitively determine your JVM location.

@cescoffier
Copy link
Member Author

I will first check with toolchain, it would be more reliable.

@starksm64
Copy link
Contributor

I have added toolchain support with fallback to the java.home used to launch maven and I'm testing it on windows.

starksm64 added a commit that referenced this issue Mar 13, 2019
Improve the java executable location logic, #241
@gsmet
Copy link
Member

gsmet commented Mar 20, 2019

This one has been fixed.

@gsmet gsmet closed this as completed Mar 20, 2019
@gsmet gsmet added this to the 0.12.0 milestone Mar 20, 2019
aureamunoz added a commit to aureamunoz/quarkus that referenced this issue Jan 22, 2021
maxandersen added a commit to maxandersen/quarkus that referenced this issue Nov 5, 2022
Add --jfr for easy flight recording
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Aug 30, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Aug 30, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 2, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 2, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 2, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 2, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 2, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 2, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 2, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 2, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 3, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 3, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
maxr2011-tech11 added a commit to maxr2011/quarkus that referenced this issue Sep 4, 2024
gsmet pushed a commit to gsmet/quarkus that referenced this issue Sep 6, 2024
danielsoro pushed a commit to danielsoro/quarkus that referenced this issue Sep 20, 2024
bschuhmann pushed a commit to bschuhmann/quarkus that referenced this issue Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants