|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.lang.ClassLoader
java.security.SecureClassLoader
java.net.URLClassLoader
com.wolfram.jlink.JLinkClassLoader
public class JLinkClassLoader
This class loader does all the loading of classes requested via the Mathematica function LoadJavaClass[]. It also loads native libraries and resources used by classes that it loads. The directories and files it searches for classes and other resources are provided to it via the addLocations() method, which is called from Mathematica during every call to LoadJavaClass[]. The JLink Mathematica code manages the class path locations, including providing a default set of extra dirs. Directories are automatically searched for jar or zip files they contain, so you do not have to name jar files explicitly in addLocations(), although you can if you want to limit the search to a specific jar file in a directory.
Most uses of this class are internal to J/Link, and most programmers will never deal with it directly.
One reason to use this class is if you want to load a class in Java code that is found in the special
set of extra locations that this class loader knows about (such as Java subdirectories of Mathematica
application directories). Each KernelLink has a JLinkClassLoader instance associated with it,
which you can retrieve using the link's getClassLoader() method.
Here is an example of loading a class from a special J/Link-specific directory.
Class cls = Class.forName("Some.class.that.only.JLink.can.find", ml.getClassLoader());
You can add directories and jar files to the search path using the addLocations() method:
// This is equivalent to calling AddToClassPath["/some/dir", "/another/dir"] in Mathematica.
ml.getClassLoader().addLocations(new String[]{"/some/dir", "/another/dir"}, true);
If you using this class from a standalone Java program you should first call
the KernelLink method enableObjectReferences() to
initialize the class loader with the special Mathematica-specific locations for Java classes.
Another advanced reason to use this class is if you need the class loader to have a specific parent loader, because that parent loader has certain capabilities or can load classes from certain locations that J/Link would otherwise not know about.
KernelLink ml = MathLinkFactory.createKernelLink(...); ml.discardAnswer(); ml.setClassLoader(new JLinkClassLoader(mySpecialParentClassLoader)); ml.enableObjectReferences();The class has some static methods that are intended to be used from Mathematica code. Here is an example that loads a resource from a Mathematica application's Java subdirectory:
(* Mathematica code *) LoadJavaClass["com.wolfram.jlink.JLinkClassLoader"]; JLinkClassLoader`getInstance[]@getResource["myImage.gif"];When using the class from Java, you will always have a specific link in hand, so instead of calling static methods you should obtain a JLinkClassLoader instance from the link using its getClassLoader() method and then call instance methods.
KernelLink.getClassLoader(),
KernelLink.setClassLoader(JLinkClassLoader)| Constructor Summary | |
|---|---|
JLinkClassLoader()
Constructs a new JLinkClassLoader instance. |
|
JLinkClassLoader(java.lang.ClassLoader parent)
Constructs a new JLinkClassLoader instance that has the given class loader as its parent loader. |
|
| Method Summary | |
|---|---|
void |
addLocations(java.lang.String[] locations,
boolean searchForJars)
Adds URLs, directories, and jar files in which this classloader will look for classes. |
java.lang.Class |
classFromBytes(java.lang.String className,
byte[] bytes)
Converts an array of bytes into an instance of class Class. |
static java.lang.Class |
classFromName(java.lang.String name)
Loads the class with the given name using the JLinkClassLoader instance of the currently-active link. |
protected java.lang.String |
findLibrary(java.lang.String libName)
|
java.lang.String[] |
getClassPath()
Gives the set of locations in which this class loader will search for classes. |
static JLinkClassLoader |
getInstance()
Retrieves the JLinkClassLoader instance being used by the currently-active link. |
| Methods inherited from class java.net.URLClassLoader |
|---|
addURL, definePackage, findClass, findResource, findResources, getPermissions, getURLs, newInstance, newInstance |
| Methods inherited from class java.security.SecureClassLoader |
|---|
defineClass, defineClass |
| Methods inherited from class java.lang.ClassLoader |
|---|
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findLoadedClass, findSystemClass, getPackage, getPackages, getParent, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, loadClass, resolveClass, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public JLinkClassLoader()
public JLinkClassLoader(java.lang.ClassLoader parent)
| Method Detail |
|---|
public static JLinkClassLoader getInstance()
public static java.lang.Class classFromName(java.lang.String name)
throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
public void addLocations(java.lang.String[] locations,
boolean searchForJars)
public java.lang.String[] getClassPath()
public java.lang.Class classFromBytes(java.lang.String className,
byte[] bytes)
protected java.lang.String findLibrary(java.lang.String libName)
findLibrary in class java.lang.ClassLoader
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||