Downloading classes in Java. Write-off of own class loader



  • I want to download the classes from the switch, my own, jar file, to the project.

    Specifically where I want to use it. Multimodal application. I have three modules, like A.war B.war C.jar. Where A and B are parent modules for C. So that's what I'm gonna delete module C I want to load the classes out of it with my own class loader. How can that be realized?



  • Like the commentaries said, there's a class. https://docs.oracle.com/javase/8/docs/api/java/net/URLClassLoader.html ♪

    Designer accepts as a parameter mass URL addresses before jar files.

    URL[] classLoaderUrls = new URL[]{new URL("./somestuf.jar")};
    URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls);
    

    It's not comfortable working with this:

    Class loading

    Class<?> beanClass = urlClassLoader.loadClass("ex.coolStuf.Stuf");
    

    Establishment of class object

    Constructor<?> constructor = beanClass.getConstructor();
    Object beanObj = constructor.newInstance();
    

    Method issue

    Method method = beanClass.getMethod("sayHello");
    method.invoke(beanObj);
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2