Search code examples
javamysqleclipseconnectionclassnotfoundexception

Deployment Assembly and CLASSNOTFOUND Java/MySQL Issue


I've been looking on here for a couple of hours and tried a bunch of different solutions, but I haven't gotten any further. I've got my .jar file in my environment variables and all that. I made sure Eclipse has mysql-connector...jar in my build path.

I can't find the Deployment Assembly setting which leads me to believe there's something I have no idea exists that I'm doing wrong. Or maybe they took that feature out of photon? I feel like I've hit an adamantium wall.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class main {


public static void main(String[] args) {
    Connection conn = null;
try {
//Server connection details
Class.forName("com.mysql.jdbc.Driver");
String host = "jdbc:mysql://localhost/3306/db";
String userName = "admin";
String password = "admin";

 //Get the connection going.

conn = DriverManager.getConnection(host, userName, password);
}
catch (SQLException err) {
System.out.println(err.getMessage());
}
}}

Here's what I get:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

Unhandled exception type ClassNotFoundException

at main.main(main.java:12)


Solution

  • It is obvious Eclipse can't find the MySQL Jdbc driver in your project class path. Follow the steps mentioned below in Eclipse -

    1. Window -> Preferences -> Connectivity -> Driver Definitions.

    Check if MySQL JDBC Driver is visible/present in the Driver Definitions list as depicted in the image below. enter image description here

    If MySQL JDBC DRiver is not visible, Click Add and a window will appear as shown in the following image.

    enter image description here

    Select MySQL from the Filter, select your MySQL version. If a MySQL driver is already registered with your Eclipse Project, you should be able to select that and click on Edit to check if it points to the correct JAR file in your file system.

    If not, you can click on Add, select MySQL JDBC from the list, switch to the JAR tab, add the JAR file by locating it in the file system, click ok and you should be good to go.

    If you add the MySQL JDBC Connector properly, I am most certain, the ClassNotFoundException will disappear.

    However, you will have a new set of errors because you are catching only the SQLException in your main.java.

    A quick dirty fix to make your source compile fast is to use Exception as the catch argument as follows -

        import java.sql.Connection;
        import java.sql.DriverManager;
        import java.sql.SQLException;
    
        public class main {
    
    
        public static void main(String[] args) {
            Connection conn = null;
        try {
         //Server connection details
         Class.forName("com.mysql.jdbc.Driver");
         String host = "jdbc:mysql://localhost/3306/db";
         String userName = "admin";
         String password = "admin";
    
         //Get the connection going.
    
         conn = DriverManager.getConnection(host, userName, password);
        }
        catch (Exception err) { // Catch All otherwise the compiler will flag an uncaught Connection exception
          System.out.println(err.getMessage());
        }
        }}
    

    I hope this sorts out your compile+build issues. Afaik, Eclipse ignores Windows System ENV variables.