It is required to make a class loader from a file for java.

I heard that this is done by overriding the ClassLoader operators, how can this be done?

On the Internet is not one normal instructions.

  • 2
    What does class mean? Implementing your Classloader is somewhere above the advanced level of Java knowledge, and what you need is probably the JavaCompiler API, dig in this direction. - Vartlok
  • Corrected) no I do not need to make my Classloader, I need to make a Classloader that works like a regular Classloader but loads classes from a file. - Denis Kotlyarov
  • Those. you need to implement your ClassLoader) And where does your default ship from? - Vartlok
  • @Vartlok, Not particularly advanced. - Nofate ♦
  • one
    @Nofate yes? Well then can share wisdom =) - Vartlok

1 answer 1

Found a very good example . The only pity is that the link to the original is broken.

Option - use URL:

// Π‘ΠΎΠ·Π΄Π°ΠΉΡ‚Π΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ File Π² ΠΊΠΎΡ€Π½Π΅ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ, которая содСрТит class-Ρ„Π°ΠΉΠ». File file = new File("c:\\myclasses\\"); try { // ΠŸΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠΉΡ‚Π΅ File Π² URL URL url = file.toURI().toURL(); // file:/c:/myclasses/ URL[] urls = new URL[]{url}; // Π‘ΠΎΠ·Π΄Π°ΠΉΡ‚Π΅ Π½ΠΎΠ²Ρ‹ΠΉ class loader с Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠ΅ΠΉ ClassLoader cl = new URLClassLoader(urls); // Π—Π°Π³Ρ€ΡƒΠ·ΠΈΡ‚Π΅ класс; MyClass.class Π΄ΠΎΠ»ΠΆΠ΅Π½ Ρ€Π°ΡΠΏΠΎΠ»Π°Π³Π°Ρ‚ΡŒΡΡ Π² // Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΠΈ file:/c:/myclasses/com/mycompany Class cls = cl.loadClass("com.mycompany.MyClass"); } catch (MalformedURLException e) { } catch (ClassNotFoundException e) { } 
  • So simple?? I'm testing - Denis Kotlyarov
  • one
    Yes, everything works fine) is a good example. - Denis Kotlyarov