src/java.base/share/classes/java/lang/ClassLoader.java

Print this page


   1 /*
   2  * Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1730         }
1731     }
1732 
1733     // All native library names we've loaded.
1734     private static Vector<String> loadedLibraryNames = new Vector<>();
1735 
1736     // Native libraries belonging to system classes.
1737     private static Vector<NativeLibrary> systemNativeLibraries
1738         = new Vector<>();
1739 
1740     // Native libraries associated with the class loader.
1741     private Vector<NativeLibrary> nativeLibraries = new Vector<>();
1742 
1743     // native libraries being loaded/unloaded.
1744     private static Stack<NativeLibrary> nativeLibraryContext = new Stack<>();
1745 
1746     // The paths searched for libraries
1747     private static String usr_paths[];
1748     private static String sys_paths[];
1749 
1750     private static String[] initializePath(String propname) {
1751         String ldpath = System.getProperty(propname, "");
1752         String ps = File.pathSeparator;
1753         int ldlen = ldpath.length();
1754         int i, j, n;
1755         // Count the separators in the path
1756         i = ldpath.indexOf(ps);
1757         n = 0;
1758         while (i >= 0) {
1759             n++;
1760             i = ldpath.indexOf(ps, i + 1);
1761         }
1762 
1763         // allocate the array of paths - n :'s = n + 1 path elements
1764         String[] paths = new String[n + 1];
1765 
1766         // Fill the array with paths from the ldpath
1767         n = i = 0;
1768         j = ldpath.indexOf(ps);
1769         while (j >= 0) {
1770             if (j - i > 0) {
1771                 paths[n++] = ldpath.substring(i, j);
1772             } else if (j - i == 0) {
1773                 paths[n++] = ".";
1774             }
1775             i = j + 1;
1776             j = ldpath.indexOf(ps, i);























1777         }
1778         paths[n] = ldpath.substring(i, ldlen);

1779         return paths;
1780     }
1781 
1782     // Invoked in the java.lang.Runtime class to implement load and loadLibrary.
1783     static void loadLibrary(Class<?> fromClass, String name,
1784                             boolean isAbsolute) {
1785         ClassLoader loader =
1786             (fromClass == null) ? null : fromClass.getClassLoader();
1787         if (sys_paths == null) {
1788             usr_paths = initializePath("java.library.path");
1789             sys_paths = initializePath("sun.boot.library.path");
1790         }
1791         if (isAbsolute) {
1792             if (loadLibrary0(fromClass, new File(name))) {
1793                 return;
1794             }
1795             throw new UnsatisfiedLinkError("Can't load library: " + name);
1796         }
1797         if (loader != null) {
1798             String libfilename = loader.findLibrary(name);


   1 /*
   2  * Copyright (c) 2013, 2015 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1730         }
1731     }
1732 
1733     // All native library names we've loaded.
1734     private static Vector<String> loadedLibraryNames = new Vector<>();
1735 
1736     // Native libraries belonging to system classes.
1737     private static Vector<NativeLibrary> systemNativeLibraries
1738         = new Vector<>();
1739 
1740     // Native libraries associated with the class loader.
1741     private Vector<NativeLibrary> nativeLibraries = new Vector<>();
1742 
1743     // native libraries being loaded/unloaded.
1744     private static Stack<NativeLibrary> nativeLibraryContext = new Stack<>();
1745 
1746     // The paths searched for libraries
1747     private static String usr_paths[];
1748     private static String sys_paths[];
1749 
1750     private static String[] initializePath(String propName) {
1751         String ldPath = System.getProperty(propName, "");
1752         int ldLen = ldPath.length();
1753         char ps = File.pathSeparatorChar;
1754         int psCount = 0;
1755 
1756         if (ClassLoaderHelper.allowsQuotedPathElements &&
1757                 ldPath.indexOf('\"') >= 0) {
1758             // First, remove quotes put around quoted parts of paths.
1759             // Second, use a quotation mark as a new path separator.
1760             // This will preserve any quoted old path separators.
1761             char[] buf = new char[ldLen];
1762             int bufLen = 0;
1763             for (int i = 0; i < ldLen; ++i) {
1764                 char ch = ldPath.charAt(i);
1765                 if (ch == '\"') {
1766                     while (++i < ldLen &&
1767                             (ch = ldPath.charAt(i)) != '\"') {
1768                         buf[bufLen++] = ch;





1769                     }
1770                 } else {
1771                     if (ch == ps) {
1772                         psCount++;
1773                         ch = '\"';
1774                     }
1775                     buf[bufLen++] = ch;
1776                 }
1777             }
1778             ldPath = new String(buf, 0, bufLen);
1779             ldLen = bufLen;
1780             ps = '\"';
1781         } else {
1782             for (int i = ldPath.indexOf(ps); i >= 0;
1783                     i = ldPath.indexOf(ps, i + 1)) {
1784                 psCount++;
1785             }
1786         }
1787 
1788         String[] paths = new String[psCount + 1];
1789         int pathStart = 0;
1790         for (int j = 0; j < psCount; ++j) {
1791             int pathEnd = ldPath.indexOf(ps, pathStart);
1792             paths[j] = (pathStart < pathEnd) ?
1793                     ldPath.substring(pathStart, pathEnd) : ".";
1794             pathStart = pathEnd + 1;
1795         }
1796         paths[psCount] = (pathStart < ldLen) ?
1797                 ldPath.substring(pathStart, ldLen) : ".";
1798         return paths;
1799     }
1800 
1801     // Invoked in the java.lang.Runtime class to implement load and loadLibrary.
1802     static void loadLibrary(Class<?> fromClass, String name,
1803                             boolean isAbsolute) {
1804         ClassLoader loader =
1805             (fromClass == null) ? null : fromClass.getClassLoader();
1806         if (sys_paths == null) {
1807             usr_paths = initializePath("java.library.path");
1808             sys_paths = initializePath("sun.boot.library.path");
1809         }
1810         if (isAbsolute) {
1811             if (loadLibrary0(fromClass, new File(name))) {
1812                 return;
1813             }
1814             throw new UnsatisfiedLinkError("Can't load library: " + name);
1815         }
1816         if (loader != null) {
1817             String libfilename = loader.findLibrary(name);