Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gmail.plasmavn.plib.util;
- import java.io.File;
- import java.io.IOException;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLClassLoader;
- import java.util.Enumeration;
- import java.util.Set;
- import java.util.jar.JarEntry;
- import java.util.jar.JarFile;
- import com.google.common.collect.Sets;
- public class ClassLoaderUtil {
- public static void registerClasses(URL url) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
- Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
- method.setAccessible(true);
- method.invoke(classLoader, url);
- method.setAccessible(false);
- }
- public static URL getURL(String path) throws MalformedURLException {
- return new URL("jar:file:" + new File("C:\\Users\\PlasmaVN\\Desktop\\WAF\\Murder.jar") + "!/");
- }
- @SuppressWarnings("resource")
- public static Set<Class<?>> getClasses(String jarfile) {
- Set<Class<?>> classes = Sets.newHashSet();
- File pathToJar = new File("C:\\Users\\PlasmaVN\\Desktop\\WAF\\Murder.jar");
- JarFile jarFile = null;
- try {
- jarFile = new JarFile(pathToJar);
- } catch (IOException ex) {}
- Enumeration<JarEntry> e = jarFile.entries();
- URL[] urls = new URL[1];
- try {
- urls[0] = new URL("jar:file:" + pathToJar + "!/");
- } catch (MalformedURLException ex) {}
- URLClassLoader cl = URLClassLoader.newInstance(urls);
- while (e.hasMoreElements()) {
- JarEntry je = e.nextElement();
- if (je.isDirectory() || !je.getName().endsWith(".class")){
- continue;
- }
- // -6 because of .class
- String className = je.getName().substring(0, je.getName().length() - 6);
- className = className.replace('/', '.');
- System.out.println("Checking for class " + className);
- Class<?> c = null;
- try {
- c = cl.loadClass(className);
- } catch (ClassNotFoundException ex) {
- continue;
- }
- classes.add(c);
- }
- return classes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment