title: Maven - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
body: <p>I have a Java app based on Maven, and want to connect to MySQL server.</p>
  
  <p>My pom has:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;mysql&lt;/groupId&gt;
      &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
      &lt;version&gt;5.1.17&lt;/version&gt;
      &lt;type&gt;jar&lt;/type&gt;
      &lt;scope&gt;runtime&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>With <em>runtime</em>, as I want to connect to MySQL server at runtime - have also tried compile and provided, but does not work.</p>
  
  <p>The SQL code is standard:</p>
  
  <pre><code>String dbClass = "com.mysql.jdbc.Driver";
  
  Class.forName(dbClass);
  Connection connection = DriverManager.getConnection(dbUrl,
      username, password);
  Statement statement = connection.createStatement();
  ResultSet resultSet = statement.executeQuery(query);
  while (resultSet.next()) {
      String tableName = resultSet.getString(1);
      System.out.println("Table name : " + tableName);
  }
  </code></pre>
  
  <p>When I run this <strong>from Eclipse</strong>, it works fine and prints table names.</p>
  
  <p>However, from maven, the generated SNAPSHOT always gives an error when executed via ><code>java -jar target\File.jar</code> after running <code>mvn clean install</code>.</p>
  
  <blockquote>
    <p>java.lang.ClassNotFoundException: com.mysql.jdbc.Driver</p>
  </blockquote>
  
  <p>What am I missing here to get the maven build to work? Running <code>mvn clean install</code> gives no error and builds fine. It is only when executing the SNAPSHOT exe the error happens.</p>
  
  <p>The MySQL jar is in my .m2 repo, and I tried adding it explicitly via mvn command line, but says it already exists.</p>