From: Luis on
How do you force a java class to load, so that class initialization occurs?

The obvious approach:

>> java.lang.Class.forName('org.apache.tools.zip.ZipFile')

generates a ClassNotFoundException exception.

The goal is to just get the class initializer to run (and leave the class loaded, as shown by inmem). One kludge would be to find a way to *construct* the class

>> x = org.apache.tools.zip.ZipFile(123)

(if the class happens to have a constructor that takes an int, which it does not). Clearly this is undesirable since one may not want to construct an instance of the class.

Thanks.

Luis
From: Yair Altman on
"Luis " <lemon.loshea.removethisandfruit(a)gmail.com> wrote in message <hsvlkk$bpu$1(a)fred.mathworks.com>...
> How do you force a java class to load, so that class initialization occurs?
>
> The obvious approach:
>
> >> java.lang.Class.forName('org.apache.tools.zip.ZipFile')
>
> generates a ClassNotFoundException exception.
>
> The goal is to just get the class initializer to run (and leave the class loaded, as shown by inmem). One kludge would be to find a way to *construct* the class
>
> >> x = org.apache.tools.zip.ZipFile(123)
>
> (if the class happens to have a constructor that takes an int, which it does not). Clearly this is undesirable since one may not want to construct an instance of the class.
>
> Thanks.
>
> Luis


Instantiating a class object is my preferred way, but here's another alternative you could use, which I use in my checkClass utility on the Matlab File Exchange:

try
thisClass = java.lang.Class.forName(className);
catch
classLoader = com.mathworks.jmi.ClassLoaderManager.getClassLoaderManager;
thisClass = classLoader.loadClass(className);
end

This enables loading a class that has no constructor, or whose constructors you do not want, or are unable, to invoke.

Note that it uses the undocumented/unsupported JMI classloader, so it might not work on some versions/platforms/classes.

Yair Altman
http://UndocumentedMatlab.com
 | 
Pages: 1
Prev: error message
Next: random data between range.