public class JarClassLoader extends SecureClassLoader implements Closeable
Please note that this class is NOT thread safe for adding additional Jars and subdirectories. This should be done PRIOR to using the class loader. This way, the code does not use any synchronization and is therefore faster.
Constructor and Description |
---|
JarClassLoader(ClassLoader parent,
JarFilePath... jarFilePaths)
Creates the Jar file class loader using the default parent class loader for delegation.
|
JarClassLoader(IJarFile jar)
Creates the Jar file class loader using the default parent class loader for delegation.
|
JarClassLoader(IJarFile jar,
ClassLoader parent)
Creates the Jar file class loader with a specified parent class loader for delegation.
|
JarClassLoader(IJarFile jar,
String subDirectory,
ClassLoader parent)
Creates the Jar file class loader using the default parent class loader for delegation.
|
Modifier and Type | Method and Description |
---|---|
void |
add(File directory)
Adds a new Directory entry to the class loader.
|
void |
add(IJarFile jar)
Adds a new jar with root as subdirectory to the class loader.
|
void |
add(IJarFile jar,
String subDirectory)
Adds a new jar with subdirectory to the class loader.
|
void |
add(JarFilePath jarFilePath)
Adds a new JarFilePath entry to the class loader.
|
void |
close()
Closes this class loader, so that it can no longer be used to load
new classes or resources that are defined by this loader.
|
Class<?> |
findClass(String name)
Finds the class with the specified binary name.
|
URL |
findResource(String name)
Finds the resource with the given name.
|
Enumeration<URL> |
findResources(String name)
Returns an enumeration of
objects
representing all the resources with the given name. |
InputStream |
getResourceAsStream(String name)
Open for reading, a resource of the specified name from the search path used to load classes.
|
void |
initialize()
Initializes the class loader by defining all packages.
|
defineClass, defineClass, getPermissions
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLibrary, findLoadedClass, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getResource, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, registerAsParallelCapable, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
public JarClassLoader(IJarFile jar)
It is IMPORTANT to call
before using the
class loader.initialize()
jar
- The Jar file for the class path.public JarClassLoader(IJarFile jar, ClassLoader parent)
It is IMPORTANT to call
before using the
class loader.initialize()
jar
- The Jar file for the class path.parent
- The Parent class loader.public JarClassLoader(IJarFile jar, String subDirectory, ClassLoader parent) throws IllegalArgumentException
It is IMPORTANT to call
before using the
class loader.initialize()
jar
- The Jar file for the class path.subDirectory
- Path inside the Jar file, e.g. System/classpath/bin/, or null
for the entire JarFile. If an entry is not ending with a "/",
it will be appended.parent
- The Parent class loader.IllegalArgumentException
- If the subDirectory
is not found in the Jar file.public JarClassLoader(ClassLoader parent, JarFilePath... jarFilePaths) throws IllegalArgumentException
It is IMPORTANT to call
before using the
class loader.initialize()
parent
- The Parent class loader.jarFilePaths
- Jar file and paths list or array.IllegalArgumentException
- If the subDirectory
is not found in the Jar file.public final void add(IJarFile jar)
jar
- The Jar file for the class path.IllegalStateException
- If class loader already is initialized or has been closed.public final void add(IJarFile jar, String subDirectory)
jar
- The Jar file for the class path.subDirectory
- Path inside the Jar file, e.g. System/classpath/bin/, or null
for the entire JarFile. If an entry is not ending with a "/",
it will be appended.IllegalStateException
- If class loader already is initialized or has been closed.public final void add(JarFilePath jarFilePath)
jarFilePath
- The Jar file for the class path.IllegalStateException
- If class loader already is initialized or has been closed.public final void add(File directory) throws IOException
directory
- The directory for the class path.IOException
- If the directory is not found or refers to a non-directory.IllegalStateException
- If class loader already is initialized or has been closed.public final void initialize() throws IOException
IOException
- For I/O errors or if there is a sealing problem in the Jar file.IllegalStateException
- If class loader already is initialized or has been closed.public void close() throws IOException
In the case of jar:, file:, npjar: and memjar: URLs, it also closes any
files that were opened by it. If another thread is loading a
class when the close
method is invoked, then the result of
that load is undefined.
The method makes a best effort attempt to close all opened files,
by catching IOException
's internally. Unchecked exceptions
and errors are not caught. Calling close on an already closed
loader has no effect.
close
in interface Closeable
close
in interface AutoCloseable
IOException
- If closing any file opened by this class loader
resulted in an IOException. Any such exceptions are
caught internally. If only one is caught, then it is
re-thrown. If more than one exception is caught, then
the second and following exceptions are added as
suppressed exceptions of the first one caught, which
is then re-thrown.public final Class<?> findClass(String name) throws ClassNotFoundException
findClass
in class ClassLoader
name
- The binary name of the classClassNotFoundException
- If the class could not be found.IllegalStateException
- If class loader is not initialized.ClassLoader.findClass(java.lang.String)
public final URL findResource(String name)
findResource
in class ClassLoader
name
- The resource name.URL
object for reading the resource, or
null
if the resource could not be found.IllegalStateException
- If class loader is not initialized.public final Enumeration<URL> findResources(String name)
URL
objects
representing all the resources with the given name. Class loader
implementations should override this method to specify where to load
resources from.findResources
in class ClassLoader
name
- The resource name.URL
objects for
the resources.IllegalStateException
- If class loader is not initialized.public final InputStream getResourceAsStream(String name)
getResourceAsStream
in class ClassLoader
name
- the name of the resource.IllegalStateException
- If class loader is not initialized.ClassLoader.getResourceAsStream(java.lang.String)
Phantom® and NetPhantom® are registered trademarks of Mindus SARL.
© © Mindus SARL, 2024. All rights reserved.