Java &燃气轮机&燃气轮机&燃气轮机;URI不是分层的

Java &燃气轮机&燃气轮机&燃气轮机;URI不是分层的,java,Java,对不起,我是新来的,但我有一个问题,我希望有人能帮我解决 此代码在eclipse中运行时非常完美,但编译后会显示: java.lang.IllegalArgumentException:URI不是分层的 任何帮助都将被占用,谢谢 public void loadMods(String pkg) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationExcep

对不起,我是新来的,但我有一个问题,我希望有人能帮我解决

此代码在eclipse中运行时非常完美,但编译后会显示:

java.lang.IllegalArgumentException:URI不是分层的

任何帮助都将被占用,谢谢

public void loadMods(String pkg) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
    List<Class<?>> classes = getClasses(pkg);
    for(Class<?> c : classes) {
        for (Method m : c.getMethods()) {
            Object o = null;
            o = c.newInstance();
            if (m.getName().contains("load")) {
                m.setAccessible(true);
                m.invoke(o);
            }
        }
    }
}

public static List<Class<?>> getClasses(String pkg) {
    String pkgname = pkg;
    List<Class<?>> classes = new ArrayList<Class<?>>();
    File directory = null;
    String fullPath;
    String relPath = pkgname.replace('.', '/');
    URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
    if (resource == null) {
        throw new RuntimeException("No resource for " + relPath);
    }
    fullPath = resource.getFile();
    try {
        directory = new File(resource.toURI());
    } catch (URISyntaxException e) {
        throw new RuntimeException(pkgname + " (" + resource + ") invalid URL / URI.", e);
    } catch (IllegalArgumentException e) {
        directory = null;
    }
    if (directory != null && directory.exists()) {
        String[] files = directory.list();
        for (int i = 0; i < files.length; i++) {
            if (files[i].endsWith(".class")) {
                String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
                try {
                    classes.add(Class.forName(className));
                } catch (ClassNotFoundException e) {
                    throw new RuntimeException("ClassNotFoundException loading " + className);
                }
            } else {
                String pkgnamex = pkgname + '.' + files[i];
                List<Class<?>> classesx = new ArrayList<Class<?>>();
                File directoryx = null;
                String fullPathx;
                String relPathx = pkgnamex.replace('.', '/');
                URL resourcex = ClassLoader.getSystemClassLoader().getResource(relPathx);
                if (resourcex == null) {
                    throw new RuntimeException("No resource for " + relPathx);
                }
                fullPathx = resourcex.getFile();
                try {
                    directoryx = new File(resourcex.toURI());
                } catch (URISyntaxException e) {
                    throw new RuntimeException(pkgnamex + " (" + resourcex + ") invalid URL / URI.", e);
                } catch (IllegalArgumentException e) {
                    directoryx = null;
                }
                if (directoryx != null && directoryx.exists()) {
                    String[] filesx = directoryx.list();
                    for (int ix = 0; ix < filesx.length; ix++) {
                        if (filesx[ix].endsWith(".class")) {
                            String classNamex = pkgnamex + '.' + filesx[ix].substring(0, filesx[ix].length() - 6);
                            try {
                                classes.add(Class.forName(classNamex));
                            } catch (ClassNotFoundException e) {
                                throw new RuntimeException("ClassNotFoundException loading " + classNamex);
                            }
                        }
                    }
                }
            }
        }
    }
    return classes;
}
public void loadMods(字符串pkg)抛出IllegaAccessException、IllegalArgumentException、InvocationTargetException、InstanceionException{
列表>获取类(字符串包){
字符串pkgname=pkg;
列表>();
文件目录=null;
字符串完整路径;
字符串relPath=pkgname.replace('.','/');
URL resource=ClassLoader.getSystemClassLoader().getResource(relPath);
if(资源==null){
抛出新的RuntimeException(“没有“+relPath”的资源);
}
fullPath=resource.getFile();
试一试{
directory=新文件(resource.toURI());
}捕获(URISyntaxException e){
抛出新的运行时异常(pkgname+“(“+resource+”)无效的URL/URI。”,e);
}捕获(IllegalArgumentException e){
directory=null;
}
if(directory!=null&&directory.exists()){
String[]files=directory.list();
对于(int i=0;i();
文件目录x=null;
字符串fullPathx;
字符串relPathx=pkgnamex.replace('.','/');
URL resourcex=ClassLoader.getSystemClassLoader().getResource(relPathx);
if(resourcex==null){
抛出新的RuntimeException(“没有“+relPathx”的资源);
}
fullPathx=resourcex.getFile();
试一试{
directoryx=新文件(resourcex.toURI());
}捕获(URISyntaxException e){
抛出新的运行时异常(pkgnamex+“(“+resourcex+”)无效的URL/URI。”,e);
}捕获(IllegalArgumentException e){
directoryx=null;
}
如果(directoryx!=null&&directoryx.exists()){
字符串[]filesx=directoryx.list();
for(intix=0;ix
当您从Eclipse中运行代码时,它会使用已编译的类(默认情况下在“target”文件夹中)。但是,如果您从外部运行代码,通常会使用Eclipse创建的JAR文件

当引用罐子里的东西时,这个问题就出现了,这是由链接的问题解释的


简言之:文件系统中的URI在语法上是正确的。引用某个内容到JAR中的URI不再是有效的URI。

同一JAR中包含的加载MOD的可能副本的可能副本似乎有问题。如果您有可选模块,有更好的技术来实现这一点,例如OSGI或Service Providerr、 你也可以尝试使用ProtectionDomain,但这相当棘手,可能会因为安全限制而失败。好吧,我终于找到了一种方法来做我想做的事情,我只是想和大家分享一下,以防有人尝试做我正在做的事情而失败。下面是我要做的工作的源代码:Thanks提供的代码。这太多了,无法在几分钟内对其进行更深层次的分析。没有侮辱,但它看起来有问题:1没有JavaDoc 2所有方法都是公共的,但一个修改了一个成员,从而改变了其他方法的行为=>以不同的顺序调用这些方法可能会产生不同的结果3)没有私有构造函数来防止意外事件实例化4)使用更多API可以缩短代码5)使用Set而不是List可能会提高性能6)无测试。代码看起来非常适合成对编程链接github.com/sowhoyo/breakcraft/blob/master/mod/ModLoader.jav‌​a坏了:'-(