Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ClassLoader and inner classes

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ClassLoader and inner classes

    I have this method (in my own classloader) that loads classes from zip:
    ZipInputStream in = new ZipInputStream(new FileInputStream(zip));
    ZipEntry entry;
    while ((entry = in.getNextEntry()) != null) {
    	if (!entry.isDirectory()) {
    		byte[] buffer = new byte[(int) entry.getSize()];
    		in.read(buffer);
    		if (!entry.getName().endsWith(".class"))
    			continue;
    		String name = entry.getName().replace(".class", "").replace("/", ".");
    		Class<?> cls = this.defineClass(name, buffer, 0, buffer.length);
    		this.resolveClass(cls);
    	}
    }

    The zip that im trying to load looks like this:
    TestClass.class
    TestClass$SomeOtherInnerClass.class
    My problem is that defineClass() fails to load the TestClass$SomeOtherInnerClass. If this class is loaded before the actual TestClass i get this:
    java.lang.NoClassDefFoundError: TestClass
    I also tried to load the TestClass.class first but then im getting this error:
    java.lang.ClassFormatError: Wrong InnerClasses attribute length in class file TestClass
    Please help me


  2. #2
    Junior Member Kenny's Avatar
    Join Date
    Apr 2011
    Location
    Dallas, TX
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ClassLoader and inner classes

    The problem is that the class loader cannot load the inner class. For example, URLClassLoader assumes all requests are for outer classes.

    Try this:
    Thread.currentThread().getContextClassLoader().loa dClass( classFQN );

    This works, even for inner classes. Many people recommend creating your own class loader or extending URLClassLoader but that is not actually necessary, most of the time.

Similar Threads

  1. Usage of ClassLoader.setSigners() method
    By AlexDorogensky in forum Java Theory & Questions
    Replies: 1
    Last Post: January 13th, 2011, 09:37 AM
  2. cant run the program with 2 classes
    By clone_hiryu in forum Object Oriented Programming
    Replies: 5
    Last Post: November 17th, 2010, 11:28 PM
  3. i/o classes question
    By p0oint in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 28th, 2010, 04:04 PM
  4. connecting two classes?
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 1st, 2009, 03:15 PM
  5. [SOLVED] Java program using two classes
    By AZBOY2000 in forum Object Oriented Programming
    Replies: 7
    Last Post: April 21st, 2009, 06:55 AM