com.wolfram.jlink
Class JLinkClassLoader

java.lang.Object
  extended by java.lang.ClassLoader
      extended by com.wolfram.jlink.JLinkClassLoader

public class JLinkClassLoader
extends java.lang.ClassLoader

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[] and also by AddToClassPath[]. 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.

Since:
3.0
See Also:
KernelLink.getClassLoader(), KernelLink.setClassLoader(JLinkClassLoader)

Field Summary
protected  com.wolfram.jlink.JLinkClassLoaderHelper helper
           
 
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.
 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.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)
           
 
Methods inherited from class java.lang.ClassLoader
defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findResources, findSystemClass, getPackage, getPackages, getParent, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, loadClass, resolveClass, setSigners
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

helper

protected com.wolfram.jlink.JLinkClassLoaderHelper helper
Constructor Detail

JLinkClassLoader

public JLinkClassLoader()
Constructs a new JLinkClassLoader instance. Only advanced programmers will need to use this.

Since:
3.0

JLinkClassLoader

public JLinkClassLoader(java.lang.ClassLoader parent)
Constructs a new JLinkClassLoader instance that has the given class loader as its parent loader. Only advanced programmers will need to use this.

Since:
3.0
Method Detail

clearAssertionStatus

public void clearAssertionStatus()
Overrides:
clearAssertionStatus in class java.lang.ClassLoader

setClassAssertionStatus

public void setClassAssertionStatus(java.lang.String className,
                                    boolean enabled)
Overrides:
setClassAssertionStatus in class java.lang.ClassLoader

setDefaultAssertionStatus

public void setDefaultAssertionStatus(boolean enabled)
Overrides:
setDefaultAssertionStatus in class java.lang.ClassLoader

setPackageAssertionStatus

public void setPackageAssertionStatus(java.lang.String packageName,
                                      boolean enabled)
Overrides:
setPackageAssertionStatus in class java.lang.ClassLoader

getResource

public java.net.URL getResource(java.lang.String name)
Overrides:
getResource in class java.lang.ClassLoader

getResourceAsStream

public java.io.InputStream getResourceAsStream(java.lang.String name)
Overrides:
getResourceAsStream in class java.lang.ClassLoader

loadClass

public java.lang.Class loadClass(java.lang.String name)
                          throws java.lang.ClassNotFoundException
Overrides:
loadClass in class java.lang.ClassLoader
Throws:
java.lang.ClassNotFoundException

getInstance

public static JLinkClassLoader getInstance()
Retrieves the JLinkClassLoader instance being used by the currently-active link. This method is intended to be called only from Mathematica, where the "active link" is well-defined (it is the link on which the call is coming in on).

Since:
3.0

classFromName

public static java.lang.Class classFromName(java.lang.String name)
                                     throws java.lang.ClassNotFoundException
Loads the class with the given name using the JLinkClassLoader instance of the currently-active link. This method is intended to be called only from Mathematica, where the "active link" is well-defined (it is the link on which the call is coming in on).

Throws:
java.lang.ClassNotFoundException
Since:
3.0

addLocations

public void addLocations(java.lang.String[] locations,
                         boolean searchForJars)
Adds URLs, directories, and jar files in which this classloader will look for classes. The elements of the locations array must be full paths to directories or jar or zip files, or http URLs. The searchForJars parameter determines whether directories will be automatically searched for any jar files they contain.

Since:
3.0

addLocations

public void addLocations(java.lang.String[] locations,
                         boolean searchForJars,
                         boolean prepend)

getClassPath

public java.lang.String[] getClassPath()
Gives the set of locations in which this class loader will search for classes. Only the locations known by this loader, not its parent loader, are returned.

Since:
3.0

classFromBytes

public java.lang.Class classFromBytes(java.lang.String className,
                                      byte[] bytes)
Converts an array of bytes into an instance of class Class. This method is effectively a public export of the final ClassLoader method defineClass(). It exists mainly for future internal uses within J/Link applications.

Since:
3.0


J/Link is Copyright (c) 1999-2008, Wolfram Research, Inc. All rights reserved.