PlasmaVNGaming

Untitled

Jun 3rd, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. package com.gmail.plasmavn.plib.util;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.lang.reflect.Method;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9. import java.net.URLClassLoader;
  10. import java.util.Enumeration;
  11. import java.util.Set;
  12. import java.util.jar.JarEntry;
  13. import java.util.jar.JarFile;
  14.  
  15. import com.google.common.collect.Sets;
  16.  
  17. public class ClassLoaderUtil {
  18.  
  19. public static void registerClasses(URL url) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
  20. URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
  21. Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
  22. method.setAccessible(true);
  23. method.invoke(classLoader, url);
  24. method.setAccessible(false);
  25. }
  26.  
  27. public static URL getURL(String path) throws MalformedURLException {
  28. return new URL("jar:file:" + new File("C:\\Users\\PlasmaVN\\Desktop\\WAF\\Murder.jar") + "!/");
  29. }
  30.  
  31. @SuppressWarnings("resource")
  32. public static Set<Class<?>> getClasses(String jarfile) {
  33. Set<Class<?>> classes = Sets.newHashSet();
  34. File pathToJar = new File("C:\\Users\\PlasmaVN\\Desktop\\WAF\\Murder.jar");
  35. JarFile jarFile = null;
  36. try {
  37. jarFile = new JarFile(pathToJar);
  38. } catch (IOException ex) {}
  39. Enumeration<JarEntry> e = jarFile.entries();
  40.  
  41. URL[] urls = new URL[1];
  42. try {
  43. urls[0] = new URL("jar:file:" + pathToJar + "!/");
  44. } catch (MalformedURLException ex) {}
  45. URLClassLoader cl = URLClassLoader.newInstance(urls);
  46.  
  47. while (e.hasMoreElements()) {
  48. JarEntry je = e.nextElement();
  49. if (je.isDirectory() || !je.getName().endsWith(".class")){
  50. continue;
  51. }
  52. // -6 because of .class
  53. String className = je.getName().substring(0, je.getName().length() - 6);
  54. className = className.replace('/', '.');
  55. System.out.println("Checking for class " + className);
  56. Class<?> c = null;
  57. try {
  58. c = cl.loadClass(className);
  59. } catch (ClassNotFoundException ex) {
  60. continue;
  61. }
  62. classes.add(c);
  63. }
  64. return classes;
  65. }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment