Bug 490103 [1.9] JREContainer should work without any physical libraries

Change-Id: I7f07bc0c76338830a337a900cd82986541a67a4f
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainer.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainer.java
index 3c672af..14b0b07 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainer.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainer.java
@@ -22,6 +22,7 @@
 import java.util.Map;
 
 import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
 import org.eclipse.jdt.core.IAccessRule;
 import org.eclipse.jdt.core.IClasspathAttribute;
 import org.eclipse.jdt.core.IClasspathContainer;
@@ -29,6 +30,7 @@
 import org.eclipse.jdt.core.IJavaProject;
 import org.eclipse.jdt.core.JavaCore;
 import org.eclipse.jdt.launching.IVMInstall;
+import org.eclipse.jdt.launching.IVMInstall2;
 import org.eclipse.jdt.launching.IVMInstallChangedListener;
 import org.eclipse.jdt.launching.JavaRuntime;
 import org.eclipse.jdt.launching.LibraryLocation;
@@ -305,6 +307,36 @@
 				rules = environment.getAccessRules(vm, libs, project);
 			}
 		}
+		if (vm instanceof IVMInstall2) {
+			// vm.getInstallLocation()
+			String version = ((IVMInstall2) vm).getJavaVersion();
+			if (JavaCore.compareJavaVersions(version, JavaCore.VERSION_1_8) > 0) {
+				IPath sourcePath = null;
+				IPath rootPath = null;
+				if (libs != null && libs.length > 0) {
+					sourcePath = libs[0].getSystemLibrarySourcePath();
+					rootPath = libs[0].getPackageRootPath();
+				}
+				IPath jdkPath = new Path(vm.getInstallLocation().getAbsolutePath());
+				if (sourcePath.isEmpty()) {
+					sourcePath = null;
+				}
+
+				if (rootPath.isEmpty()) {
+					rootPath = null;
+				}
+				// construct the classpath attributes for this library location
+				IClasspathAttribute[] attributes = JREContainer.buildClasspathAttributes(vm, libs[0], overrideJavaDoc);
+				IAccessRule[] libRules = null;
+				if (rules != null) {
+					libRules = rules[0];
+				} else {
+					libRules = EMPTY_RULES;
+				}
+				IClasspathEntry jrtEntry = JavaCore.newJrtEntry(jdkPath, sourcePath, rootPath, libRules, attributes, false);
+				return new IClasspathEntry[] { jrtEntry };
+			}
+		}
 		RuleKey key = null;
 		if (vm != null && rules != null && environmentId != null) {
 			key = new RuleKey(vm, environmentId);
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainerInitializer.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainerInitializer.java
index e8bb817..13d8837 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainerInitializer.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JREContainerInitializer.java
@@ -341,10 +341,10 @@
 		LibraryLocation[] libs = new LibraryLocation[entries.length];
 		for (int i = 0; i < entries.length; i++) {
 			IClasspathEntry entry = entries[i];
-			if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
+			if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY || entry.getEntryKind() == IClasspathEntry.CPE_JRT_SYSTEM) {
 				IPath path = entry.getPath();
 				File lib = path.toFile();
-				if (lib.exists() && lib.isFile()) {
+				if (lib.exists()) {
 					IPath srcPath = entry.getSourceAttachmentPath();
 					if (srcPath == null) {
 						srcPath = Path.EMPTY;
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaClasspathVariablesInitializer.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaClasspathVariablesInitializer.java
index f40a8db..d9351cb 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaClasspathVariablesInitializer.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaClasspathVariablesInitializer.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2013 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -44,9 +44,9 @@
 			for (int i = 0; i < locations.length; i++) {
 				LibraryLocation location = locations[i];
 				String name = location.getSystemLibraryPath().lastSegment();
-				if (name.equalsIgnoreCase("rt.jar")) { //$NON-NLS-1$
+				if ("rt.jar".equalsIgnoreCase(name)) { //$NON-NLS-1$
 					rtjar = location;
-				} else if (name.equalsIgnoreCase("classes.zip")) { //$NON-NLS-1$
+				} else if ("classes.zip".equalsIgnoreCase(name)) { //$NON-NLS-1$
 					classeszip = location;
 				}
 			}
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 a3b7eef..bdc5097 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
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2017 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -74,7 +74,6 @@
 	private static final String BAR = "|"; //$NON-NLS-1$
 	private static final String RELEASE_FILE = "release"; //$NON-NLS-1$
 	private static final String JAVA_VERSION = "JAVA_VERSION"; //$NON-NLS-1$
-	private static final String JRT_FS_JAR = "jrt-fs.jar"; //$NON-NLS-1$
 
 	public static final String ID_STANDARD_VM_TYPE = "org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType"; //$NON-NLS-1$
 
@@ -453,11 +452,10 @@
 						sourceRootPath = getDefaultSystemLibrarySource(lib); // To attach source if available
 					}
 				}
-				IPath pathName = new Path(installLocation.getAbsolutePath()).append(LIB).append(JRT_FS_JAR);
-				// From Java 9 149 version, we see that jrt-fs.jar is moved to lib directory so we need to look at both places
+				IPath pathName = new Path(installLocation.getAbsolutePath());
 				File jrtfsJar = pathName.toFile();
 				if (!jrtfsJar.exists()) {
-					pathName = new Path(installLocation.getAbsolutePath()).append(JRT_FS_JAR);
+					pathName = new Path(installLocation.getAbsolutePath());
 				}
 
 				LibraryLocation libraryLocation = new LibraryLocation(pathName,
diff --git a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/JavaRuntime.java b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/JavaRuntime.java
index d9ca0f7..ecb1a8e 100644
--- a/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/JavaRuntime.java
+++ b/org.eclipse.jdt.launching/launching/org/eclipse/jdt/launching/JavaRuntime.java
@@ -2139,8 +2139,10 @@
                     javadocLocations[i]= defJavaDocLocation;
                 }
                 indexes[i] = dflts[i].getIndexLocation();
-				if (!libraryPaths[i].toFile().isFile()) {
-					libraryPaths[i]= Path.EMPTY;
+				if (compareJavaVersions(vm, JavaCore.VERSION_9) < 0) {
+					if (!libraryPaths[i].toFile().isFile()) {
+						libraryPaths[i] = Path.EMPTY;
+					}
 				}
 
 				annotationPaths[i] = Path.EMPTY;