Java JDK Home Get getJDKHomeVariable()

Here you can find the source of getJDKHomeVariable()

Description

get JDK Home Variable

License

Open Source License

Declaration

public static String getJDKHomeVariable() 

Method Source Code


//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.io.File;

public class Main {
    public static String getJDKHomeVariable() {
        String jdkHome = System.getProperty("jdk.home"); //$NON-NLS-1$

        if (jdkHome == null || "".equals(jdkHome)) { //$NON-NLS-1$
            jdkHome = getJDKHomeFromEclipseVm();
        }//  www  . jav  a2 s.c  o  m

        if (jdkHome == null || "".equals(jdkHome)) { //$NON-NLS-1$
            jdkHome = System.getenv("JDK_HOME"); //$NON-NLS-1$
        }
        return jdkHome;
    }

    private static String getJDKHomeFromEclipseVm() {
        String eclipseVm = System.getProperty("eclipse.vm"); //$NON-NLS-1$
        if (eclipseVm != null && !"".equals(eclipseVm)) {
            File javaexe = new File(eclipseVm);
            if (javaexe.exists()) {
                String jdk = getJDKPath(javaexe);
                if (jdk != null && new File(jdk, "lib/tools.jar").exists()) {//$NON-NLS-1$
                    return jdk;
                }
            }

        }
        return null;
    }

    private static String getJDKPath(File file) {
        if (file == null) {
            return null;
        }
        if ("bin".equals(file.getName())) {//$NON-NLS-1$
            return file.getParent();
        } else {
            return getJDKPath(file.getParentFile());
        }
    }
}

Related

  1. getJdkBin()
  2. getJdkHome()
  3. getJdkHome()
  4. getJDKPath(File file)