Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Custom class loader and local class accessing local field

7 views
Skip to first unread message

Knut Støre

unread,
Jan 28, 2003, 9:17:37 AM1/28/03
to
I have written my own class loader to solve a specific problem. It
seemed to work very well, but then I started noticing strange errors in
the log output. Here is an example. Some of the names are in Norwegian,
but they are not important to this discussion. JavaNotis.Oppstart is the
name of my class loader class.

java.lang.ClassFormatError: JavaNotis/SendMeldingDialog$1 (Illegal
variable name " val$indeks")
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
at java.lang.ClassLoader.defineClass(ClassLoader.java:431)
at JavaNotis.Oppstart.findClass(Oppstart.java:193)
at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
at JavaNotis.SendMeldingDialog.init(SendMeldingDialog.java:78)
at JavaNotis.SendMeldingDialog.<init>(SendMeldingDialog.java:54)
at JavaNotis.Notistavle.sendMelding(Notistavle.java:542)
at JavaNotis.Notistavle.access$900(Notistavle.java:59)
at JavaNotis.Notistavle$27.actionPerformed(Notistavle.java:427)
...


JavaNotis/SendMeldingDialog$1 is a local class in the method
JavaNotis.SendMeldingDialog.init, and it's accessing a final local
variable named indeks. The compiler automatically turns this into a
variable in the inner class called val$indeks. But look at the error
message, there is an extra space in front of the variable name.

This error doesn't occur when I don't use my custom class loader and
instead load the classes through the default class loader in the JVM.

Here is my class loading code. Is there something wrong with it?
Again some Norwegian words, but it should still be understandable I hope.

protected Class findClass(String name) throws ClassNotFoundException
{
byte[] b = loadClassData(name);
return defineClass(name, b, 0, b.length);
}

private byte[] loadClassData(String name) throws
ClassNotFoundException
{
ByteArrayOutputStream ut = null;
InputStream inn = null;

try
{
JarEntry klasse = arkiv.getJarEntry(name.replace('.', '/')
+ ".class");

if (klasse == null)
throw new ClassNotFoundException("Finner ikke klassen "
+ NOTISKLASSE);

inn = arkiv.getInputStream(klasse);
ut = new ByteArrayOutputStream(inn.available());
byte[] kode = new byte[4096];
int antall = inn.read(kode);

while (antall > 0)
{
ut.write(kode, 0, antall);
antall = inn.read(kode);
}

return ut.toByteArray();
}
catch (IOException ioe)
{
throw new RuntimeException(ioe.getMessage());
}
finally
{
try
{
if (inn != null)
inn.close();

if (ut != null)
ut.close();
}
catch (IOException ioe)
{
}
}
}

I hope somebody can help. :-)

Regards,
Knut Støre

0 new messages