src/java.base/share/classes/sun/misc/Launcher.java

Print this page

        

@@ -31,14 +31,15 @@
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.net.MalformedURLException;
 import java.net.URLStreamHandler;
 import java.net.URLStreamHandlerFactory;
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.StringTokenizer;
 import java.util.Set;
-import java.util.Vector;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.security.PrivilegedExceptionAction;
 import java.security.AccessControlContext;
 import java.security.PermissionCollection;

@@ -51,14 +52,18 @@
 
 /**
  * This class is used by the system to launch the main application.
 Launcher */
 public class Launcher {
+
+    // ensure URLClassPath for boot loader is initialized first
+    static {
+        URLClassPath ucp = BootClassPathHolder.bcp;
+    }
+
     private static URLStreamHandlerFactory factory = new Factory();
     private static Launcher launcher = new Launcher();
-    private static String bootClassPath =
-        System.getProperty("sun.boot.class.path");
 
     public static Launcher getLauncher() {
         return launcher;
     }
 

@@ -127,27 +132,28 @@
 
         /**
          * create an ExtClassLoader. The ExtClassLoader is created
          * within a context that limits which files it can read
          */
-        public static ExtClassLoader getExtClassLoader() throws IOException
-        {
-            final File[] dirs = getExtDirs();
-
+        public static ExtClassLoader getExtClassLoader() throws IOException {
             try {
                 // Prior implementations of this doPrivileged() block supplied
                 // aa synthesized ACC via a call to the private method
                 // ExtClassLoader.getContext().
 
                 return AccessController.doPrivileged(
                     new PrivilegedExceptionAction<ExtClassLoader>() {
                         public ExtClassLoader run() throws IOException {
-                            int len = dirs.length;
-                            for (int i = 0; i < len; i++) {
-                                MetaIndex.registerDirectory(dirs[i]);
-                            }
-                            return new ExtClassLoader(dirs);
+                            // ext modules linked into image
+                            String home = System.getProperty("java.home");
+                            File dir = new File(new File(home, "lib"), "modules");
+                            File jimage = new File(dir, "extmodules.jimage");
+
+                            File jfxrt = new File(new File(home, "lib"), "jfxrt.jar");
+                            File[] files = jfxrt.exists() ? new File[] {jimage, jfxrt}
+                                                          : new File[] {jimage};
+                            return new ExtClassLoader(files);
                         }
                     });
             } catch (java.security.PrivilegedActionException e) {
                 throw (IOException) e.getException();
             }

@@ -158,91 +164,20 @@
         }
 
         /*
          * Creates a new ExtClassLoader for the specified directories.
          */
-        public ExtClassLoader(File[] dirs) throws IOException {
-            super(getExtURLs(dirs), null, factory);
+        public ExtClassLoader(File[] files) throws IOException {
+            super(getExtURLs(files), null, factory);
         }
 
-        private static File[] getExtDirs() {
-            String s = System.getProperty("java.ext.dirs");
-            File[] dirs;
-            if (s != null) {
-                StringTokenizer st =
-                    new StringTokenizer(s, File.pathSeparator);
-                int count = st.countTokens();
-                dirs = new File[count];
-                for (int i = 0; i < count; i++) {
-                    dirs[i] = new File(st.nextToken());
-                }
-            } else {
-                dirs = new File[0];
-            }
-            return dirs;
-        }
-
-        private static URL[] getExtURLs(File[] dirs) throws IOException {
-            Vector<URL> urls = new Vector<URL>();
-            for (int i = 0; i < dirs.length; i++) {
-                String[] files = dirs[i].list();
-                if (files != null) {
-                    for (int j = 0; j < files.length; j++) {
-                        if (!files[j].equals("meta-index")) {
-                            File f = new File(dirs[i], files[j]);
+        private static URL[] getExtURLs(File[] files) throws IOException {
+            List<URL> urls = new ArrayList<>();
+            for (File f : files) {
                             urls.add(getFileURL(f));
                         }
-                    }
-                }
-            }
-            URL[] ua = new URL[urls.size()];
-            urls.copyInto(ua);
-            return ua;
-        }
-
-        /*
-         * Searches the installed extension directories for the specified
-         * library name. For each extension directory, we first look for
-         * the native library in the subdirectory whose name is the value
-         * of the system property <code>os.arch</code>. Failing that, we
-         * look in the extension directory itself.
-         */
-        public String findLibrary(String name) {
-            final String libname = System.mapLibraryName(name);
-            URL[] urls = super.getURLs();
-            File prevDir = null;
-            for (int i = 0; i < urls.length; i++) {
-                // Get the ext directory from the URL
-                File dir = new File(urls[i].getPath()).getParentFile();
-                if (dir != null && !dir.equals(prevDir)) {
-                    // Look in architecture-specific subdirectory first
-                    // Read from the saved system properties to avoid deadlock
-                    final String arch = VM.getSavedProperty("os.arch");
-                    String pathname = AccessController.doPrivileged(
-                        new PrivilegedAction<String>() {
-                            public String run() {
-                                if (arch != null) {
-                                    File file = new File(new File(dir, arch), libname);
-                                    if (file.exists()) {
-                                        return file.getAbsolutePath();
-                                    }
-                                }
-                                // Then check the extension directory
-                                File file = new File(dir, libname);
-                                if (file.exists()) {
-                                    return file.getAbsolutePath();
-                                }
-                                return null;
-                            }
-                        });
-                    if (pathname != null) {
-                        return pathname;
-                    }
-                }
-                prevDir = dir;
-            }
-            return null;
+            return urls.toArray(new URL[0]);
         }
 
         private static AccessControlContext getContext(File[] dirs)
             throws IOException
         {

@@ -272,12 +207,22 @@
         }
 
         public static ClassLoader getAppClassLoader(final ClassLoader extcl)
             throws IOException
         {
-            final String s = System.getProperty("java.class.path");
-            final File[] path = (s == null) ? new File[0] : getClassPath(s, true);
+            // modules linked into image are prepended to class path
+            String home = System.getProperty("java.home");
+            File dir = new File(new File(home, "lib"), "modules");
+            String jimage = new File(dir, "appmodules.jimage").getPath();
+
+            String cp = System.getProperty("java.class.path");
+            if (cp == null) {
+                cp = jimage;
+            } else {
+                cp = jimage + File.pathSeparator + cp;
+            }
+            final File[] path = getClassPath(cp, true);
 
             // Note: on bugid 4256530
             // Prior implementations of this doPrivileged() block supplied
             // a rather restrictive ACC via a call to the private method
             // AppClassLoader.getContext(). This proved overly restrictive

@@ -285,12 +230,11 @@
             // accessClassInPackage.sun.* grants from being honored.
             //
             return AccessController.doPrivileged(
                 new PrivilegedAction<AppClassLoader>() {
                     public AppClassLoader run() {
-                    URL[] urls =
-                        (s == null) ? new URL[0] : pathToURLs(path);
+                        URL[] urls = pathToURLs(path);
                     return new AppClassLoader(urls, extcl);
                 }
             });
         }
 

@@ -318,12 +262,11 @@
         }
 
         /**
          * allow any classes loaded from classpath to exit the VM.
          */
-        protected PermissionCollection getPermissions(CodeSource codesource)
-        {
+        protected PermissionCollection getPermissions(CodeSource codesource) {
             PermissionCollection perms = super.getPermissions(codesource);
             perms.add(new RuntimePermission("exitVM"));
             return perms;
         }
 

@@ -366,15 +309,16 @@
     }
 
     private static class BootClassPathHolder {
         static final URLClassPath bcp;
         static {
-            URL[] urls;
-            if (bootClassPath != null) {
-                urls = AccessController.doPrivileged(
+            URL[] urls = AccessController.doPrivileged(
                     new PrivilegedAction<URL[]>() {
                         public URL[] run() {
+                            String bootClassPath = System.getProperty("sun.boot.class.path");
+                            if (bootClassPath == null)
+                                return new URL[0];
                             // Skip empty path in boot class path i.e. not default to use CWD
                             File[] classPath = getClassPath(bootClassPath, false);
                             int len = classPath.length;
                             Set<File> seenDirs = new HashSet<File>();
                             for (int i = 0; i < len; i++) {

@@ -390,13 +334,10 @@
                             }
                             return pathToURLs(classPath);
                         }
                     }
                 );
-            } else {
-                urls = new URL[0];
-            }
             bcp = new URLClassPath(urls, factory);
         }
     }
 
     public static URLClassPath getBootstrapClassPath() {