1 /* Copyright (C) 1999, 2001, 2002, 2003, 2004 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 /* Author: Kresten Krab Thorup <krab@gnu.org> */
11 package gnu.gcj.runtime;
14 import java.util.StringTokenizer;
15 import java.util.HashSet;
18 public final class VMClassLoader extends java.net.URLClassLoader
20 private VMClassLoader ()
24 = System.getProperty ("gnu.gcj.runtime.VMClassLoader.library_control",
26 if ("never".equals(p))
27 lib_control = LIB_NEVER;
28 else if ("cache".equals(p))
29 lib_control = LIB_CACHE;
30 else if ("full".equals(p))
32 // In case we ever want to change the default.
33 lib_control = LIB_FULL;
36 lib_control = LIB_FULL;
42 = new StringTokenizer (System.getProperty ("java.class.path", "."),
43 System.getProperty ("path.separator", ":"));
45 while (st.hasMoreElements ())
47 String e = st.nextToken ();
50 File path = new File(e);
51 // Ignore invalid paths.
54 if (!e.endsWith (File.separator) && path.isDirectory ())
55 addURL(new URL("file", "", -1, e + File.separator));
57 addURL(new URL("file", "", -1, e));
59 catch (java.net.MalformedURLException x)
61 // This should never happen.
62 throw new RuntimeException(x);
66 // Add the contents of the extensions directories.
67 st = new StringTokenizer (System.getProperty ("java.ext.dirs"),
68 System.getProperty ("path.separator", ":"));
72 while (st.hasMoreElements ())
74 String dirname = st.nextToken ();
75 File dir = new File (dirname);
78 if (! dirname.endsWith (File.separator))
79 dirname = dirname + File.separator;
81 = dir.list (new FilenameFilter ()
83 public boolean accept (File dir, String name)
85 return (name.endsWith (".jar")
86 || name.endsWith (".zip"));
89 for (int i = files.length - 1; i >= 0; i--)
90 addURL(new URL("file", "", -1, dirname + files[i]));
94 // Add core:/ to the end of the java.class.path so any resources
95 // compiled into this executable may be found.
96 addURL(new URL("core", "", -1, "/"));
98 catch (java.net.MalformedURLException x)
100 // This should never happen.
101 throw new RuntimeException(x);
105 /** This is overridden to search the internal hash table, which
106 * will only search existing linked-in classes. This will make
107 * the default implementation of loadClass (in ClassLoader) work right.
108 * The implementation of this method is in java/lang/natClassLoader.cc.
110 protected native Class findClass(String name)
111 throws java.lang.ClassNotFoundException;
113 // This can be package-private because we only call it from native
114 // code during startup.
115 static void initialize ()
120 // This keeps track of shared libraries we've already tried to load.
121 private HashSet tried_libraries = new HashSet();
123 // Holds one of the LIB_* constants; used to determine how shared
124 // library loads are done.
125 private int lib_control;
127 // The only VMClassLoader that can exist.
128 static VMClassLoader instance = new VMClassLoader();
130 private static final int LIB_FULL = 0;
131 private static final int LIB_CACHE = 1;
132 private static final int LIB_NEVER = 2;