title: Java dynamic class loading fails on windows, but working fine on linux
body: <p>I am trying to load a class dynamically from a jar file. It worked fine on a Ubuntu linux box ( Sun Java Version 1.6.0_24 (b07).</p>
  
  <p>When I tried to run the same thing on Windows (Windows 7, Java version "1.6.0_14") it fails with Class Not Found exception.</p>
  
  <p>Following is code :</p>
  
  <pre><code>    try {
          String jarFile = "/sqljdbc4.jar";
          File newf = new File(jarFile);
          System.out.println(newf.getAbsolutePath());
          System.out.println("File exists ? :" + newf.exists());
          String urlPath = "jar:file://" + newf.getAbsolutePath() + "!/";
          System.out.println(urlPath);
          ClassLoader cur = Thread.currentThread().getContextClassLoader();
  
          URL[] jarUrlArray = { new URL(urlPath) };
          URLClassLoader cl = URLClassLoader.newInstance(jarUrlArray, cur);
  
          Class c = Class.forName(
                  "com.microsoft.sqlserver.jdbc.SQLServerDriver", true, cl);
          Method m[] = c.getMethods();
          for (Method mm : m) {
              System.out.println(mm.getName());
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>While running on Linux, jar is placed at root and for Windows its at c:\ (source and binaries are in some folder on C:\  so "/sqljdbc4.jar" resolves to c:\sqljdbc4.jar on windows, I have made sure that correct jar location in passed to classloader for both the platforms.</p>
  
  <p>Following is the stack trace i get on windows</p>
  
  <pre><code>java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
  at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
  at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:594)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
  at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
  at java.lang.Class.forName0(Native Method)
  at java.lang.Class.forName(Class.java:247)
  at DemoClass.loadAClass(DemoClass.java:31)
  at DemoClass.main(DemoClass.java:14)
  </code></pre>
  
  <p><strong>NOTE</strong> : You can use any jar that u have to try this out. I was playing with MS SQL Server JDBC Driver jar.</p>
  
  <p>Thanks !</p>
  
  <p>-Abhijeet.</p>