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 10 of 10

Thread: Class loader wont let me extend classes...?

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Class loader wont let me extend classes...?

    Hey guys. So I am dynamically loading classes from a jar file. If I try to make two classes, B and C, that both extend A, it says that I am trying to define class A multiple times and crashes. I was wondering how to fix this. Please reply asap! The class loader I am using is shown below:

    package com.sci.rpi;
     
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
    import java.util.zip.ZipInputStream;
     
    public class ZipClassLoader extends ClassLoader
    {
    	private ZipFile file;
     
    	public ZipClassLoader(File file) throws IOException
    	{
    		this.file = new ZipFile(file);
    	}
     
    	public ZipClassLoader(ClassLoader parent, File file) throws IOException
    	{
    		super(parent);
    		this.file = new ZipFile(file);
    	}
     
    	@Override
    	protected Class<?> findClass(String name) throws ClassNotFoundException
    	{
    		ZipEntry entry = this.file.getEntry(name.replace(".", "/") + ".class");
    		if(entry == null) { throw new ClassNotFoundException(name); }
    		try
    		{
    			byte[] data = new byte[1024];
    			InputStream in = this.file.getInputStream(entry);
    			ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
    			int len = in.read(data);
    			while(len > 0)
    			{
    				out.write(data, 0, len);
    				len = in.read(data);
    			}
    			return defineClass(name, out.toByteArray(), 0, out.size());
    		}
    		catch(IOException e)
    		{
    			throw new ClassNotFoundException(name, e);
    		}
    	}
     
    	public List<Class<?>> loadClasses() throws IOException
    	{
    		List<Class<?>> classes = new ArrayList<Class<?>>();
    		ZipInputStream zis = new ZipInputStream(new FileInputStream(file.getName()));
    		ZipEntry ze;
    		while((ze = zis.getNextEntry()) != null)
    		{
    			String[] blacklist = new String[]
    			{ "com/sci/rpi/api/RaspberryPi.class", "com/sci/rpi/api/Position.class", "META-INF/MANIFEST.MF" };
    			if(!contains(blacklist, ze.getName()) && !ze.getName().startsWith(".") && ze.getName().endsWith(".class"))
    			{
    				classes.add(constructClass(ze));
    			}
    		}
    		zis.close();
    		return classes;
    	}
     
    	public boolean contains(String[] blacklist, String name)
    	{
    		for(String str : blacklist)
    		{
    			if(str.equals(name))
    				return true;
    		}
    		return false;
    	}
     
    	private Class<?> constructClass(ZipEntry entry) throws IOException
    	{
    		byte[] data = new byte[1024];
    		InputStream in = this.file.getInputStream(entry);
    		ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
    		int len = in.read(data);
    		while(len > 0)
    		{
    			out.write(data, 0, len);
    			len = in.read(data);
    		}
    		return defineClass((entry.getName().replace(".class", "")).replace("/", "."), out.toByteArray(), 0, out.size());
    	}
     
    	@Override
    	protected URL findResource(String name)
    	{
    		ZipEntry entry = this.file.getEntry(name);
    		if(entry == null) { return null; }
    		try
    		{
    			return new URL("jar:file:" + this.file.getName() + "!/" + entry.getName());
    		}
    		catch(MalformedURLException exception)
    		{
    			return null;
    		}
    	}
    }


  2. #2
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class loader wont let me extend classes...?

    Anyone? I kind of NEED this... so...

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class loader wont let me extend classes...?

    I still haven't been able to figure out how to fix this... can someone help? Someone must know how to fix this...

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Class loader wont let me extend classes...?

    Quote Originally Posted by sci4me View Post
    ...it says that I am trying to define class A multiple times and crashes...
    Always post the full text of any error messages with your code and question.
    Any code on the forum lengthy enough to get it's own scroll bar should have some comment in the code to draw forum eyes attention to the suspect area.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class loader wont let me extend classes...?

    I literally told you the error...
    Caused by: java.lang.LinkageError: loader (instance of  com/sci/rpi/util/ZipClassLoader): attempted  duplicate class definition for name: "Console"
    	at java.lang.ClassLoader.defineClass1(Native Method)
    	at java.lang.ClassLoader.defineClass(ClassLoader.java:787)
    	at java.lang.ClassLoader.defineClass(ClassLoader.java:630)
    	at com.sci.rpi.util.ZipClassLoader.constructClass(ZipClassLoader.java:94)
    	at com.sci.rpi.util.ZipClassLoader.loadClasses(ZipClassLoader.java:66)

    I think its because it isnt making my classes unique to others - not using the packages? Im not really sure... since I only have one interface of console... so.. idk... there must be something wrong in constructClass or loadClasses... what is it that im doing wrong?

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Class loader wont let me extend classes...?

    Quote Originally Posted by sci4me View Post
    I literally told you the error...

    "it says that I am trying to define class A multiple times and crashes"
    is NOT the same as
    Caused by: java.lang.LinkageError: loader (instance of com/sci/rpi/util/ZipClassLoader): attempted duplicate class definition for name: "Console"
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :787)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :630)
    at com.sci.rpi.util.ZipClassLoader.constructClass(Zip ClassLoader.java:94)
    at com.sci.rpi.util.ZipClassLoader.loadClasses(ZipCla ssLoader.java:66)
    The actual error message contains useful information in troubleshooting the cause of the problem.


    If I try to make two classes, B and C, that both extend A, it says that I am trying to define class A multiple times
    I don't see any classes named A B or C. Where is Console?
    Perhaps you should make an SSCCE.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class loader wont let me extend classes...?

    ... classes a b and c were examples...
    it doesnt matter what the class is, it happens no matter what the classes are
    console could be a and there could be some class b and/or c that extends console.. but in this case, thats not happening
    for some reason, using another class at all (console) is crashing it... all im doing is making an instance of console...

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Class loader wont let me extend classes...?

    Quote Originally Posted by sci4me View Post
    all im doing is making an instance of console...
    Your opinion does not match that of the compiler.
    attempted duplicate class definition for name: "Console"
    Verify your assumptions

  9. #9
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class loader wont let me extend classes...?

    Quote Originally Posted by jps View Post
    Your opinion does not match that of the compiler.
    attempted duplicate class definition for name: "Console"
    Verify your assumptions
    It is probably because Console is an interface and i am doing this:
    new Console(){}
    And filling in the methods. However, that isn't the point. The point is that I am getting this error and I don't know how to fix it. Now that I think about it, I should have probably posted in "Whats wrong with my code?" but.. too late now. Anyway, can someone at least tell me why the error happens? What's wrong with making more than one class that extends another class...? Because that is what appears to be causing this...

  10. #10
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Class loader wont let me extend classes...?

    ok, I am now using this to load the class:
    URLClassLoader child = new URLClassLoader(new URL[]
    					{ jarFile.toURI().toURL() }, this.getClass().getClassLoader());
    					URL url = child.findResource("META-INF/MANIFEST.MF");
    					Manifest manifest = new Manifest(url.openStream());
    					Class<? extends RaspberryPi> main = (Class<? extends RaspberryPi>) child.loadClass(manifest.getMainAttributes().getValue("Main-Class"));
    					RaspberryPi pi = main.newInstance();
    					ret = pi;
    					child.close();
    However, that doesnt automatically load the other classes for me... and its not possible for me to just add them into that code to laod them because I dont know what they are going to be... so... How do i fix this? In the object pi/ret, I am using another class and since it isnt loaded, i get this:
    2013-05-19 13:13:57 [INFO] [STDERR] Exception in thread "Thread-14" java.lang.NoClassDefFoundError: com/sci/ss/SciScriptEngine
    2013-05-19 13:13:57 [INFO] [STDERR] 	at com.sci.RPI.run(RPI.java:144)
    2013-05-19 13:13:57 [INFO] [STDERR] 	at java.lang.Thread.run(Thread.java:722)
    2013-05-19 13:13:57 [INFO] [STDERR] Caused by: java.lang.ClassNotFoundException: com.sci.ss.SciScriptEngine
    2013-05-19 13:13:57 [INFO] [STDERR] 	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    2013-05-19 13:13:57 [INFO] [STDERR] 	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    2013-05-19 13:13:57 [INFO] [STDERR] 	at java.security.AccessController.doPrivileged(Native Method)
    2013-05-19 13:13:57 [INFO] [STDERR] 	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    2013-05-19 13:13:57 [INFO] [STDERR] 	at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    2013-05-19 13:13:57 [INFO] [STDERR] 	at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    2013-05-19 13:13:57 [INFO] [STDERR] 	... 2 more
    And I don't know how to fix it. Ideas?

Similar Threads

  1. Where to define ur Custom Class Loader?
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: January 25th, 2012, 05:13 AM
  2. Class Loader question
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: January 17th, 2012, 10:24 PM
  3. Replies: 6
    Last Post: July 18th, 2011, 08:48 AM
  4. [SOLVED] Class loader , InputStream-AudioStream (method) , returns an AudioStream Objet
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 14
    Last Post: July 9th, 2011, 11:14 AM
  5. class wont compile
    By waspandor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 15th, 2011, 03:40 PM