用java编程游戏。我需要从相关目录中读取类文件,并返回从这些文件创建的对象

时间:2016-03-22 21:49:17

标签: java object directory urlclassloader

我可以获取/创建文件的目录,但无法返回正确的数据类型。

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;
}

创建目录./Zones/(an integer like 1)/(random).class 但我不知道如何访问所述目录中的信息(过去像文本文档)

传递的类文件的示例

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();
    }
}

必须以这种方式创建类文件,并且必须随机返回 来自目录的文件,这是其他人必须的类项目 能够创建自己的类并将它们添加到我的代码中,我不想要 人们必须改变我的源文件。

是的,我尝试访问的所有文件都已编译。

当前版本的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;
}

这个问题是它在跑步者中返回 ClassNotFoundException (与遭遇类相同的目录)

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)

使用

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

访问该方法。

2 个答案:

答案 0 :(得分:0)

从问题中不清楚您是否期望已编译的.class文件或源代码(.java文件)。对于后者,我想你需要javax.tools.JavaCompiler来动态编译.java文件。要动态加载第三方.class文件,您需要以下内容:

ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Class myClass = classLoader.loadClass(classNameToBeLoaded);
Object instance = myClass.newInstance();

您不关心安全问题吗?例如,如果第三方代码包含System.exit()并且您在某个时候调用它,该怎么办?

答案 1 :(得分:0)

我已使用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;
}

这个系统允许我提取所需的.class文件并创建必要的构造函数。谢谢你的指示:D