title: Java: Adding classes from external source to classpath
body: <p>So here is my issue. I am creating a piece of software that is similar to a DOS command line. I would like people to be able to add other commands by simply dropping a jar file containing classes extending the base <code>Command</code> class into a folder. Nothing I have tried yet works.</p>
  
  <p>Here is what I have tried:</p>
  
  <ol>
  <li><strong>Using the Reflections library to add search the jar file for classes extending the Command class</strong><br>
  This threw many an error and just didn't find the majority of the classes in my jar. I think this has something to do with all the <code>SomeClass$1.class</code> stuff that goes on.</li>
  <li><strong>Iterating through every file in the jar and adding it to the classpath</strong><br>
  I couldn't find a way for this to work because I simply couldn't turn the iterations of <code>ZipEntry</code> into anything that could be added to the classpath, e.g URLs.</li>
  <li><strong>Adding the whole jar to the classpath</strong><br>
  This didn't work either because the program won't know the names of these classes, therefore, it can't turn them into commands.</li>
  </ol>
  
  <p>I would love some help here. Any suggestions on how I can make my program more extensible would be very much welcome. +1 for those including code ;)<br>
  If you need any further information just let me know.  </p>
  
  <p><strong>EDIT:</strong><br>
  New code:  </p>
  
  <pre><code>URLClassLoader ucl = (URLClassLoader) ClassLoader.getSystemClassLoader();
      for(File file : FileHelper.getProtectedFile("/packages/").listFiles()) {
          if(file.getName().endsWith(".jar")) {
              Set&lt;Class&lt;?&gt;&gt; set = new HashSet&lt;Class&lt;?&gt;&gt;();
              JarFile jar = null;
              try {
                  jar = new JarFile(file);
                  Enumeration&lt;JarEntry&gt; entries = jar.entries();
                  while(entries.hasMoreElements()) {
                      JarEntry entry = entries.nextElement();
  
                      if(!entry.getName().endsWith(".class"))
                          continue;
  
                      Class&lt;?&gt; clazz;
                      try {
                          clazz = ucl.loadClass(entry.getName().replace("/", ".").replace(".class", "")); // THIS IS LINE 71
                      } catch(ClassNotFoundException e) {
                          e.printStackTrace();
                          continue;
                      }
  
                      if(entry.getName().contains("$1"))
                          continue;
  
                      if(clazz.getName().startsWith("CMD_"))
                          set.add(clazz);
  
                      jar.close();
                  }
              } catch(Exception e) {
                  //Ignore file
                  try {
                      jar.close();
                  } catch (IOException e1) {/* Don't worry about it too much... */}
                  OutputHelper.log("An error occurred whilst adding package " + OutputStyle.ITALIC + file.getName() + OutputStyle.DEFAULT + ". " + e.getMessage() + ". Deleting package...", error);
                  e.printStackTrace();
                  file.delete();
              }
  </code></pre>
  
  <p>Error thrown:    </p>
  
  <pre><code>java.lang.ClassNotFoundException: com.example.MyCommands.CMD_eggtimer
  at java.net.URLClassLoader$1.run(Unknown Source)
  at java.net.URLClassLoader$1.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(Unknown Source)
  at java.lang.ClassLoader.loadClass(Unknown Source)
  at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
  at java.lang.ClassLoader.loadClass(Unknown Source)
  at com.MyWebsite.MyApplication.Commands.CommandList.update(CommandList.java:71)
  </code></pre>
  
  <p>Line 71 has been marked in the above code.</p>