public class JLinkClassLoader
extends java.lang.ClassLoader
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)| Modifier and Type | Field and Description |
|---|---|
protected com.wolfram.jlink.JLinkClassLoaderHelper |
helper |
| Constructor and Description |
|---|
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.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addLocations(java.lang.String[] locations,
boolean searchForJars)
Adds URLs, directories, and jar files in which this classloader will look for classes.
|
void |
addLocations(java.lang.String[] locations,
boolean searchForJars,
boolean prepend) |
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.
|
void |
clearAssertionStatus() |
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.
|
java.net.URL |
getResource(java.lang.String name) |
java.io.InputStream |
getResourceAsStream(java.lang.String name) |
java.util.Enumeration |
getResources(java.lang.String name) |
java.lang.Class |
loadClass(java.lang.String name) |
void |
setClassAssertionStatus(java.lang.String className,
boolean enabled) |
void |
setDefaultAssertionStatus(boolean enabled) |
void |
setPackageAssertionStatus(java.lang.String packageName,
boolean enabled) |
defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getClassLoadingLock, getPackage, getPackages, getParent, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, registerAsParallelCapable, resolveClass, setSignerspublic JLinkClassLoader()
public JLinkClassLoader(java.lang.ClassLoader parent)
public void clearAssertionStatus()
clearAssertionStatus in class java.lang.ClassLoaderpublic void setClassAssertionStatus(java.lang.String className,
boolean enabled)
setClassAssertionStatus in class java.lang.ClassLoaderpublic void setDefaultAssertionStatus(boolean enabled)
setDefaultAssertionStatus in class java.lang.ClassLoaderpublic void setPackageAssertionStatus(java.lang.String packageName,
boolean enabled)
setPackageAssertionStatus in class java.lang.ClassLoaderpublic java.net.URL getResource(java.lang.String name)
getResource in class java.lang.ClassLoaderpublic java.util.Enumeration getResources(java.lang.String name)
throws java.io.IOException
getResources in class java.lang.ClassLoaderjava.io.IOExceptionpublic java.io.InputStream getResourceAsStream(java.lang.String name)
getResourceAsStream in class java.lang.ClassLoaderpublic java.lang.Class loadClass(java.lang.String name)
throws java.lang.ClassNotFoundException
loadClass in class java.lang.ClassLoaderjava.lang.ClassNotFoundExceptionpublic static JLinkClassLoader getInstance()
public static java.lang.Class classFromName(java.lang.String name)
throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundExceptionpublic void addLocations(java.lang.String[] locations,
boolean searchForJars)
public void addLocations(java.lang.String[] locations,
boolean searchForJars,
boolean prepend)
public java.lang.String[] getClassPath()
public java.lang.Class classFromBytes(java.lang.String className,
byte[] bytes)
J/Link is Copyright (c) 1999-2012, Wolfram Research, Inc. All rights reserved.