title: ClassNotFoundException for com.microsoft.sqlserver.jdbc.SQLServerDriver
body: <p>This is an issue that has come up <a href="https://stackoverflow.com/questions/3974114/how-can-i-fix-this-package-does-not-exist-error">before</a> on SO, and I'm sure seasoned Java devs are tired of telling newbies howto set the classpath. That said, I've tried setting the classpath via environment variable and with the -cp option, with no success.</p>
  
  <p>I'm using the sample applications that are bundled with the SQLServer JDBC driver. I'm running Ubuntu 14.10. I compile the app on the command line:</p>
  
  <pre><code>javac -cp .:/path/to/sqljdbc42.jar connectURL.java 
  </code></pre>
  
  <p>and the run it:</p>
  
  <pre><code>java connectURL
  </code></pre>
  
  <p>which gets me the familiar <code>ClassNotFoundException</code>:</p>
  
  <pre><code>java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
      at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:191)
      at connectURL.main(connectURL.java:42)
  </code></pre>
  
  <p>I haven't modified the sample class file, but I'm including it here for completeness:</p>
  
  <pre><code>import java.sql.*;
  
  public class connectURL {
  
      public static void main(String[] args) {
  
          // Create a variable for the connection string.
          String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
              "databaseName=AdventureWorks;integratedSecurity=true;";
  
          // Declare the JDBC objects.
          Connection con = null;
          Statement stmt = null;
          ResultSet rs = null;
  
              try {
                  // Establish the connection.
                  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                      con = DriverManager.getConnection(connectionUrl);
  
                      // Create and execute an SQL statement that returns some data.
                      String SQL = "SELECT TOP 10 * FROM Person.Contact";
                      stmt = con.createStatement();
                      rs = stmt.executeQuery(SQL);
  
                      // Iterate through the data in the result set and display it.
                      while (rs.next()) {
                          System.out.println(rs.getString(4) + " " + rs.getString(6));
                      }
              }
  
          // Handle any errors that may have occurred.
          catch (Exception e) {
              e.printStackTrace();
          }
  
          finally {
              if (rs != null) try { rs.close(); } catch(Exception e) {}
                  if (stmt != null) try { stmt.close(); } catch(Exception e) {}
                  if (con != null) try { con.close(); } catch(Exception e) {}
          }
      }
  }
  </code></pre>
  
  <p>The path to the SQL JDBC .jar is definitely correct. If I add <code>import com.microsoft.sqlserver.jdbc.SQLServerDriver;</code> to the class file I don't get any complaints when compiling, but still get the <code>ClassNotFoundException</code> at runtime.</p>
  
  <p>I've read elsewhere that newer versions of the JDBC don't need you to load drivers via <code>Class.forName</code>, but if I remove that line then I get, predictably, <code>java.sql.SQLException: No suitable driver found</code>.</p>
  
  <p>What am I doing wrong? I'm sure the .jar is being loaded and the class is being found, because if I try e.g. <code>import com.microsoft.sqlserver.jdbc.SomeNonExistentClass;</code> I get:</p>
  
  <p><code>connectURL.java:2: error: cannot find symbol</code></p>
  
  <p>My Java details:</p>
  
  <pre><code>$ java -version
  java version "1.7.0_79"
  OpenJDK Runtime Environment (IcedTea 2.5.5) (7u79-2.5.5-0ubuntu0.14.10.2)
  OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)
  </code></pre>