6 Replies Latest reply: Mar 28, 2011 1:30 PM by jschellSomeoneStoleMyAlias RSS

    Dynamically load classes from file path

    850750
      Hi,

      I wanted to program a java application where I can dynamically load classes from a file-path.
      I understand how classes are loaded in general, but not how those are loaded which reside in a certain file on the file system.

      So what I have is a LinkedList full of files (respectively file paths).
      Those files all have the extension .class.
      Now i want to open those classes with a method like

      Class<?> myClass = Class.forname();

      Obviously i can't put the file-path into the forname()-method as it requires a ClassName.

      So the question is:
      How do i get from the path of a file to the class name which i can use to load the class that is represented by the file?


      Cheers

      Edited by: 847747 on 28.03.2011 01:11
        • 1. Re: Dynamically load classes from file path
          793415
          847747 wrote:
          ..I wanted to program a java application where I can dynamically load classes from a file-path.
          Why?
          • 2. Re: Dynamically load classes from file path
            850750
            To add classes on runtime and use their methods.
            • 3. Re: Dynamically load classes from file path
              793415
              847747 wrote:
              To add classes on runtime and use their methods.
              <head-desk /> Why not add them before start?

              Or to put your answer in a way that is more clear to me, express it as you might express it as a 'feature' on the shrink-wrapped packaging. E.G.

              This product is:
              <li>Whiter, brighter, with extra bubbles.
              <li>30% faster than major competitors
              <li>...
              <li>Your feature here

              Edited by: Andrew Thompson on Mar 28, 2011 10:41 AM
              • 4. Re: Dynamically load classes from file path
                850750
                Andrew Thompson wrote:
                847747 wrote:
                To add classes on runtime and use their methods.
                <head-desk /> Why not add them before start?
                Because it's supposed to be an exercise on loading classes dynamically.
                And if I add them before start, well, that would probably miss the point of the whole thing ;)
                • 5. Re: Dynamically load classes from file path
                  YoungWinston
                  847747 wrote:
                  So the question is:
                  How do i get from the path of a file to the class name which i can use to load the class that is represented by the file?
                  I believe you need ClassLoader.loadclass(), but most of them are tied in to the classpath. [url http://www.exampledepot.com/egs/java.lang/LoadClass.html]This page shows an example of loading a class that is not in the classpath using a URLClassLoader, but I don't know if it works or any 'gotchas' that might be associated with the technique, because (a) I've never had any reason to do it, and (b) I've never had the urge to try ;-).

                  Winston
                  • 6. Re: Dynamically load classes from file path
                    jschellSomeoneStoleMyAlias
                    YoungWinston wrote:
                    ... shows an example of loading a class that is not in the classpath using a URLClassLoader, but I don't know if it works
                    Certainly one can load them via that class. Whether that example is correct I don't know.
                    or any 'gotchas' that might be associated with the technique,
                    Normal problems are
                    1. Attempting to use the same class that is to be loaded in the main app.
                    2. Problems with unloading.