title: Custom class loader for loading dll in applet
body: <p>To address the .dll file <a href="https://stackoverflow.com/questions/4956145/how-to-unload-dll-in-java">loading/unloading issue</a> with applets. I am loading, following <a href="http://codethesis.com/sites/default/index.php?servlet=4&amp;content=2" rel="nofollow noreferrer">this tutorial</a>, .dll files using a custom class loader as : </p>
  
  <p><strong>1- Class Loader</strong> (Copied from <a href="http://codethesis.com/sites/default/index.php?servlet=4&amp;content=1" rel="nofollow noreferrer">tutorial</a>)</p>
  
  <pre><code>public class CustomClassLoader extends ClassLoader {
      private static final String CLASS_NAME = CustomClassLoader.class.getName();
  
      private Map&lt;String, Class&lt;?&gt;&gt; classes;
  
      public CustomClassLoader() {
          super(CustomClassLoader.class.getClassLoader());
          classes = new HashMap&lt;String, Class&lt;?&gt;&gt;();
      }
  
      public String toString() {
          return CustomClassLoader.class.getName();
      }
  
      @Override
      public Class&lt;?&gt; findClass(String name) throws ClassNotFoundException {
  
          if (classes.containsKey(name)) {
              return classes.get(name);
          }
  
          String path = name.replace('.', File.separatorChar) + ".class";
          byte[] b = null;
  
          try {
              b = loadClassData(path);
          } catch (IOException e) {
              throw new ClassNotFoundException("Class not found at path: " + new File(name).getAbsolutePath(), e); **
          }
  
          Class&lt;?&gt; c = defineClass(name, b, 0, b.length);
          resolveClass(c);
          classes.put(name, c);
  
          return c;
      }
  
      private byte[] loadClassData(String name) throws IOException {
          String methodName = CLASS_NAME + ".loadClassData";
          byte buff[] = null;
  
          try {
              File file = new File(name);
              int size = (int) file.length();
              buff = new byte[size];
              DataInputStream in = new DataInputStream(new FileInputStream(name));
              in.readFully(buff);
              in.close();
  
          } catch (IOException e) {
              throw new IOException(e.getMessage());
          }
          return buff;
      }
  }
  </code></pre>
  
  <p><strong>2- A class to load .dll files</strong></p>
  
  <pre><code>public class DllLoader {
  
      private static final String CLASS_NAME = AppletReload.class.getName();
  
      static String javaHome = System.getProperty("java.io.tmpdir");
      private static String dllPath = javaHome + Constant.SMARTCARD_JACSPCSC_DLL_NAME;
      private static String dllPath1 = javaHome + Constant.SMARTCARD_RXTXSERIAL_DLL_NAME;
  
      public DllLoader() {
      }
  
      static {
          try {
              System.load(dllPath);
              Logger.write(LoggerConstant.TRACE, "JACSPCSC Dll loaded from the path: " + dllPath, "Dll Loader");
              System.load(dllPath1);
              Logger.write(LoggerConstant.TRACE, "RXTXSERIAL Dll loaded from the path: " + dllPath1, "Dll Loader");
          } catch (Exception e) {
          // Log exception;
          }
      }
  }
  </code></pre>
  
  <p>And this is how I am using this class loader:</p>
  
  <pre><code>cl = new CustomClassLoader();
  ca = cl.findClass("com.DllLoader");
  a = ca.newInstance();
  </code></pre>
  
  <p>The motivation behind loading .dll using custom class loader is that it would guarantee the unloading as well and this is what most of the answers to question on .dll loading/unloading suggest. But in my case, <code>loadClassData()</code> (a function in my classLoader) thows this exception:</p>
  
  <pre><code>   loadClassData,Error: com\DllLoader.class (The system cannot find the path specified)
      java.io.FileNotFoundException: com\DllLoader.class (The system cannot find the path specified)
  </code></pre>
  
  <p>and the absolute path of file is show logged as:</p>
  
  <p>**C:\Program Files\Mozilla Firefox\com.DllLoader</p>
  
  <p>I think this is where its searching for the file, and .jar file of applet isn't located here. </p>
  
  <p>I would appreciate if anyone can point out mistake I am making or tell how can I guide browser to look for class file in correct folder.</p>
  
  <p>P.S: Kindly note that a answer to seemingly <a href="https://stackoverflow.com/questions/872905/applet-class-loader-cannot-find-a-class-in-the-applets-jar">duplicate question</a> don't solve this problem. </p>