title: Using URLClassLoader to load .class file
body: <p>I'm aware that this question has been asked before:</p>
  
  <p><a href="https://stackoverflow.com/questions/738393/how-to-use-urlclassloader-to-load-a-class-file">How to use URLClassLoader to load a *.class file?</a></p>
  
  <p>However I don't really understand due to the lack of example. I'm currently working on a project and trying to load user-given .class objects which can be located in any directories on the machine.</p>
  
  <pre><code>//Create URL to hash function class file
  URL url_hashFunctionPath = new URL("file:///" + _sHashFunctionFilePath);
  
  //Packet URL to a URL array to be used by URLClassLoader
  URL[] urlA_hashFunctionPath = {url_hashFunctionPath};
  
  //Load URL for hash function via a URL class loader
  URLClassLoader urlCl_hashFunctionClassLoader = new URLClassLoader(urlA_hashFunctionPath);
  
  //Load user hash function into class to initialize later (TEST: HARD CODE FOR NOW)
  m_classUserHashClass = urlCl_hashFunctionClassLoader.loadClass(_sUserHashClassName);
  </code></pre>
  
  <p>The last line gave me a ClassNotFoundException, from my experiment &amp; understanding the user-given class function has to be in the classpath?</p>
  
  <p>PS: 1st time posting questions feel free to correct me where I did not follow the appropriate manner.</p>
  
  <p>//SOLUTION</p>
  
  <p>The solution that I arrived at with the generous help of [WillShackleford][1], this solution can load the a .class file in a given filepath. For more information refer to code and their given comments.</p>
  
  <pre><code>//The absolute file path to the class that is to be loaded (_sHashFunctionFilePath = absolute file path)
  String pathToClassFile = _sHashFunctionFilePath;
  System.out.println("File to class: " + _sHashFunctionFilePath);
  
  //Declare the process builder to execute class file at run time (Provided filepath to class)
  ProcessBuilder pb = new ProcessBuilder("javap", pathToClassFile);
  try
  {
      //Start the process builder
      Process p = pb.start();
  
      //Declare string to hold class name
      String classname = null;
      //Declare buffer reader to read the class file &amp; get class name
      try(BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())))
      {
          String line;
          while(null != (line = br.readLine()))
          {
              if(line.startsWith("public class"))
              {
                  classname = line.split(" ")[2];
                  break;
              }
          }
          System.out.println("classname = " + classname);
      }
      catch(IOException _error)
      {
  
      }
  
      //Declare file path to directory containing class to be loaded
      String pathToPackageBase = pathToClassFile.substring(0, pathToClassFile.length() - (classname + ".class").length());
      System.out.println("pathToPackageBase = " + pathToPackageBase);
  
      try
      {
          //Create class to hold the class to be loaded via a URL class loader
          Class clss = new URLClassLoader(
                  new URL[]{new File(pathToPackageBase).toURI().toURL()}
          ).loadClass(classname);
  
          //Create ab object/instance of said class
          Object test = clss.newInstance();
  
          //Declare &amp; create requested method from user hash function class (Method is work &amp; takes no arguments)
          Method method = clss.getMethod("work", null);
          method.invoke(test, null);
      }
  </code></pre>