Hi, Peter!
I think, you should use your classloader by using 2-level architecture. First,
you try to load class through forName, and if program throws an exception, load
class from archive, like this:
...
private FileClassLoader loader;
...
public Class getClass(String className) throws Exception {
Class clazz = null;
try {
clazz = Class.forName(className.replace('/', '.'));
}
catch(Throwable e) {
try {
clazz = loader.loadClass(className.replace('/', '.'), true);
}
catch(Throwable e2) {
throw new Exception("Error reading class " + className);
}
}
return clazz;
}
[EMAIL PROTECTED] wrote:
> I have written my own Custom Class Loader to emulate the Primordial [System
> Default] Class Loader in the Virtual Machine?
>
> It works fine, except I don't think it work across packages.
>
> Basically I would like to do this :
>
> Properties jvmProps = System.getProperties();
> String oldClassPath = System.getProperty("java.class.path");
>
> jvmProp.put( "java.class.path", "extra/new/sexy.jar:"+oldClassPath );
>
> But of course, this does not work! Hence my custom class loader.
>
> Now want I have done is this. I have a java source file called
> `foobar.java' linked a this extra Jar.
>
> % javac -g -classpath animal.jar:$CLASSPATH acme/apps/foobar.java
>
> Let suppose `sexy.jar' contains a class `animals/pets/Cat.class'
> that `foobar.java' imports.
>
> Once I compiled every java class for the project I can invoke the
> application with.
>
> % java -classpath animal.jar:$CLASSPATH acme.apps.foobar.java
>
> Now I have made my CustomClassLoader bit more clever than usual.
> It load classes, yes, but it also can invoke the main method of another
> dynamically loaded class. In other words I use the it to load the
> `acme.apps.foobar.class' and dynamically invoke the `public static void
> main( String [] args )' for it.
>
> (#1)
>
> % java CustomClassLoaderLauncher animal.jar acme.apps.foobar
>
> So the first argument is the extra class path to find classes and the
> second argument is the class name containing `main()'.
>
> I tried this in JDK 1.1.7-v1a Solaris/Linux. Although it loads the class
> `acme.apps.foobar' and invoke `main()', it fails to find the
> `animals.pet.Cat.class' inside the `animal.jar'.
> I get `ClassDefNotFound' exception throwned.
>
> However when I explicitly specify the jar in the classpath flag it works.
>
> (#2)
> % java -classpath animal.jar:$CLASSPATH CustomClassLoaderLauncher
> animal.jar acme.apps.foobar
>
> It seems to me it is impossible for custom class loader and launcher across
> packages.
>
> Why does (#1) fail and (#2) works ?
> Can anyone figure out what I have done wrong?
>
> Peter Pilgrim
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]