簡體   English   中英

>>> URI不是分層的

[英]>>> URI is not hierarchical

抱歉,我是新來的,但我有問題,希望有人能幫助我解決。

這段代碼在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;
}

當您在Eclipse中運行代碼時,它會使用已編譯的類(默認情況下位於“目標”文件夾中)。 但是,如果通常從外部運行代碼,則使用Eclipse創建的JAR文件。

當引用由鏈接的問題解釋的JAR中的內容時,就會出現此問題。

簡而言之:文件系統中的URI在語法上是正確的。 將某些內容引用到JAR中的URI不再是有效的URI。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM