Search code examples
javaobjectdirectoryurlclassloader

Programming a game in java. I need to read class files from a related directory, and return an object created from those


I can get/create the directory of the file but have trouble returning the correct data type.

public fighter rndmFighter(int z) throws Exception
{
    File here = new File("encounter.java");
    String path = here.getAbsolutePath();
    path = path.substring(0,path.lastIndexOf("/"));
    path +="/Zones/"+z+"/enemy";
    File zone = new File(path);
    File[] items = zone.listFiles();
    int r = (int)(Math.random()*(items.length));
    fighter t = items[r].getName();
    return t;
}

creates the directory ./Zones/(an integer like 1)/(random).class but I have no idea how to access the information in said directory (past something like a text document)

example of a passed class file

public class imp extends fighter implements monster {
    private String name = "";

    public imp(int health, int attack, int amr) {
        super("imp", health, attack, amr);
        randomName();
        setGoldVal(4);
        setDodge(15);
        exp(3);
    }

    public imp() {
        super("imp", (10 + (int) (Math.random() * 10)), 4 + (int) (Math.random() * 2), 0);
        randomName();
        setGoldVal((int) (Math.random() * 5));
        setDodge(10 + (int) (Math.random() * 10));
        exp(expVal());
    }

    public void Name(String n) {
        n = name;
    }

    public void randomName() {
        int x = (int) (Math.random() * 5);
        switch (x) {
        case 1:
            name = "Zoryap";
            break;
        case 2:
            name = "Yazkin";
            break;
        case 3:
            name = "Azbis";
            break;
        case 4:
            name = "Quzlop";
            break;
        case 5:
            name = "Ziltik";
            break;
        default:
            name = "Taylor Rogers";
            break;
        }
    }

    public int expVal() {
        return 1 + (int) (Math.random() * 4);
    }

    public String toString() {
        if (HP() < 0)
            return name + ", the deceased imp.";
        return name + super.toString();
    }
}

The class files must be created in this way and it must return a random file from the directory, this is a class project where other people have to be able to create their own classes and add them to my code, and I don't want people to have to change my source files.

Yes, all of the files that I am trying to access are compiled.

current version of rndmFighter()

    public Object rndmFighter(int z)throws Exception
{
    File here = new File("encounter.java");
    String path = here.getAbsolutePath();
    path = path.substring(0,path.lastIndexOf("/"));
    path +="/Zones/"+z+"/enemy";
    File zone = new File(path);
    File[] items = zone.listFiles();
    int r = (int)(Math.random()*(items.length));
    path+="/"+items[r].getName();
    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    Class myClass = classLoader.loadClass(path);
    Object t = myClass.newInstance();
    return t;
}

the issue with this is that it returns ClassNotFoundException in the runner (same directory as the encounter class)

Exception in thread "main" java.lang.ClassNotFoundException: /home/user/Documents/Text Adventure/Zones/1/enemy/imp.class
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at encounter.rndmFighter(encounter.java:30)
    at testGame.main(testGame.java:12)

using the line

        fighter e = fighter.class.cast(t.rndmFighter(1));

to access the method.


Solution

  • I have resolved my issue using URLClassLoader

        public Object rndmFighter(int z)throws Exception
    {
        File here = new File("encounter.java");
        String path = here.getAbsolutePath();
        path = path.substring(0,path.lastIndexOf("/"));
        path +="/Zones/"+z+"/enemy";
        File zone = new File(path);
        File[] items = zone.listFiles();
        int r = (int)(Math.random()*(items.length));
        String s = items[r].getName().substring(0,items[r].getName().indexOf("."));
        URL url = zone.toURL();     
        URL[] urls = new URL[]{url};
        ClassLoader cl = new URLClassLoader(urls);
        Class myClass = cl.loadClass(s);
        Object t = myClass.newInstance();
        return t;
    }
    

    This system allows me to pull the required .class files and create the necessary constructor. Thanks for the pointers though :D