Class NativeLoader

java.lang.Object
  extended by NativeLoader

public class NativeLoader
extends java.lang.Object

This is for loading classes which use a native library.

Greenfoot uses classloaders to allow the same class to be loaded multiple times. However if a class loads a native library, then that will be loaded multiple times. Loading a native library multiple times is not allowed by the JVM, and so it will fail.

This solution works by manually loading classes using the root classloader, which is the parent to all of Greenfoots classloaders. By doing this the class is only ever loaded once and is accessible to all of the child classloaders.

Author:
Joseph Lenton

Constructor Summary
NativeLoader()
          Trivial constructor.
 
Method Summary
 void addClasspath(java.io.File dir)
          This is for adding directories to check when a loaded class is searching for classes.
 void addClasspath(java.lang.String directory)
          The same as the other addClasspathDirectory method, only this takes a string for the directory to add rather then a File object.
 void loadClass(java.lang.String name)
          Looks for and then loads the class given using the root class loader.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NativeLoader

public NativeLoader()
Trivial constructor.

Method Detail

addClasspath

public void addClasspath(java.io.File dir)

This is for adding directories to check when a loaded class is searching for classes.

The idea is that if you have a class loaded using the root classloader, when that class imports other non-loaded classes then you want them to be found. That can be done by adding the folder where those classes are found to the root classloader. This method allows you to do that.

Note that this only has an effect when the JVM searches itself for a class; it has no effect on where the methods 'loadClass' and 'loadClasses' search.

You cannot add the Greenfoot project folder to the classpath (which is located at '.') as this will cause Greenfoot to crash.

You can also pass in a .jar file instead of a directory to add that to the class patth.

Parameters:
dir - The directory to add to the root classloaders classpath, cannot be null.

addClasspath

public void addClasspath(java.lang.String directory)

The same as the other addClasspathDirectory method, only this takes a string for the directory to add rather then a File object.

Parameters:
directory - The directory to add to the root classloaders classpath.

loadClass

public void loadClass(java.lang.String name)

Looks for and then loads the class given using the root class loader.

The given name should be the name of a java class, such as: net.foo.MyClass

If the class is in the default package then it is presumed to be residing inside of the Greenfoot project folder.

Parameters:
The - Java name of the class to load.