8

I'm currently creating a plugin system (My first attempt), looking at other peoples code I'm trying to piece together my own classloader and get the plugins loaded from a directory (These will be class files)

My problem is that whenever I attempt to load the class with my classloader, any imports in the plugin referencing the program are not found by the classloader. (ie: MyClass extends Plugin, com.mgmc.plugins noclassdeffound) Different namespace?

Some sample code: Classloader:

/*


* To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mcgm.game.provider;

import com.mcgm.utils.Misc;
import com.mcgm.utils.Paths;
import java.awt.AWTPermission;
import java.io.*;
import java.net.MalformedURLException;
import java.net.SocketPermission;
import java.net.URL;
import java.security.CodeSigner;
import java.security.CodeSource;
import java.security.Permissions;
import java.security.ProtectionDomain;
import java.util.PropertyPermission;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Tom
 */
public class GameClassLoader extends ClassLoader {
private final ProtectionDomain domain;
private final URL base;

public GameClassLoader(final URL url) {
    base = url;
    final CodeSource codeSource = new CodeSource(base, (CodeSigner[]) null);
    domain = new ProtectionDomain(codeSource, getPermissions());
}

public void loadGames() {
    for (File f : Paths.compiledFolder.listFiles()) {
        try {
            Class c = loadClass(f.getPath());
            Misc.outPrint(c.getName());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GameClassLoader.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

private Permissions getPermissions() {
    final Permissions ps = new Permissions();
    ps.add(new AWTPermission("accessEventQueue"));
    ps.add(new PropertyPermission("user.home", "read"));
    ps.add(new PropertyPermission("java.vendor", "read"));
    ps.add(new PropertyPermission("java.version", "read"));
    ps.add(new PropertyPermission("os.name", "read"));
    ps.add(new PropertyPermission("os.arch", "read"));
    ps.add(new PropertyPermission("os.version", "read"));
    ps.add(new SocketPermission("*", "resolve"));
    ps.add(new FilePermission(Paths.compiledFolder.getPath(), "read,write,delete"));
    ps.setReadOnly();
    return ps;
}

@Override
@SuppressWarnings("rawtypes")
public Class<?> loadClass(final String name, final boolean resolve) throws ClassNotFoundException {
    Class clazz = findLoadedClass(name);

    if (clazz == null) {
        try {
            byte[] bytes = loadClassData(name);
            clazz = defineClass(name, bytes, 0, bytes.length, domain);
            if (resolve) {
                resolveClass(clazz);
            }
        } catch (final Exception e) {
            clazz = super.loadClass(name, resolve);
        }
    }

    return clazz;
}

public byte[] loadClassData(final String name) {
    try {
        final InputStream in = getResourceAsStream(name.replace('.', '/') + ".class");
        final byte[] buffer = new byte[4096];
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        int n;
        while ((n = in.read(buffer, 0, 4096)) != -1) {
            out.write(buffer, 0, n);
        }
        return out.toByteArray();
    } catch (IOException ex) {
        Logger.getLogger(GameClassLoader.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;

}

@Override
public URL getResource(final String name) {
    try {
        return new URL(base, name);
    } catch (final MalformedURLException e) {
        return null;
    }
}

@Override
public InputStream getResourceAsStream(final String name) {
    try {
        return new URL(base, name).openStream();
    } catch (final IOException e) {
        return null;
    }
}
}

The Plugin I am loading: (The annotation isn't found either)

import com.mcgm.GameInfo;
import com.mcgm.game.Minigame;
  @GameInfo(name = "RandomGame",
description = "A really long and boring game.",
authors = {"Tom", "Is", "The", "Greatest"},
version = 0.1,
maxPlayers = 100,
teamBased = false,
teamAmount = -1,
PvP = false)
public class game extends Minigame {
}

How I'm calling the class to be loaded:

  GameClassLoader classLoader = new GameClassLoader(Paths.compiledFolder.toURI().toURL());
            classLoader.loadClass("game", true);

I figure this is trivial for those that know what they're doing!

6
  • 2
    you're not being a burden man. People on SO are either asking questions or answering them. That's cool. Commented Oct 19, 2012 at 16:14
  • I'm surprised: you haven't any packages? I expect classLoader.loadClass( "a.b.c.d.game", true ); Commented Oct 19, 2012 at 16:28
  • I'm trying not to add packages, Only because of the way I want people to be able to add their own minigames - I know it goes against conventions but this way I'm able to have all minigames in a single folder (Or Jar) It's eventually going to scan the folder for all files, check for .class and .jar files and load them (No packages means no scanning subdirectories) Commented Oct 19, 2012 at 16:32
  • 3
    The issue is that your GameClassLoader isn't honoring the system class loader as its parent class loader to delegate to for finding classes. (But I'm not yet sure why; still researching though.) Commented Oct 19, 2012 at 16:42
  • Thanks Vulcan! I've been racking my brains for a day and a half now, I figured someone else would know! I tried using URLClassLoader and setting the parent loader to getSystemClassLoader() with no luck, but to be honest, If I think about it - I have a feeling it wouldn't load the resource at all hmm, perhaps the answer lies in writing a parent classloader into my custom one? Commented Oct 19, 2012 at 16:46

2 Answers 2

4

FINALLY! After a day and a half of searching, Vulcan pointed me in the right direction: I changed the GameClassLoader to:

public GameClassLoader(final URL url, ClassLoader parent) {
    super(parent);

and finally added the base url to the getResourceAsStream()

final InputStream in = getResourceAsStream(base.getFile() + "/" + name.replace('.', '/') + ".class");

Thanks so much for your help guys!

Sign up to request clarification or add additional context in comments.

1 Comment

Glad to have helped! Don't forget to mark your own answer as accepted for the future reference of others.
0

Try to add leading slash into resource path: final InputStream in = getResourceAsStream("/" + name.replace('.', '/') + ".class");

This is the thing I have to try every time I am calling getResourceAsStream(). I just have tried this for you again. The path here is relative to package of class that is used to call getResourceAsStream(), so leading slash is needed to say "start from root."

1 Comment

Unfortunately this stops it from loading at all, The problem isn't loading the class; it's loading the class dependencies: java.lang.NoClassDefFoundError: com/mcgm/game/Minigame The class was found absolutely fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.