如何在运行时获取applet jar中的所有类名(属于特定包)?

时间:2017-06-07 06:48:48

标签: java reflection applet

此代码适用于从本地计算机加载applet(c:/abcd/index.html)。但是当它从具有http://localhost:8080/example/abcd/index.html的服务器加载时,它会失败。 (index.html加载applet。)

private static ArrayList<Class<?>> getClassesForPackage(Package pkg) {
        String pkgname = pkg.getName();
        ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
        // Get a File object for the package
        File directory = null;
        String fullPath;
        String relPath = pkgname.replace('.', '/');
        logger.info("ClassDiscovery - Package: " + pkgname + " becomes Path:" + relPath);
        //URL resource = ClassLoader.getSystemClassLoader().getResource(relPath);
        URL resource = MessageUtil.class.getClassLoader().getResource(relPath);
        logger.info("ClassDiscovery - Resource = " + resource);
        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
                            + ") does not appear to be a valid URL / URI.  Strange, since we got it from the system...",
                    e);
        } catch (IllegalArgumentException e) {
            directory = null;
        }

        if (directory != null && directory.exists()) {
            // Get the list of the files contained in the package
            String[] files = directory.list();
            for (int i = 0; i < files.length; i++) {
                // we are only interested in .class files
                if (files[i].endsWith(".class")) {
                    // removes the .class extension
                    String className = pkgname + '.' + files[i].substring(0, files[i].length() - 6);
                    if (className.trim().endsWith(".") || className.contains("$"))
                        continue;
                    logger.info("ClassName: " + className);
                    try {
                        classes.add(Class.forName(className));
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException("ClassNotFoundException loading " + className);
                    }
                }
            }
        }
        else {
            try {
                String jarPath = fullPath.replaceFirst("[.]jar[!].*", ".jar").replaceFirst("file:", "");
                JarFile jarFile = new JarFile(jarPath);
                Enumeration<JarEntry> entries = jarFile.entries();
                while (entries.hasMoreElements()) {
                    JarEntry entry = entries.nextElement();
                    String entryName = entry.getName();
                    if (entryName.startsWith(relPath) && entryName.length() > (relPath.length() + "/".length())) {
                        String className = entryName.replace('/', '.').replace('\\', '.').replace(".class", "");
                        logger.info("ClassName: " + className);
                        if (className.trim().endsWith(".") || className.contains("$"))
                            continue;
                        try {
                            classes.add(Class.forName(className));
                        } catch (ClassNotFoundException e) {
                            throw new RuntimeException("ClassNotFoundException loading " + className);
                        }
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(pkgname + " (" + directory + ") does not appear to be a valid package", e);
            }
        }
        return 

0 个答案:

没有答案