Bug 407402 - [MAC OS] Checked JRE is system JRE (should be JRE defined
in eclipse.ini)
diff --git a/org.eclipse.jdt.launching.macosx/macosx/org/eclipse/jdt/internal/launching/macosx/MacOSXVMInstallType.java b/org.eclipse.jdt.launching.macosx/macosx/org/eclipse/jdt/internal/launching/macosx/MacOSXVMInstallType.java
index 751ca13..153e2e2 100644
--- a/org.eclipse.jdt.launching.macosx/macosx/org/eclipse/jdt/internal/launching/macosx/MacOSXVMInstallType.java
+++ b/org.eclipse.jdt.launching.macosx/macosx/org/eclipse/jdt/internal/launching/macosx/MacOSXVMInstallType.java
@@ -135,23 +135,51 @@
 	@Override
 	public File detectInstallLocation() {
 		try {
+			// try to find the VM used to launch Eclipse
+			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=407402
+			File defaultLocation = getJavaHomeLocation();
+			
 			// find all installed VMs
-			File defaultLocation = null;
 			VMStandin[] vms = MacInstalledJREs.getInstalledJREs(null);
-			File location = null;
+			File firstLocation = null;
+			IVMInstall firstInstall = null;
+			IVMInstall defaultInstall = null;
 			for (int i= 0; i < vms.length; i++) {
-				location = vms[i].getInstallLocation();
+				File location = vms[i].getInstallLocation();
 				IVMInstall install = findVMInstall(vms[i].getId());
 				if (install == null) {
 					install= vms[i].convertToRealVM();
 				}
 				if (i == 0) {
-					defaultLocation = location;
-					try {
-						JavaRuntime.setDefaultVMInstall(install, null);
-					} catch (CoreException e) {
-						LaunchingPlugin.log(e);
-					}
+					firstLocation = location;
+					firstInstall = install;
+				}
+				if (defaultInstall == null && defaultLocation != null && defaultLocation.equals(location)) {
+					defaultInstall = install;
+				}
+			}
+			
+			// determine the default VM
+			if (defaultInstall == null) {
+				if (defaultLocation != null) {
+					// prefer the VM used to launch Eclipse
+					String version = System.getProperty("java.version"); //$NON-NLS-1$
+					VMStandin standin = new MacInstalledJREs.MacVMStandin(this,
+							defaultLocation,
+							version == null ? Messages.MacOSXVMInstallType_jre : NLS.bind(Messages.MacOSXVMInstallType_jre_version, version),
+							(version == null ? "???" : version),  //$NON-NLS-1$
+							String.valueOf(System.currentTimeMillis()));
+					defaultInstall = standin.convertToRealVM();
+				} else {
+					defaultInstall = firstInstall;
+					defaultLocation = firstLocation;
+				}
+			}
+			if (defaultInstall != null) {
+				try {
+					JavaRuntime.setDefaultVMInstall(defaultInstall, null);
+				} catch (CoreException e) {
+					LaunchingPlugin.log(e);
 				}
 			}
 			return defaultLocation;
diff --git a/org.eclipse.jdt.launching.macosx/macosx/org/eclipse/jdt/internal/launching/macosx/Messages.java b/org.eclipse.jdt.launching.macosx/macosx/org/eclipse/jdt/internal/launching/macosx/Messages.java
index f47a59a..ad207c5 100644
--- a/org.eclipse.jdt.launching.macosx/macosx/org/eclipse/jdt/internal/launching/macosx/Messages.java
+++ b/org.eclipse.jdt.launching.macosx/macosx/org/eclipse/jdt/internal/launching/macosx/Messages.java
@@ -17,6 +17,8 @@
 	public static String MacOSXVMInstallType_0;
 	public static String MacOSXVMInstallType_1;
 	public static String MacOSXVMInstallType_2;
+	public static String MacOSXVMInstallType_jre;
+	public static String MacOSXVMInstallType_jre_version;
 	static {
 		// initialize resource bundle
 		NLS.initializeMessages(BUNDLE_NAME, Messages.class);
diff --git a/org.eclipse.jdt.launching.macosx/macosx/org/eclipse/jdt/internal/launching/macosx/messages.properties b/org.eclipse.jdt.launching.macosx/macosx/org/eclipse/jdt/internal/launching/macosx/messages.properties
index 1e0a4d2..3c20477 100644
--- a/org.eclipse.jdt.launching.macosx/macosx/org/eclipse/jdt/internal/launching/macosx/messages.properties
+++ b/org.eclipse.jdt.launching.macosx/macosx/org/eclipse/jdt/internal/launching/macosx/messages.properties
@@ -11,3 +11,5 @@
 MacOSXVMInstallType_0=MacOS X VM
 MacOSXVMInstallType_1=Java VM default location {0} not found, JVMs were not scanned.
 MacOSXVMInstallType_2=Not a JDK Root; MacOS X executable was not found
+MacOSXVMInstallType_jre=JRE
+MacOSXVMInstallType_jre_version=JRE [{0}]
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/MacInstalledJREs.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/MacInstalledJREs.java
index 334729d..aaa7fc3 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/MacInstalledJREs.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/MacInstalledJREs.java
@@ -58,7 +58,7 @@
 	 * Custom stand-in that allows us to provide a version
 	 * @since 3.7.0
 	 */
-	static class MacVMStandin extends VMStandin {
+	public static class MacVMStandin extends VMStandin {
 		
 		String version = null;
 		
@@ -67,7 +67,6 @@
 			setInstallLocation(location);
 			setName(name);
 			this.version = version;
-			// TODO Auto-generated constructor stub
 		}
 		
 		@Override
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/StandardVMType.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/StandardVMType.java
index 967c3c0..814821c 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/StandardVMType.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/StandardVMType.java
@@ -46,7 +46,6 @@
 import org.eclipse.jdt.launching.IVMInstall;
 import org.eclipse.jdt.launching.JavaRuntime;
 import org.eclipse.jdt.launching.LibraryLocation;
-import org.eclipse.osgi.service.environment.Constants;
 import org.eclipse.osgi.util.NLS;
 
 /**
@@ -228,11 +227,23 @@
 	 * @see org.eclipse.jdt.launching.IVMInstallType#detectInstallLocation()
 	 */
 	public File detectInstallLocation() {
-		// do not detect on the Mac OS
-		if (Platform.getOS().equals(Constants.OS_MACOSX)) {
+		// We want a Mac OSX VM install so don't process the install location for this type
+		if(Platform.OS_MACOSX.equals(Platform.getOS())) {
 			return null;
-		}		
-		
+		}
+		return getJavaHomeLocation();
+	}
+
+	/**
+	 * Returns the VM install location using the <code>java.home</code> system
+	 * property or <code>null</code>.
+	 * 
+	 * @return the install location of this type of VM based off of the
+	 *  <code>java.home</code> system property, or <code>null</code> if not found
+	 * 
+	 * @since 3.7
+	 */
+	protected File getJavaHomeLocation() {
 		// Retrieve the 'java.home' system property.  If that directory doesn't exist, 
 		// return null.
 		File javaHome; 
@@ -273,7 +284,7 @@
 		
 		return javaHome;
 	}
-
+	
 	/**
 	 * Return an <code>IPath</code> corresponding to the single library file containing the
 	 * standard Java classes for most VMs version 1.2 and above.